NULL values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Fisher

    #1

    NULL values

    How are people handling NULL values for value types.

    Descriptions of problem

    Say you have a typical method with the signature:

    public int InsertPerson(st ring firstName, string lastName, int
    personCategoryI D)

    Now say you only want the firstName and lastName parameters to be
    required and to retain the NULL value in the database for
    personCategoryI D. Here are a couple typical solutions for this
    scenerio would be:

    A. Overload the InsertPerson method and create the following method.

    public int InsertPerson(st ring firstName, string lastName)

    So when you detect in your UI that personCategoryI D is to be NULL or
    "empty" you will call InsertPerson with two parameters. Therefore,
    the personCategoryI D field in the database will remain NULL.

    B. Treat the int as an object and use boxing and unboxing. In my
    example convert the int parameter to an object type and check for null
    in the method so I can set the stored procedure parameter to DBNull.



    My slant on the problem

    I happen to have inserts for different types of "People" that have
    many (30-40) parameters in their insert methods. So option A above
    would not be feasable and option B is more likely however I would have
    to modify alot of code to change all my int it objects and use boxing
    and unboxing(but certainly feasible). I am interested to hear how
    other people are handling this problem.

    JF
  • Frank Oquendo

    #2
    Re: NULL values

    James Fisher wrote:
    [color=blue]
    > public int InsertPerson(st ring firstName, string lastName)[/color]

    Instead of overloading, you could change the signature to accept an
    object.

    Personally, I prefer to manipulate the data directly within a DataSet.

    --
    There are 10 kinds of people. Those who understand binary and those who
    don't.




    Comment

    • Mark

      #3
      Re: NULL values

      Hi all
      From my understanding of the framework etc. everything is inherited from an
      object even system types such as int.

      You can declare a variable as an int with a value of null
      eg int personCategoryI D = null;

      The framework will then give it the default value of null for an int which
      is 0.

      You can then test for this in your function - e.g.
      public int InsertPerson(st ring firstName, string lastName, int
      personCategoryI D)
      {
      if (personCategory ID != null)
      {
      //code
      }
      }

      The only downside to this is if you use the default value (i.e. 0 in this
      case) in your database, but thats just a management thing - just start from
      1.

      Of course the other aregument about whether you should use integers or GUIDs
      for IDs comes to mind to. I would use the GUID and test for null (again a
      string of 0's in the GUID format)

      Hope it helps
      Mark
      "James Fisher" <jfish13@snip.n et> wrote in message
      news:76d6d3cd.0 307301345.48097 d50@posting.goo gle.com...[color=blue]
      > How are people handling NULL values for value types.
      >
      > Descriptions of problem
      >
      > Say you have a typical method with the signature:
      >
      > public int InsertPerson(st ring firstName, string lastName, int
      > personCategoryI D)
      >
      > Now say you only want the firstName and lastName parameters to be
      > required and to retain the NULL value in the database for
      > personCategoryI D. Here are a couple typical solutions for this
      > scenerio would be:
      >
      > A. Overload the InsertPerson method and create the following method.
      >
      > public int InsertPerson(st ring firstName, string lastName)
      >
      > So when you detect in your UI that personCategoryI D is to be NULL or
      > "empty" you will call InsertPerson with two parameters. Therefore,
      > the personCategoryI D field in the database will remain NULL.
      >
      > B. Treat the int as an object and use boxing and unboxing. In my
      > example convert the int parameter to an object type and check for null
      > in the method so I can set the stored procedure parameter to DBNull.
      >
      >
      >
      > My slant on the problem
      >
      > I happen to have inserts for different types of "People" that have
      > many (30-40) parameters in their insert methods. So option A above
      > would not be feasable and option B is more likely however I would have
      > to modify alot of code to change all my int it objects and use boxing
      > and unboxing(but certainly feasible). I am interested to hear how
      > other people are handling this problem.
      >
      > JF[/color]


      Comment

      • James Fisher

        #4
        Re: NULL values

        sullebay@bellso uth.net (bjs10) wrote in message news:<583627d0. 0307310526.4dc1 e857@posting.go ogle.com>...[color=blue]
        > This won't work; you can't set a value type to null.
        >
        > "Mark" <f1fannz@hotmai l.com> wrote in message news:<#dxW7mwVD HA.624@TK2MSFTN GP10.phx.gbl>.. .[color=green]
        > > Hi all
        > > From my understanding of the framework etc. everything is inherited from an
        > > object even system types such as int.
        > >
        > > You can declare a variable as an int with a value of null
        > > eg int personCategoryI D = null;
        > >
        > > The framework will then give it the default value of null for an int which
        > > is 0.
        > >[/color][/color]

        Yes bjs is correct,

        There really is no great solution to this. If I were using VB.NET I
        could use the combination of Nothing and IsNull and not have to change
        much. However, using C# I created a wrapper type for my value types.
        For example, I created an Integer class that wraps the good ole' int.
        It has two properties which are IsNull and Value. So by changing my
        object properties to Integers lets me check whether or not they are
        null before using them and properly inserting a NULL into the
        database.

        JF

        Comment

        • James Fisher

          #5
          Re: NULL values

          sullebay@bellso uth.net (bjs10) wrote in message news:<583627d0. 0307310526.4dc1 e857@posting.go ogle.com>...[color=blue]
          > This won't work; you can't set a value type to null.
          >
          > "Mark" <f1fannz@hotmai l.com> wrote in message news:<#dxW7mwVD HA.624@TK2MSFTN GP10.phx.gbl>.. .[color=green]
          > > Hi all
          > > From my understanding of the framework etc. everything is inherited from an
          > > object even system types such as int.
          > >
          > > You can declare a variable as an int with a value of null
          > > eg int personCategoryI D = null;
          > >
          > > The framework will then give it the default value of null for an int which
          > > is 0.
          > >[/color][/color]

          Yes bjs is correct,

          There really is no great solution to this. If I were using VB.NET I
          could use the combination of Nothing and IsNull and not have to change
          much. However, using C# I created a wrapper type for my value types.
          For example, I created an Integer class that wraps the good ole' int.
          It has two properties which are IsNull and Value. So by changing my
          object properties to Integers lets me check whether or not they are
          null before using them and properly inserting a NULL into the
          database.

          JF

          Comment

          Working...