Instance set to a null reference?

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

I have the following lines in a class:

private _State as string = nothing

'Then later in a validate method I have the line:
'See if the string is either less than or greater than 2 in size.

If _State.Length <> 2 Then



When I run a unit test I get the annoying Object instance set to a null
(nothing) reference. What is wrong with the line above?
 
Andy B. said:
I have the following lines in a class:

private _State as string = nothing

'Then later in a validate method I have the line:
'See if the string is either less than or greater than 2 in size.

If _State.Length <> 2 Then



When I run a unit test I get the annoying Object instance set to a null
(nothing) reference. What is wrong with the line above?

'Length' is a method of an instance of the 'String' type. If '_State' is a
reference to 'Nothing', the method cannot be called.

There are two ways to overcome this problem:

\\\
If _State Is Nothing OrElse _State.Length <> 2 Then
...
End If
///

- or -

\\\
If Len(_State) <> 2 Then
...
End If
///
 
Andy,

private _State as string = nothing
has not much sense, it is the same as
private _State as string

Keep the rule that as you write code, that the code has sense, otherwise
latter when there is some maintenance needed you or somebody else will
investigate why you made that.

private _State as string = ""
means that the string is empty.

Beside that is the underscore as you us it a little bit inconsequent in your
code.
An unerscore can be used to distinct a field from a property for that.
However you use no property.

Cor
 
Rather be safe than sorry. I have had some variables initialize with some
goffy stuff in it. I figure it is better to set it to something when first
creating them.
_State as string = Nothing 'make sure for a fact it is set to (null)
_State as string 'might result in string.empty or (null)

I had a public property set for _State. It wasn't relevant to what I was
doing though.
 
Andy B. said:
Rather be safe than sorry. I have had some variables initialize with
some goffy stuff in it. I figure it is better to set it to something
when first creating them.

Maybe that's true in an other world, means: for an other programming
language.
_State as string = Nothing 'make sure for a fact it is set to (null)

Sure, by this you are setting a content of Nothing to Nothing. That
never will go wrong or produce a different result.
_State as string 'might result in string.empty or (null)

No, it allways results to Nothing (null) in fact. Only in
string-comparing expressions it _will_ (not: might!) compare to
String.Empty ("").


Harald M. Genauck

"VISUAL STUDIO one" - http://www.visualstudio1.de
"ABOUT Visual Basic" - http://www.aboutvb.de
 
Andy B. said:
Rather be safe than sorry. I have had some variables initialize with some
goffy stuff in it. I figure it is better to set it to something when first
creating them.
_State as string = Nothing 'make sure for a fact it is set to (null)
_State as string 'might result in string.empty or (null)

I had a public property set for _State. It wasn't relevant to what I was
doing though.

The ability of a language to handle a null condition gives you the
oportunity to know one simple fact. If the variable is null (nothing) then
you do not know the value of the variable.

If a string is equal to String.Empty then you know that the value is and
empty string but if it is null then you don't know the value. This adds a
level of information to your application.

Example: Lets say we have a survey which is filled in by a user and there
is a field which the user does not input a value (and that passes
validation). This would mean that a value of null means that the user did
not indicate a value. The value String.Empty means that the user
intentionally input no value and of course other values mean the user input
that value.

Thus setting the value to nothing means that you don't know what value the
variable has (an entirely useful and valid value).

To get around your problem you would most likely set the value to
string.empty and then your test for the length will not fail.

LS
 
Guess I still have to get some old time c/c++ burnt out of me... *shiver*
so dim _SomeString as string is the same as dim _SomeString as string =
nothing?

What is something like dim _ISValid as boolean set to? false I probably
would imagine. Or is it nothing?
 
Guess I still have to get some old time c/c++ burnt out of me... *shiver*

You'll never get it out over your system :)
so dim _SomeString as string is the same as dim _SomeString as string =
nothing?

What is something like dim _ISValid as boolean set to? false I probably
would imagine. Or is it nothing?

Yes. VB auto initializes variables to their default values. What this
amounts to is esentially that VB initializes the memory used to 0 :) Hence,
for a reference type, the value is Nothing. For a value type, it's basically
what ever 0 means - so, for a boolean the value would be false.
 
A "Public/Private/Dim X As Y" always provides a 'default' value for the
type Y. Which value it will be depends specifically on the kind of a
type:

* Boolean: False

* All numeric value types: numeric value 0.

* DateTime: DateTime.MinValue.

* Enum: First/top entry/member.

* Structure: The structure, basically all members having their
type-specific default values. Take care: While accessing the structure
the first time, the value of these members might be changed to an
explicit assigned value or via a construtor of that structure.

* String, Char: Nothing.

* Any other object: Nothing.

If I did forget any kind of type, a type's documentation should tell
you more.


Harald M. Genauck

"VISUAL STUDIO one" - http://www.visualstudio1.de
"ABOUT Visual Basic" - http://www.aboutvb.de
 
Andy said:
I have the following lines in a class:

private _State as string = nothing

'Then later in a validate method I have the line:
'See if the string is either less than or greater than 2 in size.

If _State.Length <> 2 Then

When I run a unit test I get the annoying Object instance set to a
null (nothing) reference. What is wrong with the line above?

You could use the VB Len function, which will return 0 if
String.IsNullOrEmpty.

Andrew
 
Hi Andy

instead of

private _State as string = nothing

you might use

Private _State As String = String.Empty

hth

Michel
 
Harald M. Genauck said:
* String, Char: Nothing.

The 'Char' type's default value is 'ControlChars.NullChar' (which is the
same as 'Nothing'), but the 'String' type's default value is a reference to
'Nothing'.

Basically each variable is initialized as 'Nothing', which means "reference
to no object" and "all-zeros" for the bits occupied by a value type.
* Any other object: Nothing.

Better: Any other type.
 
Back
Top