Passing nil to ByRef

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?

Thanks

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=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?

Thanks

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.

Within myMethod it just does ‘myObj = new MsgBoxShower’. I got my method I wrote to exclusively set the variable to a new instance.

Thanks

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.

Within myMethod, do you ever create a new object that ultimately gets assigned back to myObj?

what is the CONSTRUCTOR for MsgBoxShower? perhaps therein lies your culprit

Very weird, I can’t even imagine what could cause a NOE on that line.

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

Time to post your code, I think.

My code

In Window1. open

Sub Open()
  dim myObj as MsgBoxShower
  myMethod(myObj)
  myObj.ShowMsgBox("hello")
End Sub

myMethod on Window1 is

Private Sub myMethod(byref f as MsgBoxSHower)
  f = new MsgBoxSHower
  
  if f is nil then
   break // note it doesn't break here
  end if
End Sub

plus I have a class called msgboxshower with one method

Sub ShowMsgBox(msg as string)
  MsgBox msg
End Sub

and this shows up a msgbox that says hello
so something else is going ion in your code

Are you using Operator_Convert? That could cause a NOE on that line or when assigning, depending on your converts and types.

Your original code is this:

dim myObj as MsgBoxShower myMethod(myObj) myObj.ShowMsgBox("hello")
It should be this:

dim myObj as new MsgBoxShower myMethod(myObj) myObj.ShowMsgBox("hello")

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

I overlooked that I called the method from a variable. That’s where the problem sits. Thanks