Check if is nothing

S

shapper

Hello,

I have the following:
If MyTag Is Nothing Then
...
End If

I am getting an error.

MyTag is of type HtmlTextWriterTag

Private MyTag As HtmlTextWriterTag

How can I check if a value was given to MyTag or not?

Thanks,
Miguel
 
G

Guest

Use "x = Nothing" for value types and "x Is Nothing" for reference types.
This will return true if x is set to it's default value. (a further
complication is that you can use both for strings, but that's a story for
another day).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
G

Guest

That's just another form of "MyTag Is Nothing", which won't work for value
types.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
S

shapper

That's just another form of "MyTag Is Nothing", which won't work for value
types.
--
David Antonwww.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI

David,

I got lost. You mean I should use:
If MyTag = Nothing Then ...

Is that right?

Thanks,
Miguel
 
G

Guest

Exactly. This will work for variable of any value type, including enums.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

shapper said:
Hello,

I have the following:
If MyTag Is Nothing Then
...
End If

I am getting an error.

MyTag is of type HtmlTextWriterTag

Private MyTag As HtmlTextWriterTag

How can I check if a value was given to MyTag or not?

Thanks,
Miguel

HtmlTextWriterTag is an enumeration, so the value can never be Nothing.
A value type always has a value, so you can never check if it has been
given a value or not.

The default value of a HtmlTextWriterTag variable is
HtmlTextWriterTag.Unknown. This is the value that the variable will have
if it has not been assigned any value.
 
G

Guest

In VB, "Nothing" is always a shortcut to the default value of a type, whether
the type is a ref type or value type. You just have to use "Is" with ref
types and "=" with value types. This is far more general than the C# "null",
which doesn't apply at all to value types.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
S

Scott M.

In VB, "Nothing" is always a shortcut to the default value of a type,
whether
the type is a ref type or value type.

So, how about this...

Dim x As Integer = 0

Will the following produce true?

If x = Nothing then
....
End If

Zero is the default value of an Integer type, yet I have explicitly set its
value to zero, so how could I get a true when testing against Nothing?
 
G

Guest

I could write a book on VB weirdness...
The problem is the way VB has developed over the years. It's almost as if
the VB design team has simply responded ad-hoc to user requests, no matter
how half-baked ("why can't I add parentheses to property calls?" "why can't I
omit parentheses on method calls?", "why can't Nothing apply to value
types?", "why do I have to declare my variables?", etc.).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
G

Guest

David said:
I could write a book on VB weirdness...
The problem is the way VB has developed over the years. It's almost as if
the VB design team has simply responded ad-hoc to user requests, no matter
how half-baked ("why can't I add parentheses to property calls?" "why can't I
omit parentheses on method calls?", "why can't Nothing apply to value
types?", "why do I have to declare my variables?", etc.).

Well, the last one would rather be the other way around, as not
declaring the variables was how it was done in BASIC.

"Why can't I declare my variables, as you do in a real programming
language?" ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top