Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: Is there a way to keep Nothing from defaulting to 0 for number types?

Tags:

vb.net

nothing

It's really bugging me that the VS 2010 IDE isn't barking at me for trying to pass Nothing through a method parameter that takes an user-defined enum. Instead, it's passing 0 through to the method. c# would never allow this. Is there some module-level modifier I can add like option strict that will force the IDE to not allow these types of implicit conversions?

like image 624
oscilatingcretin Avatar asked Aug 01 '12 11:08

oscilatingcretin


People also ask

Is nothing null in VB net?

What is VB.NET null ? A null value is a value that doesnt refer to any object. Strings are reference types and can be equal to the null value like any other reference type. VB.NET uses the keyword Nothing for null values.

How to assign null value in VB?

Value types can be modified at declaration to be Nullable Value Types by appending a ? to the end of the type, int? (C#) or Integer? (VB) for example. This makes the value type able to be set to null.

What is nothing in VB net?

With references, it means a reference that points to no object. And with values, it means the default (zero-initialized) value. On references, Nothing is null. It may provoke a NullReferenceException. As VB.NET developers, we must understand that "null" refers to Nothing.

Is not nothing in C#?

IsNothing is intended to work on reference types. It returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False.


1 Answers

Sadly, no.


But you can assign values to your enumeration members while skipping 0 (or use a placeholder named None or something like that), and at least handle this case at run time.

Sub Main
    MyMethod(Nothing) ' throws Exception
End Sub

Sub MyMethod(e as MyEnum)
    If e = 0 Then
        Throw New Exception
    End If
End Sub

Enum MyEnum
    a=1
    b=2
    c=3
End Enum
like image 72
sloth Avatar answered Sep 28 '22 17:09

sloth