Why doesn't a String of Nothing cause cause an exception? (BC42104) [duplicate]Difference between Equals/equals and == operator?Nothing = String.Empty (Why are these equal?)Why is Dictionary preferred over Hashtable in C#?Compiler warning: null reference exceptionVariable 'cl' is passed by reference before it has been assigned a value for DataView Variablewarning BC42104 - Variable used before assigned a valueNull Reference List(of String)Why does checking if an object is null cause a warning at compile time?Visual Basic warningvariable “stat_in” is used before it has been assigned a value. a null exception could result at runtimeVariable 'reader' is used before it has been assigned a value. A null reference exception could result at run timeVB.Net no idea how to get this to compile also Retro BASIC
How can I reorder triggered abilities in Arena?
“T” in subscript in formulas
What does "rel" in `mathrel` and `stackrel` stands for?
How to respectfully refuse to assist co-workers with IT issues?
Does this VCO produce a sine wave or square wave
Can a giant mushroom be used as a material to build watercraft or sailing ships?
Removal of て in Japanese novels
"Opusculum hoc, quamdiu vixero, doctioribus emendandum offero."?
Command "root" and "subcommands"
Why do banks “park” their money at the European Central Bank?
Rent contract say that pets are not allowed. Possible repercussions if bringing the pet anyway?
I don't have the theoretical background in my PhD topic. I can't justify getting the degree
Add 2 new columns to existing dataframe using apply
How long do you think advanced cybernetic implants would plausibly last?
How do I make my image comply with the requirements of this photography competition?
How do I prevent other wifi networks from showing up on my computer?
Why does Windows store Wi-Fi passwords in a reversible format?
Another solution to create a set with two conditions
To get so rich that you are not in need of anymore money
Boot Windows from SAN
"fF" letter combination seems to be typeset strangely or incorrectly
Ordering a list of integers
Server Integrity Check CheckCommands question
"There were either twelve sexes or none."
Why doesn't a String of Nothing cause cause an exception? (BC42104) [duplicate]
Difference between Equals/equals and == operator?Nothing = String.Empty (Why are these equal?)Why is Dictionary preferred over Hashtable in C#?Compiler warning: null reference exceptionVariable 'cl' is passed by reference before it has been assigned a value for DataView Variablewarning BC42104 - Variable used before assigned a valueNull Reference List(of String)Why does checking if an object is null cause a warning at compile time?Visual Basic warningvariable “stat_in” is used before it has been assigned a value. a null exception could result at runtimeVariable 'reader' is used before it has been assigned a value. A null reference exception could result at run timeVB.Net no idea how to get this to compile also Retro BASIC
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Difference between Equals/equals and == operator?
11 answers
If I declare a string but do not assign a value, the Equals
function throws an exception but it doesn't throw an exception if it is compared to a value.
The error list warns about the problem:
Warning BC42104 Variable
a
is used before it has been assigned a
value. A null reference exception could result at runtime.
Dim a As String
Dim b as string = "bar"
a.Equals("foo") 'causes System.NullReferenceException
a = "foo" 'No exception although a is nothing
a = b 'No exception although a is nothing
I know the warning says it COULD cause an exception, but does anyone know why this is the happening?
vb.net
marked as duplicate by Cody Gray♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 8 more comments
This question already has an answer here:
Difference between Equals/equals and == operator?
11 answers
If I declare a string but do not assign a value, the Equals
function throws an exception but it doesn't throw an exception if it is compared to a value.
The error list warns about the problem:
Warning BC42104 Variable
a
is used before it has been assigned a
value. A null reference exception could result at runtime.
Dim a As String
Dim b as string = "bar"
a.Equals("foo") 'causes System.NullReferenceException
a = "foo" 'No exception although a is nothing
a = b 'No exception although a is nothing
I know the warning says it COULD cause an exception, but does anyone know why this is the happening?
vb.net
marked as duplicate by Cody Gray♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
The warning is pretty much unrelated. It can always beNothing
, even if initialized; for example, the warning will go away if you writeDim a As String = Nothing
. Is your real question about the difference between the=
operator and the.Equals
method?
– Ry-♦
Mar 27 at 19:15
1
a.Equals
is calling a member function ona
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.
– Cody Gray♦
Mar 27 at 19:16
1
(Also, please confirm that you’re usinga = "foo"
anda = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)
– Ry-♦
Mar 27 at 19:16
1
To elaborate on what @CodyGray is saying, also note thatstring.operator==(string, string)
, like all overloaded operators, isstatic
(Shared
), at no point does the .NET framework try to doa.Equals
when it handles the=
operator;string.Equals(string)
, on the other hand is an instance method ofString
and will cause aNullReferenceException
.
– jrh
Mar 27 at 19:20
|
show 8 more comments
This question already has an answer here:
Difference between Equals/equals and == operator?
11 answers
If I declare a string but do not assign a value, the Equals
function throws an exception but it doesn't throw an exception if it is compared to a value.
The error list warns about the problem:
Warning BC42104 Variable
a
is used before it has been assigned a
value. A null reference exception could result at runtime.
Dim a As String
Dim b as string = "bar"
a.Equals("foo") 'causes System.NullReferenceException
a = "foo" 'No exception although a is nothing
a = b 'No exception although a is nothing
I know the warning says it COULD cause an exception, but does anyone know why this is the happening?
vb.net
This question already has an answer here:
Difference between Equals/equals and == operator?
11 answers
If I declare a string but do not assign a value, the Equals
function throws an exception but it doesn't throw an exception if it is compared to a value.
The error list warns about the problem:
Warning BC42104 Variable
a
is used before it has been assigned a
value. A null reference exception could result at runtime.
Dim a As String
Dim b as string = "bar"
a.Equals("foo") 'causes System.NullReferenceException
a = "foo" 'No exception although a is nothing
a = b 'No exception although a is nothing
I know the warning says it COULD cause an exception, but does anyone know why this is the happening?
This question already has an answer here:
Difference between Equals/equals and == operator?
11 answers
vb.net
vb.net
edited Mar 27 at 19:13
Milo
2,1116 gold badges19 silver badges33 bronze badges
2,1116 gold badges19 silver badges33 bronze badges
asked Mar 27 at 18:51
UggyUggy
32 bronze badges
32 bronze badges
marked as duplicate by Cody Gray♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Cody Gray♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Cody Gray♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 27 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
The warning is pretty much unrelated. It can always beNothing
, even if initialized; for example, the warning will go away if you writeDim a As String = Nothing
. Is your real question about the difference between the=
operator and the.Equals
method?
– Ry-♦
Mar 27 at 19:15
1
a.Equals
is calling a member function ona
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.
– Cody Gray♦
Mar 27 at 19:16
1
(Also, please confirm that you’re usinga = "foo"
anda = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)
– Ry-♦
Mar 27 at 19:16
1
To elaborate on what @CodyGray is saying, also note thatstring.operator==(string, string)
, like all overloaded operators, isstatic
(Shared
), at no point does the .NET framework try to doa.Equals
when it handles the=
operator;string.Equals(string)
, on the other hand is an instance method ofString
and will cause aNullReferenceException
.
– jrh
Mar 27 at 19:20
|
show 8 more comments
1
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
The warning is pretty much unrelated. It can always beNothing
, even if initialized; for example, the warning will go away if you writeDim a As String = Nothing
. Is your real question about the difference between the=
operator and the.Equals
method?
– Ry-♦
Mar 27 at 19:15
1
a.Equals
is calling a member function ona
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.
– Cody Gray♦
Mar 27 at 19:16
1
(Also, please confirm that you’re usinga = "foo"
anda = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)
– Ry-♦
Mar 27 at 19:16
1
To elaborate on what @CodyGray is saying, also note thatstring.operator==(string, string)
, like all overloaded operators, isstatic
(Shared
), at no point does the .NET framework try to doa.Equals
when it handles the=
operator;string.Equals(string)
, on the other hand is an instance method ofString
and will cause aNullReferenceException
.
– jrh
Mar 27 at 19:20
1
1
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
The warning is pretty much unrelated. It can always be
Nothing
, even if initialized; for example, the warning will go away if you write Dim a As String = Nothing
. Is your real question about the difference between the =
operator and the .Equals
method?– Ry-♦
Mar 27 at 19:15
The warning is pretty much unrelated. It can always be
Nothing
, even if initialized; for example, the warning will go away if you write Dim a As String = Nothing
. Is your real question about the difference between the =
operator and the .Equals
method?– Ry-♦
Mar 27 at 19:15
1
1
a.Equals
is calling a member function on a
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.– Cody Gray♦
Mar 27 at 19:16
a.Equals
is calling a member function on a
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.– Cody Gray♦
Mar 27 at 19:16
1
1
(Also, please confirm that you’re using
a = "foo"
and a = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)– Ry-♦
Mar 27 at 19:16
(Also, please confirm that you’re using
a = "foo"
and a = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)– Ry-♦
Mar 27 at 19:16
1
1
To elaborate on what @CodyGray is saying, also note that
string.operator==(string, string)
, like all overloaded operators, is static
(Shared
), at no point does the .NET framework try to do a.Equals
when it handles the =
operator; string.Equals(string)
, on the other hand is an instance method of String
and will cause a NullReferenceException
.– jrh
Mar 27 at 19:20
To elaborate on what @CodyGray is saying, also note that
string.operator==(string, string)
, like all overloaded operators, is static
(Shared
), at no point does the .NET framework try to do a.Equals
when it handles the =
operator; string.Equals(string)
, on the other hand is an instance method of String
and will cause a NullReferenceException
.– jrh
Mar 27 at 19:20
|
show 8 more comments
1 Answer
1
active
oldest
votes
This is because Dim a As String
declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar"
declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo")
returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo"
works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If thea = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.
– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?null == "foo"
works fine in C# too with no exception.
– Ry-♦
Mar 28 at 20:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because Dim a As String
declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar"
declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo")
returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo"
works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If thea = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.
– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?null == "foo"
works fine in C# too with no exception.
– Ry-♦
Mar 28 at 20:35
add a comment |
This is because Dim a As String
declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar"
declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo")
returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo"
works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If thea = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.
– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?null == "foo"
works fine in C# too with no exception.
– Ry-♦
Mar 28 at 20:35
add a comment |
This is because Dim a As String
declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar"
declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo")
returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo"
works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.
This is because Dim a As String
declares the type of variable a but doesn't assign anything to it. This is basically saying : "This variable was made to hold a String object, but it doesn't hold any right now". On the other hand, Dim b As String = "bar"
declares the variable and its type, but also assigns a String object to it ("bar"). The reason a.Equals("foo")
returns an exception is because you only declared it without assigning anything to it (so you are trying to access an object that isn't there). a = "foo"
works because you are assigning a String object of value "foo" to the variable a. It's like saying : "This variable now holds a String object with value 'foo'".
Edit :
While your code points to the assignment of the variable a, I was made aware that you wanted to know why the = operator, as a comparison operator, works. This is because what I said earlier isn't entirely true when I said it didn't hold an object. It actually is of Nothing value (which sets it as a null reference) if no String object was assigned to it (String is a nullable object).
See : https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
Hope this helps.
edited Mar 27 at 19:32
answered Mar 27 at 19:09
Olivier SamsonOlivier Samson
5112 silver badges11 bronze badges
5112 silver badges11 bronze badges
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If thea = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.
– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?null == "foo"
works fine in C# too with no exception.
– Ry-♦
Mar 28 at 20:35
add a comment |
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If thea = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.
– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?null == "foo"
works fine in C# too with no exception.
– Ry-♦
Mar 28 at 20:35
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
The question is about string comparison -- "it doesn't throw an exception if it is compared to a value". The code presented leads one to assume it is about assignment.
– TnTinMn
Mar 27 at 19:13
@TnTinMn this is true. I was looking at the code given. If the
a = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.– Olivier Samson
Mar 27 at 19:25
@TnTinMn this is true. I was looking at the code given. If the
a = "foo"
expression is used in an If statement, I assume it works because a is treated as a Nothing object, which works with the = operator.– Olivier Samson
Mar 27 at 19:25
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
You might as well cover both contingencies in your answer before someone else does. Also look at the link I posted as comment.
– TnTinMn
Mar 27 at 19:30
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
Yes this was in an If statement. Hence I was expecting an exception (like in C#) but reading TnTinMn link I see now that the VB runtime treats a string of nothing as an empty string, which now makes sense of this.
– Uggy
Mar 28 at 8:42
@Uggy: ”Like in C#” – ?
null == "foo"
works fine in C# too with no exception.– Ry-♦
Mar 28 at 20:35
@Uggy: ”Like in C#” – ?
null == "foo"
works fine in C# too with no exception.– Ry-♦
Mar 28 at 20:35
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
1
Nothing and Strings in Visual Basic
– TnTinMn
Mar 27 at 19:09
The warning is pretty much unrelated. It can always be
Nothing
, even if initialized; for example, the warning will go away if you writeDim a As String = Nothing
. Is your real question about the difference between the=
operator and the.Equals
method?– Ry-♦
Mar 27 at 19:15
1
a.Equals
is calling a member function ona
. Obviously you cannot call a member function on an object that is "null"/Nothing. However, the equality operator is special-cased to handle null objects.– Cody Gray♦
Mar 27 at 19:16
1
(Also, please confirm that you’re using
a = "foo"
anda = b
in an expression and not as a statement – preferably with real code – because it’s also a valid assignment statement. Since you already got an answer that was confused about that.)– Ry-♦
Mar 27 at 19:16
1
To elaborate on what @CodyGray is saying, also note that
string.operator==(string, string)
, like all overloaded operators, isstatic
(Shared
), at no point does the .NET framework try to doa.Equals
when it handles the=
operator;string.Equals(string)
, on the other hand is an instance method ofString
and will cause aNullReferenceException
.– jrh
Mar 27 at 19:20