如何在VB.NET中将字符初始化为空字符
我正在尝试使用如何在VB.NET中将字符初始化为空字符,vb.net,Vb.net,我正在尝试使用 Private m_clientArecode As Char='' 但是我无法分配。请不要为此使用任何内容 Private m_ClientAreaCode As Char = Nothing 在VB.Net中 不表示数据类型的默认值 虽然您不必指定任何内容来获得默认值,但这两行产生相同的结果: Private m_ClientAreaCode As Char Private m_ClientAreaCode As Char = Nothing 不要用这个 Private
Private m_clientArecode As Char=''
但是我无法分配。请不要为此使用任何内容
Private m_ClientAreaCode As Char = Nothing
在VB.Net中
不表示数据类型的默认值
虽然您不必指定任何内容来获得默认值,但这两行产生相同的结果:
Private m_ClientAreaCode As Char
Private m_ClientAreaCode As Char = Nothing
不要用这个
Private m_ClientAreaCode As Char = Nothing
在VB.Net中
不表示数据类型的默认值
虽然您不必指定任何内容来获得默认值,但这两行产生相同的结果:
Private m_ClientAreaCode As Char
Private m_ClientAreaCode As Char = Nothing
在VB.NET中没有“空字符”的概念Char
的大小是固定的,因此不能将其设为空
如果要将char变量显式初始化为空字符:
Private m_ClientAreaCode As Char? = Chr(0)
在VB.NET中没有“空字符”的概念Char
的大小是固定的,因此不能将其设为空
如果要将char变量显式初始化为空字符:
Private m_ClientAreaCode As Char? = Chr(0)
这里有一个小应用程序,显示了许多可能性。值得注意的是使用此字符时的输出
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
输出
1 one 2 one 3 one 4 one 5 one
输出缺少“两个”,并显示在同一行上。字符串包含字符,但某些方法处理“null”的方式不同。只是要知道
你会想看看这个
这里有一个小应用程序,展示了许多可能性。值得注意的是使用此字符时的输出
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
输出
1 one 2 one 3 one 4 one 5 one
输出缺少“两个”,并显示在同一行上。字符串包含字符,但某些方法处理“null”的方式不同。只是要知道
你会想看看这个
可能重复的
Private m_ClientAreaCode As Char=“”
注意使用双引号代替单引号或Private m_ClientAreaCode As Char=String。空的@Wai Ha Lee:这肯定不是重复的,因为这个问题是针对VB.Net的@schlebe-VB.NET和C#都是.NET语言,这就是为什么,例如,的文档引用的是.NET Framework而不是C#。这是一个复制品。请注意,另一个问题被标记为.NET-这是因为它不是特定于C的。抱歉,但在接受答案(重复答案)中谈论“\0”字符串与VB.NET无关。这是C++、java和C语言,而不是VB.Net。VB.Net中的Nul字符是Chr(0),但不是“\0”甚至“\0”!Private m_clientarecode As Char=“”
注意使用双引号代替单引号,或者Private m_clientarecode As Char=String。Empty
@Wai Ha Lee:这肯定不是重复的,因为这个问题是针对VB.Net的@schlebe-VB.NET和C#都是.NET语言,这就是为什么,例如,的文档引用的是.NET Framework而不是C#。这是一个复制品。请注意,另一个问题被标记为.NET-这是因为它不是特定于C的。抱歉,但在接受答案(重复答案)中谈论“\0”字符串与VB.NET无关。这是C++、java和C语言,而不是VB.Net。VB.Net中的Nul字符是Chr(0),但不是“\0”甚至“\0”!