
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) > 59 Then<BR> Do Until CInt(Mins) < 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't work. I have looked in my ASP Bible and ASPFaqs, but I don't understand how to do Functions or Subs or whatever.<BR><BR>I hope you understand what I'm trying to do.<BR><BR>TTYL<BR>Jeremy#121

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) ' do this up front<BR> hours = hours + ( mins 60 ) ' yes, that'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>

RE: I need help with this....
The problem seems to be that your function doesn't return anything. I'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't nearly as stupid as this).

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) > 59 Then<BR> Do Until CInt(PtoMin1) < 60<BR> PtoMin1 = CInt(PtoMin1)  60<BR> PtoHours1 = CInt(PtoHours1) + 1<BR> Loop<BR>End If<BR>If CInt(HolidayMin1) > 59 Then<BR> Do Until CInt(HolidayMin1) < 60<BR> HolidayMin1 = CInt(HolidayMin1)  60<BR> HolidayHours1 = CInt(HolidayHours1) + 1<BR> Loop<BR>End If<BR>If CInt(OfficeMin1) > 59 Then<BR> Do Until CInt(OfficeMin1) < 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'm trying to cut the code down, so it is easier to debug!!<BR><BR>Thanks.<BR>Jeremy#121<BR>

It's why VBS has BYREF <eom>

Did you try it???
define the sub and then try it!<BR><BR><%<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>%><BR><BR>You don't really even need the CINT calls, since the and MOD operators will convert the numbers to integers, anyway.<BR><BR>

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><%<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>%><BR>

RE: My 2 cents...
<%<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>%><BR><BR><%= GetHrs("33","559") %><BR><BR>Just for fun...

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'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>

RE: Oh well...
I wasn't quite following was I, hmm.<BR>At least I feel I'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

Forum Rules

