VB6's vbNull equivalent in VB.Net?

2019-07-31 16:56发布

I would like to discover and use the VB.Net equivalent to replace the VbNull value seen in VB6, but I can't find the proper value that represents that null value.

Dim formatProvider As NumberFormatInfo = 
    DirectCast(CultureInfo.GetCultureInfo("es-ES").NumberFormat.Clone, NumberFormatInfo)

With formatProvider
    .NumberDecimalSeparator = Microsoft.VisualBasic.vbNull
End With

Dim pi As String = Math.PI.ToString(String.Empty, formatProvider)

The NumberDecimalSeparator property does not accepts an empty value, but it accpets the vbNull value as well and it works perfectlly (the separator is removed), so... I suppose that I can reproduce the same result using the .Net Class library to avoid resort at VB6 things.

I've tried to replace the vbNull with those other values, but all give an exception due to the emptiness restriction of the property that I mentioned above:

""
String.Empty
ControlChars.NullChar()
DBNull.Value.ToString
Nothing

UPDATE

I also tried these supposed solutions, any of these throws an exception of empty value but instead that it does not give me the expected result because using this as a separator to format a decimal number the number is printed broken.

Convert.ToChar(0)
Char.ConvertFromUtf32(0)
New String(Char.ConvertFromUtf32(0))

Source: VB.NET - string of nulls

2条回答
三岁会撩人
2楼-- · 2019-07-31 17:05

vbnull is a datatype and not what you think it is. From Object Viewer (VB6 or Office VBA Editor press F2)

Const vbNull = 1
    Member of VBA.VbVarType
    Return value constant for VarType

Things you may have it confused with

Const vbNullChar = ""
    Member of VBA.Constants
    Basic constant for a single Null character (ASCII value 0); equivalent to Chr$(0)

Const vbNullString = ""
    Member of VBA.Constants
    Constant for  use when calling external procedures requiring a string whose value is zero
查看更多
时光不老,我们不散
3楼-- · 2019-07-31 17:11

Trigger is correct vbNull is a constant, but its value is defined thusly: Public Const vbNull As VariantType = VariantType.Null

Please see Microsoft Reference Source

查看更多
登录 后发表回答