Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Next Date

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
I am trying to build a function that will receive a DateTime and an Integer and then return a new DateTime:

Code:
Function GetNextDate(ByVal myDate As DateTime,ByVal myInt As Integer) As DateTime

The scenerio is, I'm working with daily schedule recurrences. A daily schedule recurrence will look something like:

Starting on June 19, 2004 at 4:30PM (this is the DateTime that will be passed to the function), run the schedule every 4 (this will be the integer that is passed to the function) days.

The function will take the two values (datetime and integer) and determine what the next "runtime" of the schedule is. Keep in mind that the next runtime must be equal or greater than Now().

Anyone have any helpful hints on how I should go about this.

tia
 
I wish it were that easy.

The problem is, the myDate value may be, let's say, 5 years old. So if I were to add, let's say, 4 days to the myDate, it's still in the past.

I need to figure out how to find the next "future" date that is in "sync" with the recurrence pattern - using the myDate and myInt values.

thanks
 
why not keep adding myint to mydate until mydate > now?


Sweep
...if it works dont mess with it
 
Would the solution look somthing like this:

Code:
Function GetNextDate(ByVal myDate As DateTime, ByVal myInt As Double) As DateTime

        Do While myDate <= Now
            myDate = DateAdd(DateInterval.Day, myInt, myDate)
        Loop
        Return myDate

    End Function


Thanks and best regards,
-Lloyd
 
I have Option Explicit = On and Option Strict = On.

Function GetNextDate(ByVal myDate As DateTime, ByVal myInt As Double) As DateTime

ByVal means that the value as opposed to a reference is passed

myDate = DateAdd(DateInterval.Day, myInt, myDate)

causes neither a compile time nor a run time error.

Why?

Where is the assignable variable myDate declared in Crystalyzer's code?

Why does Crystalyzer's code work?
 
The variables myDate and myInt are declared in the fuction declaration.

Function GetNextDate(ByVal myDate As DateTime, ByVal myInt As Double) As DateTime

Thanks and best regards,
-Lloyd
 
I never knew ByVal meant read only. What I do know is that it works.

Anyone have any other ideas?

Thanks and best regards,
-Lloyd
 
My understanding was that ByVal meant that the value was Read Only - however

msdn said:
ByVal
Optional. Indicates that the procedure cannot replace or reassign the underlying variable element in the calling code. However, if the argument is a reference type, the procedure can modify the contents or members of the underlying object. ByVal is the default in Visual Basic.

In a sense it is - but it seems that a ByVal parameter can be altered within a Sub/Function - it just wont overwrite the value in the calling code, whereas ByRef will.

This seems to mean that ByVal parameters are manipulated on the Sub's or Function's stack and we are given access to them as if they were locally declared variables.

To be honest, I think this is a confusing situation and one which Option Strict and/or Option Explicit should prevent. Nevertheless, as I originally said your code works - but personally I would always declare a local variable if I need to change the value of an incoming ByVal parameter.
 
You suggestion of a locally declared variable makes sense so that the originally passed value is maintained. I just saw a way to eliminate a few lines of code and being a hack novice I took it! LOL

Thanks for the lesson!


Thanks and best regards,
-Lloyd
 
There are 2 ways to pass a variable.

ByRef
ByRef means that when the function is called, a reference to the memory address of the parameter is passed. Since the function is receiving the memory location of that value, any changes to that value will be changed in memory.

ByVal
ByVal means that the value stored by the variable is copied in memory, and that copied version is passed to the function. Any changes made to that variable in the function will not effect the calling class's value because it is not the same piece of memory.

Code:
public sub PassByRef(ByRef MyInt as Integer)
  MyInt = 2
End Sub

public sub PassByVal(ByVal MyInt as Integer)
  myInt = 2
end sub

Public Sub Main()
  dim MyRefInt as integer = 1
  dim MyValInt as integer = 1

  passbyref(MyRefInt)
  passbyval(MyValInt)

  messagebox.show (MyRefInt.tostring) 'will display "2"
  messagebox.show (MyValInt.tostring) ' will display "1"
end sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, that is not strictly the case, which is why I don't like using ByVal parameters as anything other than Read Only.

If the variable is a reference type as opposed to a value type it is effectively passed ByRef (see MSDN notes) and thus susceptible to unexpected results.

 
I have never heard of any unexpected results. If you pass a parameter by ref, it is the same memory location, and the value will change. That is the expected result. This doesn't have anything to do with being read only. Even if the parameter is passed in ByVal the function can allter the calling method's variable if it is public.
<BR><BR>
-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top