1

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

Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 1
    .NumberDecimalSeparator = ChrW(&H200B) – Hans Passant Oct 17 '14 at 10:14
  • 2
    Hans is suggesting using a `zero-length space` - it's a character that is not displayed - http://www.fileformat.info/info/unicode/char/200B/index.htm – MarkJ Oct 17 '14 at 11:26
  • VB6's VbNull doesnt do what you think it does. It was for comparing the VarType of objects (ala modern GetType). VbNull would be the equivalent of Nothing in NET. – Ňɏssa Pøngjǣrdenlarp Oct 17 '14 at 19:55
  • @Plutonix, 'Nothing' does not work in the code above, it throws an exception of empty value, while vbNull works perfect. – ElektroStudios Oct 18 '14 at 10:51
  • 1
    I did not say it would, I was explaining what it was. VB.VBNull is just the constant `1`. In VB6 it was used something like this: `If VarType(foo) = vbNull` to test for a null object. `Convert.ToChar(1)` would be the equivalent unprintable control char. `VarType` and `VbNull` still exist in the NET VB Namespace for compatibility, but the *Zero Width Space*, U+200B suggested would be the clean NET approach. – Ňɏssa Pøngjǣrdenlarp Oct 18 '14 at 11:43
  • @MarkJ as you said, the Hans Passant character solution is not displayed, but it is PROCESSED, what I mean?, that if you iterate the characters of a string formatted with that trick (zero-length char) the unwanted character will be processed, while using vbNull does not happens this, just vbNull is perfect and non-intrusive in the code above, then which is the really equivalent for this in Vb.NET? – ElektroStudios Oct 19 '14 at 11:40
  • 1
    not so sure about "perfect". `.NumberDecimalSeparator = Microsoft.VisualBasic.vbNull` is illegal under Option Strict. [This shows that](https://dotnetfiddle.net/ghSBPD) the length is the same, just that *what* is stored as char(1) varies. So I dont know what you mean by PROCESSED. Embedding a control char (1, vbNull) seems very dicey to me. – Ňɏssa Pøngjǣrdenlarp Oct 19 '14 at 12:19
  • "I dont know what you mean by PROCESSED", if you use Hans Passant solution and do a For Each Char Loop in a formatted numeric string then the "empty" character will be processed/displayed, while if you do the same using vbNull it does not. – ElektroStudios Oct 19 '14 at 12:52
  • actually, the vbNull method is storing a literal `1` in the char array as shown in the fiddle (and easily seen with intellisense). Since they **all** result in strlen 16, it is a matter of *what* is stored. The VS immediate window shows differently than the fiddle. There is always: `strNum.Where(Function(x) Char.IsDigit(x)).ToArray` – Ňɏssa Pøngjǣrdenlarp Oct 19 '14 at 13:36

2 Answers2

3

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

Community
  • 1
  • 1
lurker
  • 159
  • 6
2

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