Testing if a varible is null
-
My original code looked like this:
If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then
But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:
If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then
(replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?
-
My original code looked like this:
If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then
But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:
If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then
(replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?
if you want to test if the string is empty use if
strNumber.Equals(string.empty)
, if you want to test the object is an actual object useif strNumber isNot Nothing
So to test the list you have above;If (strNumber isnot nothing) and (strProvider isNot Nothing) and (strEmail isNot nothing) then
or if you are testing for empty string i.e "" then
if strnumber.equals(string.empty) and strProvider.equals(string.empty) and strEmail.equals(string.empty) then
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
-
if you want to test if the string is empty use if
strNumber.Equals(string.empty)
, if you want to test the object is an actual object useif strNumber isNot Nothing
So to test the list you have above;If (strNumber isnot nothing) and (strProvider isNot Nothing) and (strEmail isNot nothing) then
or if you are testing for empty string i.e "" then
if strnumber.equals(string.empty) and strProvider.equals(string.empty) and strEmail.equals(string.empty) then
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
If it's VB.NET you can also combine them in one:
String.IsNullOrEmpty(strNumber)
Possibly not relevant to the original question, but useful to know.
-
My original code looked like this:
If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then
But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:
If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then
(replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?
The problem here is that vbNull is not the same as Null. vbNull is an integer value. It is the code value which represents a variant type of null. So,
VarType(Null) = vbNull
If this is VB6 you check for a null variant value with:
IsNull(aValue)
If it is VB.NET, as mentioned elsewhere you should use
strNumber IsNot Nothing
or, if you are still using an older version of .NET you will have to turn it round and say
Not (strNumber Is Nothing)
since the IsNot keyword was introduced in .NET 2 (I think). Also, the & operator does something completely different to the And keyword. You can't simply replace one with the other.
-
The problem here is that vbNull is not the same as Null. vbNull is an integer value. It is the code value which represents a variant type of null. So,
VarType(Null) = vbNull
If this is VB6 you check for a null variant value with:
IsNull(aValue)
If it is VB.NET, as mentioned elsewhere you should use
strNumber IsNot Nothing
or, if you are still using an older version of .NET you will have to turn it round and say
Not (strNumber Is Nothing)
since the IsNot keyword was introduced in .NET 2 (I think). Also, the & operator does something completely different to the And keyword. You can't simply replace one with the other.
Sudden thought: if it's VB6 you may also need to read up on the difference between IsNull and IsEmpty.
-
Sudden thought: if it's VB6 you may also need to read up on the difference between IsNull and IsEmpty.
I thought the new ruling on the VB board was assume .Net unless the OP tells you differently, that way [vb6 users] will be bomarded with all the .net goodness, and have to admit they are using VB6 and realise its maybe time to move on.
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
-
I thought the new ruling on the VB board was assume .Net unless the OP tells you differently, that way [vb6 users] will be bomarded with all the .net goodness, and have to admit they are using VB6 and realise its maybe time to move on.
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
that would be good, if only it got displayed on top of this forum so all can see it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
that would be good, if only it got displayed on top of this forum so all can see it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
Luc Pattyn wrote:
that would be good, if only it got displayed on top of this forum so all can see it.
:thumbsup: I mentioned it previously that it was maybe time to splinter of the formum for VB6, and let it die gracefully in the background, and rename this one VB.Net VB6 is the new Fight Club First rule of VB6 - You don't talk about VB6!
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
-
Luc Pattyn wrote:
that would be good, if only it got displayed on top of this forum so all can see it.
:thumbsup: I mentioned it previously that it was maybe time to splinter of the formum for VB6, and let it die gracefully in the background, and rename this one VB.Net VB6 is the new Fight Club First rule of VB6 - You don't talk about VB6!
Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
the problem with splitting this forum (or any other) is it would leave a lot of old messages in the wrong half. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
the problem with splitting this forum (or any other) is it would leave a lot of old messages in the wrong half. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
Granted, but the benefits of a split outweigh the inconvenience of having old messages either here or in the newly split VB6 forum. Since, I can only assume it is safe to say that the majority of old messages or threads revolve around .net, and a solid percentage of the posts of VB6 are a standard 'upgrade to .net, ms no longer supports vb6, or you can't do that in vb6' posts then it would be wiser to leave the content within the vb.net forum as it is more likely to be beneficial to other members here. Splitting them isn't that bad an idea.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
-
My original code looked like this:
If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then
But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:
If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then
(replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?
-
Well, it works. But before you use the next time the "&" operator, you should know that he is the string concatenation operator (yes, also in VBx age ...)