2

I am trying to assign empty char using

Private m_ClientAreaCode As Char = ''

But I am not able to assign.

Vivek S.
  • 19,945
  • 7
  • 68
  • 85
user3603130
  • 21
  • 1
  • 2
  • Possible duplicate of [Why is there no Char.Empty like String.Empty?](http://stackoverflow.com/questions/3670505/why-is-there-no-char-empty-like-string-empty) – Wai Ha Lee Dec 01 '16 at 10:05
  • `Private m_ClientAreaCode As Char = ""` note use double quotes instead of single quotes *or* `Private m_ClientAreaCode As Char = String.Empty` – Vivek S. Dec 01 '16 at 10:05
  • @Wai Ha Lee: It is certainly not a duplicate since this question is for VB.Net ! – schlebe Jul 03 '20 at 06:09
  • @schlebe - VB.NET and C# are both .NET languages which is why, for instance, the documentation for [`string.Empty`](https://learn.microsoft.com/en-us/dotnet/api/system.string.empty?view=netframework-4.8) refers to .NET Framework and not C#. This *is* a duplicate. Note that the other question is tagged .NET - it's because it's *not* C# specific. – Wai Ha Lee Jul 03 '20 at 06:51
  • Sorry, but speaking about "\0" string in accepted answer (duplicate answer) has nothing to do with VB.NET. This is C++, Java and C# but not VB.Net. Nul character in VB.Net is Chr(0) but not "\0" or even '\0' ! – schlebe Jul 03 '20 at 12:01

3 Answers3

1

Here is a little app that shows the many possibilities. What is interesting to note is the output when using this character.

Private m_ClientAreaCode As Char

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Debug.WriteLine("")
    Dim test1 As String = "one"
    Dim test2 As String = "two"

    Dim test As String

    test = "    1 " & test1 & m_ClientAreaCode & test2
    Debug.WriteLine(test)

    m_ClientAreaCode = Nothing
    test = "    2 " & test1 & m_ClientAreaCode & test2
    Debug.WriteLine(test)

    m_ClientAreaCode = Chr(0)
    test = "    3 " & test1 & m_ClientAreaCode & test2
    Debug.WriteLine(test)

    m_ClientAreaCode = ControlChars.NullChar
    test = "    4 " & test1 & m_ClientAreaCode & test2
    Debug.WriteLine(test)

    m_ClientAreaCode = CChar("")
    test = "    5 " & test1 & m_ClientAreaCode & test2
    Debug.WriteLine(test)
End Sub

Output

1 one    2 one    3 one    4 one    5 one

The output is missing 'two' and appears on the same line. The string contains the characters but certain methods deal with the 'null' differently. Just be aware.

You will want to read this,

https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396#EmbeddedNulls

dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

Just use Nothing for this

Private m_ClientAreaCode As Char = Nothing

In VB.Net

Nothing represents the default value of a data type.

Documentation

Though you don't have to assign anything to get the default value, so these two lines produce identical outcome:

Private m_ClientAreaCode As Char
Private m_ClientAreaCode As Char = Nothing
Esko
  • 4,109
  • 2
  • 22
  • 37
0

There's no concept of "empty character" in VB.NET. Char is of fixed size, so you cannot make it empty.

If you want to initialize a char variable as null character explicitly:

Private m_ClientAreaCode As Char? = Chr(0)
Ripple
  • 1,257
  • 1
  • 9
  • 15