Answered by:
Non null object in a helper method is returned as a null object

Question
-
I have a very weird problem:
I’ve created an object in a helper method that is being returned to another method. I know that the object is not null, because I’ve been able to output its title in my log file, but when it is returned to the enclosing method, it seems to resolve to null. I’ve never had this before and it just doesn’t seem right. Has anyone else experienced this? Does this serve me right for coding in VB?
Tuesday, April 5, 2016 2:43 PM
Answers
-
How are you returning the object to the calling method?
If the helper method creates the object, then you need to use 'ByRef' so that the calling method sees it.
In general, you need ByRef if the called method changes the identity of the object (usually via assignment) and you want the calling method to be able to see the new object.
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter- Proposed as answer by Cor Ligthert Wednesday, April 6, 2016 5:00 PM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, April 18, 2016 9:14 AM
Wednesday, April 6, 2016 1:35 PM -
How are you returning the object to the calling method?
If the helper method creates the object, then you need to use 'ByRef' so that the calling method sees it.
In general, you need ByRef if the called method changes the identity of the object (usually via assignment) and you want the calling method to be able to see the new object.
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB ConverterIt's possible that the OP used a return parameter in their method, but that doesn't fit the description.
"I’ve created an object in a helper method that is being returned to another method."
This indicates that the new object instance is created in the method being called and is returned to the caller, presumably through the Return statement in a function. If the method were using a return parameter requiring the ByRef keyword then the new object instance would have been created in the caller and only updated by the helper. I would expect that statement to read more like "I've created an object and passed it to a helper method...".
At this point we are all just guessing until we see the code.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Marked as answer by Herro wongMicrosoft contingent staff Monday, April 18, 2016 9:14 AM
Wednesday, April 6, 2016 7:40 PM
All replies
-
A couple of things come to mind:
- Scope
- Confusion between similarly named objects of maybe different types..
Do you have Option Strict on?
Tuesday, April 5, 2016 2:55 PM -
What kind of Null?
- An DataBase kind of value named Null
- A non instanced object
The later is in VB named "nothing"
Success
CorTuesday, April 5, 2016 4:36 PM -
I’ve created an object in a helper method that is being returned to another method.
Tuesday, April 5, 2016 9:49 PM -
I have a very weird problem:
I’ve created an object in a helper method that is being returned to another method. I know that the object is not null, because I’ve been able to output its title in my log file, but when it is returned to the enclosing method, it seems to resolve to null. I’ve never had this before and it just doesn’t seem right. Has anyone else experienced this? Does this serve me right for coding in VB?
Can you show use those two methods? The one that makes the new object and the one that calls it?
Without seeing your code its hard to say where you might have gone wrong. I wouldn't say this "serves you right" for coding in VB. If you ran into a memory leak because you mismatched a malloc with a delete, then that would serve you right for coding in a mixed C/C++ environment. But this just sounds like a simple beginner error, the likes of which you could expect to experience when learning any new language.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Wednesday, April 6, 2016 6:30 AM -
How are you returning the object to the calling method?
If the helper method creates the object, then you need to use 'ByRef' so that the calling method sees it.
In general, you need ByRef if the called method changes the identity of the object (usually via assignment) and you want the calling method to be able to see the new object.
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter- Proposed as answer by Cor Ligthert Wednesday, April 6, 2016 5:00 PM
- Marked as answer by Herro wongMicrosoft contingent staff Monday, April 18, 2016 9:14 AM
Wednesday, April 6, 2016 1:35 PM -
Dave,
Good catch and it is so obvious.
Success
CorWednesday, April 6, 2016 5:02 PM -
Dave,
Good catch and it is so obvious.
Success
CorThanks Cor.
When to use 'ByRef' is confusing to a lot of people, but I think the rule to remember is "does the identity of the object change?". This seems to isolate the necessary cases. Some people seem to mistakenly think that reference type parameters never need 'ByRef'.
An assignment to the object is the most obvious identity change - it can also happen when the object is passed to a further method to a 'ByRef' parameter.
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB ConverterWednesday, April 6, 2016 7:06 PM -
How are you returning the object to the calling method?
If the helper method creates the object, then you need to use 'ByRef' so that the calling method sees it.
In general, you need ByRef if the called method changes the identity of the object (usually via assignment) and you want the calling method to be able to see the new object.
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB ConverterIt's possible that the OP used a return parameter in their method, but that doesn't fit the description.
"I’ve created an object in a helper method that is being returned to another method."
This indicates that the new object instance is created in the method being called and is returned to the caller, presumably through the Return statement in a function. If the method were using a return parameter requiring the ByRef keyword then the new object instance would have been created in the caller and only updated by the helper. I would expect that statement to read more like "I've created an object and passed it to a helper method...".
At this point we are all just guessing until we see the code.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Marked as answer by Herro wongMicrosoft contingent staff Monday, April 18, 2016 9:14 AM
Wednesday, April 6, 2016 7:40 PM -
When to use 'ByRef' is confusing to a lot of people, but I think the rule to remember is "does the identity of the object change?". This seems to isolate the necessary cases. Some people seem to mistakenly think that reference type parameters never need 'ByRef'.
I would avoid identity, that looks like identifier. However, Reed is right, we don't know if the object is returned by a function or is simply put in a sub. If it is a function, then it is of course not important if it is passed byref (it is simply not passed).
Although I think that the chance that it is a sub and your catch is right is high because this are than the normal problems.
Success
CorWednesday, April 6, 2016 10:22 PM