overloading with ints

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • james b

    #1

    overloading with ints

    Hi all,
    I'm trying to do something with method overloading and I can't seem to
    get it to work

    my code is along the lines of
    public int method1(int a, int b, int c){
    //method body
    }
    public int method1(int a, int b){
    method1(a,b,nul l}
    public int method1(int a){
    method1(a,null, null)
    }

    I understand that int is a value type and so can't be null, but all
    the examples i find using value types for overloading pass 0, which is
    no good for me as this is a meaningful value for the parameter, i want
    to pass something that says the parameter is empty. I have tried using
    Int32 and this complains that it can't be null too.

    I have considered creating my own object wrapping int just so i can
    pass null, but that seems absurd. Is there an object (equivalent to
    Java's Integer) that i have missed, or are you supposed to do
    overloading in some different way in this situation?

    many thanks

    James
  • Bob Grommes

    #2
    Re: overloading with ints

    Value types simply can't be null. Your choices would be:

    1) Use a "magic number" to signifiy empty, such as Int32.MinValue.

    2) Make the 2nd & 3rd arguments object, and unbox the int if not null

    3) Use SqlInt32 instead of int. Then you can test for, say, b.Value ==
    b.Null. This is similar to what you were thinking of doing -- wrapping the
    integer in a class. It's already been done for you.

    --Bob

    "james b" <j@mesbridger.c om> wrote in message
    news:bece2df7.0 308040752.6d792 cc5@posting.goo gle.com...[color=blue]
    > Hi all,
    > I'm trying to do something with method overloading and I can't seem to
    > get it to work
    >
    > my code is along the lines of
    > public int method1(int a, int b, int c){
    > //method body
    > }
    > public int method1(int a, int b){
    > method1(a,b,nul l}
    > public int method1(int a){
    > method1(a,null, null)
    > }
    >
    > I understand that int is a value type and so can't be null, but all
    > the examples i find using value types for overloading pass 0, which is
    > no good for me as this is a meaningful value for the parameter, i want
    > to pass something that says the parameter is empty. I have tried using
    > Int32 and this complains that it can't be null too.
    >
    > I have considered creating my own object wrapping int just so i can
    > pass null, but that seems absurd. Is there an object (equivalent to
    > Java's Integer) that i have missed, or are you supposed to do
    > overloading in some different way in this situation?
    >
    > many thanks
    >
    > James[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: overloading with ints

      James,
      I would consider a variation of Bob Grommes's second item:[color=blue][color=green]
      >> 2) Make the 2nd & 3rd arguments object, and unbox the int if not null[/color][/color]

      Have a fourth helper function that the three overloaded functions call. This
      helper function has extra parameters to identify whether b & c are null or
      not.

      public int method1(int a, int b, int c)
      {
      method1(a, b, false, c, false);
      }
      public int method1(int a, int b)
      {
      method1(a,b,fal se, 0, true);
      }
      public int method1(int a)
      {
      method1(a, 0, true, 0, true);
      }

      private int method1(int a, int b, bool bNull, int c, bool cNull)
      {[color=blue]
      > //method body[/color]
      }

      The helper function is private as it is not intended to be called outside
      the function. This avoids boxing & unboxing of the parameters...

      Hope this helps
      Jay

      "james b" <j@mesbridger.c om> wrote in message
      news:bece2df7.0 308040752.6d792 cc5@posting.goo gle.com...[color=blue]
      > Hi all,
      > I'm trying to do something with method overloading and I can't seem to
      > get it to work
      >
      > my code is along the lines of
      > public int method1(int a, int b, int c){
      > //method body
      > }
      > public int method1(int a, int b){
      > method1(a,b,nul l}
      > public int method1(int a){
      > method1(a,null, null)
      > }
      >
      > I understand that int is a value type and so can't be null, but all
      > the examples i find using value types for overloading pass 0, which is
      > no good for me as this is a meaningful value for the parameter, i want
      > to pass something that says the parameter is empty. I have tried using
      > Int32 and this complains that it can't be null too.
      >
      > I have considered creating my own object wrapping int just so i can
      > pass null, but that seems absurd. Is there an object (equivalent to
      > Java's Integer) that i have missed, or are you supposed to do
      > overloading in some different way in this situation?
      >
      > many thanks
      >
      > James[/color]


      Comment

      • codewriter

        #4
        Re: overloading with ints

        Hi James,

        The way method or function overloading works is that you have to define all
        the functions with different signatures, then the compiler will figure out
        which one to use automatically.

        In your case you should do this:
        public int method1(int a, int b, int c){
        //method body
        }
        public int method1(int a, int b){
        //method body
        }
        public int method1(int a){
        //method body
        }

        Then, from your code you can use these methods like:
        methond1(10, 12, 13);
        or
        method1(10, 12)
        or
        method1(10);
        The complier will figure out which particular function to call based on the
        signature.



        "james b" <j@mesbridger.c om> wrote in message
        news:bece2df7.0 308040752.6d792 cc5@posting.goo gle.com...[color=blue]
        > Hi all,
        > I'm trying to do something with method overloading and I can't seem to
        > get it to work
        >
        > my code is along the lines of
        > public int method1(int a, int b, int c){
        > //method body
        > }
        > public int method1(int a, int b){
        > method1(a,b,nul l}
        > public int method1(int a){
        > method1(a,null, null)
        > }
        >
        > I understand that int is a value type and so can't be null, but all
        > the examples i find using value types for overloading pass 0, which is
        > no good for me as this is a meaningful value for the parameter, i want
        > to pass something that says the parameter is empty. I have tried using
        > Int32 and this complains that it can't be null too.
        >
        > I have considered creating my own object wrapping int just so i can
        > pass null, but that seems absurd. Is there an object (equivalent to
        > Java's Integer) that i have missed, or are you supposed to do
        > overloading in some different way in this situation?
        >
        > many thanks
        >
        > James[/color]


        Comment

        • james b

          #5
          Re: overloading with ints

          Hi Bob,
          Thanks for your suggestions
          I've always had a problem with the "magic number" solution, always
          seemed like such a hack :)
          Using Objects and then unboxing is something i had considered - but i
          felt i would rather wrap the int in another class to retain some
          notion of the correct type - I think I will probably end up going this
          way.
          I'm pretty sure i tried using SqlInt32 and Int32 and it still
          complained about the nulls, although i'm at home now so i can't verify
          the SqlInt32.

          James

          "Bob Grommes" <bob@bobgrommes .com> wrote in message news:<eXJdRGqWD HA.1384@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
          > Value types simply can't be null. Your choices would be:
          >
          > 1) Use a "magic number" to signifiy empty, such as Int32.MinValue.
          >
          > 2) Make the 2nd & 3rd arguments object, and unbox the int if not null
          >
          > 3) Use SqlInt32 instead of int. Then you can test for, say, b.Value ==
          > b.Null. This is similar to what you were thinking of doing -- wrapping the
          > integer in a class. It's already been done for you.
          >
          > --Bob
          >
          > "james b" <j@mesbridger.c om> wrote in message
          > news:bece2df7.0 308040752.6d792 cc5@posting.goo gle.com...[color=green]
          > > Hi all,
          > > I'm trying to do something with method overloading and I can't seem to
          > > get it to work
          > >
          > > my code is along the lines of
          > > public int method1(int a, int b, int c){
          > > //method body
          > > }
          > > public int method1(int a, int b){
          > > method1(a,b,nul l}
          > > public int method1(int a){
          > > method1(a,null, null)
          > > }
          > >
          > > I understand that int is a value type and so can't be null, but all
          > > the examples i find using value types for overloading pass 0, which is
          > > no good for me as this is a meaningful value for the parameter, i want
          > > to pass something that says the parameter is empty. I have tried using
          > > Int32 and this complains that it can't be null too.
          > >
          > > I have considered creating my own object wrapping int just so i can
          > > pass null, but that seems absurd. Is there an object (equivalent to
          > > Java's Integer) that i have missed, or are you supposed to do
          > > overloading in some different way in this situation?
          > >
          > > many thanks
          > >
          > > James[/color][/color]

          Comment

          • james b

            #6
            Re: overloading with ints

            Now that is creative !!!
            I'll end up with some very messy signatures inside the class, as I'm
            actually using several more parameters than i put in my examples, but
            my external interface to the class will look the way i want it.
            I think this is the approach i will take, i just wish i didn't have to
            jump through these hoops to achieve something that should be so
            simple...

            many thanks

            James

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message news:<OdxpRRqWD HA.2476@tk2msft ngp13.phx.gbl>. ..[color=blue]
            > James,
            > I would consider a variation of Bob Grommes's second item:[color=green][color=darkred]
            > >> 2) Make the 2nd & 3rd arguments object, and unbox the int if not null[/color][/color]
            >
            > Have a fourth helper function that the three overloaded functions call. This
            > helper function has extra parameters to identify whether b & c are null or
            > not.
            >
            > public int method1(int a, int b, int c)
            > {
            > method1(a, b, false, c, false);
            > }
            > public int method1(int a, int b)
            > {
            > method1(a,b,fal se, 0, true);
            > }
            > public int method1(int a)
            > {
            > method1(a, 0, true, 0, true);
            > }
            >
            > private int method1(int a, int b, bool bNull, int c, bool cNull)
            > {[color=green]
            > > //method body[/color]
            > }
            >
            > The helper function is private as it is not intended to be called outside
            > the function. This avoids boxing & unboxing of the parameters...
            >
            > Hope this helps
            > Jay
            >
            > "james b" <j@mesbridger.c om> wrote in message
            > news:bece2df7.0 308040752.6d792 cc5@posting.goo gle.com...[color=green]
            > > Hi all,
            > > I'm trying to do something with method overloading and I can't seem to
            > > get it to work
            > >
            > > my code is along the lines of
            > > public int method1(int a, int b, int c){
            > > //method body
            > > }
            > > public int method1(int a, int b){
            > > method1(a,b,nul l}
            > > public int method1(int a){
            > > method1(a,null, null)
            > > }
            > >
            > > I understand that int is a value type and so can't be null, but all
            > > the examples i find using value types for overloading pass 0, which is
            > > no good for me as this is a meaningful value for the parameter, i want
            > > to pass something that says the parameter is empty. I have tried using
            > > Int32 and this complains that it can't be null too.
            > >
            > > I have considered creating my own object wrapping int just so i can
            > > pass null, but that seems absurd. Is there an object (equivalent to
            > > Java's Integer) that i have missed, or are you supposed to do
            > > overloading in some different way in this situation?
            > >
            > > many thanks
            > >
            > > James[/color][/color]

            Comment

            • Jon Skeet

              #7
              Re: overloading with ints

              james b <j@mesbridger.c om> wrote:[color=blue]
              > Now that is creative !!!
              > I'll end up with some very messy signatures inside the class, as I'm
              > actually using several more parameters than i put in my examples, but
              > my external interface to the class will look the way i want it.
              > I think this is the approach i will take, i just wish i didn't have to
              > jump through these hoops to achieve something that should be so
              > simple...[/color]

              The thing is, it's *not* simple. If you really need every value
              available in int to be a valid value, but you also need "this parameter
              is valid" information, then you basically need more information than is
              available in an int, so you've got to have *something* extra - whether
              that's a reference or an extra boolean, you've got to have that
              information somewhere.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...