Passing nil to a byref deceleration in the method parameters seems to not be allowed. What’s the best alternative to this?
Should I create an instance of an object of the type of the object being passed in or is there a better approach which someone could recommend.
Here is an example of a method:
dim myObj as MsgBoxShower
myMethod(myObj)
myObj.ShowMsgBox("hello")
In the above code, I would intend to use byref to manipulate the myObj value but this is invalid?
[quote=219196:@Dave S]show the signature of the “myMethod”
sub myMethod(byref xyz AS MsgBoxShower)
should work… although depending on what MsgBoxShower is subclassed from, objects are always passed byRef[/quote]
When calling myMethod, I get a NilObjectException?
The exception is probably coming from within myMethod. Like it’s working on the parameter without checking it for nil. Otherwise passing in nil is fine.
Hmm, I don’t know, that should work. Here’s the code I used to confirm what I think I know and I get the msgbox.
[code]Sub Action()
dim d As Date
myMethod(d)
MsgBox d.LongDate
End Sub
Private Sub myMethod(byref d As Date)
d = new Date
End Sub
[/code]
Maybe there’s something in the MsgBoxShower Constructor that’s exceptioning. Try breaking before calling myMethod and step through until you get the NOE. What line is that?
[quote=219205:@Will Shank]Hmm, I don’t know, that should work. Here’s the code I used to confirm what I think I know and I get the msgbox.
[code]Sub Action()
dim d As Date
myMethod(d)
MsgBox d.LongDate
End Sub
Private Sub myMethod(byref d As Date)
d = new Date
End Sub
[/code]
Maybe there’s something in the MsgBoxShower Constructor that’s exceptioning. Try breaking before calling myMethod and step through until you get the NOE. What line is that?[/quote]
The ‘myMethod(d)’ line is highlighted with a NilObjectException.
I found the problem still occurs if I assign the value to a new instance before executing the method. The Constructor does not have any kind of conflict. Kem, the method’s whole point is to assign a new value to that variable. Thanks
that has the effect of creating one just to throw it away like
dim d as new date
d = fodleritem.CreationDate
does
And if you look at the code I posted completely unnecessary
It works with a nil reference passed
No idea why olivers code isn’t working but the snippets he’s posted are too short to determine why