Custom class/type following the "Nullable pattern"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prog123
    New Member
    • Nov 2008
    • 3

    Custom class/type following the "Nullable pattern"

    Hi!

    If I have a Nullable type variable, I can set its initial value using a quotation mark like this:

    Code:
    aNullableVariableOfInteger = 4
    In the background an instance of the type Nullable(Of Integer) is created and its property named "value" is set to 4.

    How can I create such a class/type? Can you please show me a pattern?
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Presumably you mean that you can set up the nullable variable using a question mark? Like this?

    Code:
    Dim myNullableInt As Integer?
    Nullable types in this sense only apply to value types. A reference type, as an object, would have the ability to be null anyway, so all classes you create will be nullable. You can however apply this to variable of a custom structure. You'd declare them in the same way, post-fixed with a question mark.

    You can find some more reading here:

    Nullable Class
    Nullable(T) Structure

    Comment

    Working...