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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With