Results 1 to 4 of 4

Thread: [2005] Nullable Structures

  1. #1

    Thread Starter
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    [2005] Nullable Structures

    Visual Studio 2008 introduced a way to specify that you want a nullable instance of a structure with the following:
    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3. Public Class Form1
    4.     Private Enum myEnum
    5.         Item1
    6.         Item2
    7.         Item3
    8.     End Enum
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Dim someValue As myEnum?
    11.         MessageBox.Show(someValue.ToString)
    12.     End Sub
    13. End Class
    This is valid regardless of whether you are compiling to 2.0, 3.0 or 3.5. My issue is, the project I am working on is made in visual studio 2005. At this time, we are unable to use visual studio 2008 for development; so I would like to know how this is done in 2.0? I assume it's possible considering I can compile to 2.0 in VS2008 using this syntax.

    Thanks.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Nullable Structures

    How about:
    Code:
            Dim someValue As Nullable(Of myEnum) = Nothing
            MessageBox.Show(someValue.ToString)
    All though it looks like this will hinder intellisense for the someValue object.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [2005] Nullable Structures

    Thanks

    I had considered that, but I was kind of hoping there was a way to apply a tag to the enumeration (or any structure). That way the value is still nullable, but intellisense is preserved.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Nullable Structures

    Nothing has changed in the .NET Framework for nullable types since .NET 2.0 was released. All that has changed is that VB 2008 added the same shorthand for declaring nullable types as C# has from the start. "myEnum?" is just a shorthand for "Nullable(Of myEnum)". That's all. There is no difference between the two as far as implementation is concerned.

    That said, nullables are generic, hence the "(Of myEnum)" part. As such, you get just as much Intellisense with nullables as you do with other generics, which is to say that you full Intellisense. Remember, though, that if you declare a variable as a nullable type of T then the variable is NOT type T. It is type T? or Nullable(Of T). The object's Value property is type T.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width