I need help with this....

# Thread: I need help with this....

1. Member
Join Date
Dec 1969
Posts
48

## I need help with this....

Hi-<BR><BR>I have a stupid question. I have some code that I use over and over again. I would like to write something like this...<BR><BR>Function ConvertMinToHours(Hours, Mins)<BR> If CInt(Mins) &#062; 59 Then<BR> Do Until CInt(Mins) &#060; 60<BR> Mins = CInt(Mins) - 60<BR> Hours = CInt(Hours) + 1<BR> Loop<BR> End If<BR>End Function<BR><BR>Then from my page I would like to<BR>ConvertMinToHours(23, 98)<BR>But that doesn&#039;t work. I have looked in my ASP Bible and ASPFaqs, but I don&#039;t understand how to do Functions or Subs or whatever.<BR><BR>I hope you understand what I&#039;m trying to do.<BR><BR>TTYL<BR>Jeremy#121

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## You need BYREF...

And it should be a SUB, not a function:<BR><BR>Sub ConvertMinToHours( ByRef hours, ByRef mins )<BR> hours = CInt(hours)<BR> mins = CInt(mins) &#039; do this up front<BR> hours = hours + ( mins 60 ) &#039; yes, that&#039;s a backslash<BR> mins = mins MOD 60<BR>End Sub<BR><BR>I used and MOD, to make it simpler, but your code would work, so far as I can see.<BR><BR><BR>

3. NCA
Junior Member
Join Date
Dec 1969
Posts
6

## RE: I need help with this....

The problem seems to be that your function doesn&#039;t return anything. I&#039;m no VBScript expert, but I think this sort of thing works the same as JScript. You would have to return the hours and minutes or else the change only takes place in the function. Its similar to writing:<BR><BR>200<BR><BR>then expecting a particular variable to equal 200 (but what you did isn&#039;t nearly as stupid as this).

4. Member
Join Date
Dec 1969
Posts
48

## RE: You need BYREF...

So when I do...<BR>ConvertMinToHours(23, 88)<BR>...those numbers are run through the sub?<BR><BR>What will it return? How do I get the results back into a variable?<BR><BR>See this is what I had before...<BR>If CInt(PtoMin1) &#062; 59 Then<BR> Do Until CInt(PtoMin1) &#060; 60<BR> PtoMin1 = CInt(PtoMin1) - 60<BR> PtoHours1 = CInt(PtoHours1) + 1<BR> Loop<BR>End If<BR>If CInt(HolidayMin1) &#062; 59 Then<BR> Do Until CInt(HolidayMin1) &#060; 60<BR> HolidayMin1 = CInt(HolidayMin1) - 60<BR> HolidayHours1 = CInt(HolidayHours1) + 1<BR> Loop<BR>End If<BR>If CInt(OfficeMin1) &#062; 59 Then<BR> Do Until CInt(OfficeMin1) &#060; 60<BR> OfficeMin1 = CInt(OfficeMin1) - 60<BR> OfficeHours1 = CInt(OfficeHours1) + 1<BR> Loop<BR>End If<BR><BR>I have hundreds of these, and I&#039;m trying to cut the code down, so it is easier to debug!!<BR><BR>Thanks.<BR>Jeremy#121<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## It's why VBS has BYREF <eom>

.<BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Did you try it???

define the sub and then try it!<BR><BR>&#060;%<BR>Sub FixTime( ByRef hrs, ByRef mns )<BR> hrs = hrs + mns 60<BR> mns = mns MOD 60<BR>End Sub<BR><BR>FixTime PtoHours1, PtoMin1 <BR>FixTime HolidayHours1, HolidayMin1<BR>...<BR>%&#062;<BR><BR>You don&#039;t really even need the CINT calls, since the and MOD operators will convert the numbers to integers, anyway.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I take it back...

...if the hours is a string and you call that FixTime sub with a string, then it might not work right.<BR><BR>Okay, *one* CINT, for safety:<BR><BR>&#060;%<BR>Sub FixTime( ByRef hrs, ByRef mns )<BR>hrs = CInt(hrs) + mns 60<BR>mns = mns MOD 60<BR>End Sub<BR><BR>FixTime PtoHours1, PtoMin1 <BR>FixTime HolidayHours1, HolidayMin1<BR>...<BR>%&#062;<BR>

8. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: My 2 cents...

&#060;%<BR>Function GetHrs(Hrs,Min)<BR>Dim H<BR>H = int(Min/60)<BR>M = Min - H*60<BR>GetHrs = H+Hrs &":"& M<BR>End Function<BR><BR>Function GetMin(Hrs,Min)<BR>Dim H<BR>H = int(Hrs*60)<BR>M = Min + H<BR>GetMin = M<BR>End Function<BR>%&#062;<BR><BR>&#060;%= GetHrs("33","559") %&#062;<BR><BR>Just for fun...

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Doesn't do what he asked...

...which was to alter both values in one call.<BR><BR>And if he wanted to simply right out both numbers, he could do that with a simple Sub that does the Response.Write internal to itself.<BR><BR>But I don&#039;t think he wanted to write out the numbers; I think he really did want them altered.<BR><BR>p.s.: <BR> H = Int(Min/60)<BR>is the same as <BR> H = Min 60<BR><BR>And then <BR> M = Min - H*60<BR>is the same as<BR> M = Min MOD 60<BR><BR>So assuming he really wants his answer in the form you show:<BR><BR>Function GetHrs(Hrs,Min)<BR> GetHrs = (Hrs + Min60) & ":" & (Min Mod 60)<BR>End Function<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Oh well...

I wasn&#039;t quite following was I, hmm.<BR>At least I feel I&#039;m slowly getting better. (who knows)<BR><BR>I always appreciate the corrections.<BR>jc

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•