[VB.NET] cVar = ""

  • Thread starter Alexander Vasilevsky
  • Start date
A

Alexander Vasilevsky

What is equivalent to C #, this tool please?
What is this "" when assigning a variable of type Char?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
 
H

Herfried K. Wagner [MVP]

Alexander Vasilevsky said:
What is equivalent to C #, this tool please?
What is this "" when assigning a variable of type Char?

\\\
Dim c As Char = ""
MsgBox(AscW(c)) ' 0.
///

BTW, this does not work with 'Option Strict On'.

C# equivalent: 'char x = '\0';'.
 
D

David Anton

As some additional background information to Herfried's answer, there are 3
ways (that I know of) to 'clear' a Char variable:
c = Nothing
c = New Char()
c = ""

--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
G

Göran Andersson

Alexander said:
What is equivalent to C #, this tool please?

I'm not really sure what you are asking about, so I will just answer
what I think that you are asking about...

A character literal is written using a C suffix in VB:

Dim c As Char = "p"C
What is this "" when assigning a variable of type Char?

A Char value can not be an empty string. It's always exactly one
character, never more, never less.

(Well, to be accurate, Unicode has surrogate pairs and combining
characters, so to represent a complete character you may sometimes need
more than one Char value.)
 
D

David Anton

Regardless of what should be allowed, VB regards "" as a legitimate char
assignment or comparison - and equivalent to "new Char()" or "Nothing" (note:
'Nothing' in VB means default value of the type and is not limited to
reference types).
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
G

Göran Andersson

David said:
Regardless of what should be allowed, VB regards "" as a legitimate char
assignment

Only if strict checking is disabled.
or comparison

No, actually a Char will not be compared to "". The Char will be
converted to a string, and the string will be compared to "".

So, if you assign an empty string to a Char and then compare it to an
empty string, the comparison will give a False result:

DIm c As Char = ""
If c = "" Then
' will not get here
End If
- and equivalent to "new Char()" or "Nothing" (note:
'Nothing' in VB means default value of the type and is not limited to
reference types).

Using New Char() actually creates slightly different code, but the same
result. It will create a temporary Char variable, initialise it, and
copy it to your Char variable.
 
D

David Anton

You're right - good catch on the comparison issue - I just assumed that since
the assignment was allowed (without Option Strict On), that the comparison
would also be allowed.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
H

Herfried K. Wagner [MVP]

David Anton said:
As some additional background information to Herfried's answer, there are
3
ways (that I know of) to 'clear' a Char variable:
c = Nothing
c = New Char()
c = ""

'c = ControlChars.NullChar', 'c = vbNullChar', 'c = ChrW(0)', ...

;-)
 

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

Top