Help re Generics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    Help re Generics

    Hi

    Further to my previous question ...

    Is it possible to set up a declaration using generics which permits the
    specification of any numeric value rather than a formal division between
    integers and floats

    I want to set up a generic definition for Adding 2 numbers

    Public Function Add(Byval x as Integer, Byval y as Integer) as Integer
    Public Function Add(Byval x as Double, Byval y as Double) as Double

    Really I think I'm wanting something like

    Public Function Add(Byval x as INumeric, Byval y as INumeric) as INumeric

    but I'm not sure how to implement this without wrapping every number
    into some sort of class?

    BTW I don't think I want

    Public Function Add(Of T)(Byval x as T, Byval y as T) as T

    because you need to know up-front the type of number

    e.g.

    Dim a as Integer =Add(of Integer)(1 + 1)
    Dim b as Double = Add(of Double)(1.1 + 1.1)

    Thx

    S
  • Herfried K. Wagner [MVP]

    #2
    Re: Help re Generics

    "Simon Woods" <simonjwoods@ho tmail.comschrie b:
    Is it possible to set up a declaration using generics which permits the
    specification of any numeric value rather than a formal division between
    integers and floats
    >
    I want to set up a generic definition for Adding 2 numbers
    >
    Public Function Add(Byval x as Integer, Byval y as Integer) as Integer
    Public Function Add(Byval x as Double, Byval y as Double) as Double
    >
    Really I think I'm wanting something like
    >
    Public Function Add(Byval x as INumeric, Byval y as INumeric) as INumeric
    >
    but I'm not sure how to implement this without wrapping every number into
    some sort of class?
    Unfortunately there is no such constraint defined in the .NET Framework.

    Numerics and generics - @ Head
    <URL:http://msmvps.com/blogs/bill/archive/2007/12/22/numerics-and-generics.aspx>

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Simon Woods

      #3
      Re: Help re Generics

      Herfried K. Wagner [MVP] wrote:
      "Simon Woods" <simonjwoods@ho tmail.comschrie b:
      >Is it possible to set up a declaration using generics which permits
      >the specification of any numeric value rather than a formal division
      >between integers and floats
      >>
      >I want to set up a generic definition for Adding 2 numbers
      >>
      >Public Function Add(Byval x as Integer, Byval y as Integer) as Integer
      >Public Function Add(Byval x as Double, Byval y as Double) as Double
      >>
      >Really I think I'm wanting something like
      >>
      >Public Function Add(Byval x as INumeric, Byval y as INumeric) as INumeric
      >>
      >but I'm not sure how to implement this without wrapping every number
      >into some sort of class?
      >
      Unfortunately there is no such constraint defined in the .NET Framework.
      >
      Numerics and generics - @ Head
      <URL:http://msmvps.com/blogs/bill/archive/2007/12/22/numerics-and-generics.aspx>
      OK ... thanks vm, Herfried

      Comment

      • Simon Woods

        #4
        Re: Help re Generics

        Herfried K. Wagner [MVP] wrote:
        "Simon Woods" <simonjwoods@ho tmail.comschrie b:
        >Is it possible to set up a declaration using generics which permits
        >the specification of any numeric value rather than a formal division
        >between integers and floats
        >>
        >I want to set up a generic definition for Adding 2 numbers
        >>
        >Public Function Add(Byval x as Integer, Byval y as Integer) as Integer
        >Public Function Add(Byval x as Double, Byval y as Double) as Double
        >>
        >Really I think I'm wanting something like
        >>
        >Public Function Add(Byval x as INumeric, Byval y as INumeric) as INumeric
        >>
        >but I'm not sure how to implement this without wrapping every number
        >into some sort of class?
        >
        Unfortunately there is no such constraint defined in the .NET Framework.
        >
        Numerics and generics - @ Head
        <URL:http://msmvps.com/blogs/bill/archive/2007/12/22/numerics-and-generics.aspx>
        Herfried

        wrt the above link, you don't happen to know which Brian Backman
        article/presentation/whatever Bill was referring to, do you?

        Thx

        Simon

        Comment

        • Simon Woods

          #5
          Re: Help re Generics

          Simon Woods wrote:
          Herfried K. Wagner [MVP] wrote:
          >"Simon Woods" <simonjwoods@ho tmail.comschrie b:
          >>Is it possible to set up a declaration using generics which permits
          >>the specification of any numeric value rather than a formal division
          >>between integers and floats
          >>>
          >>I want to set up a generic definition for Adding 2 numbers
          >>>
          >>Public Function Add(Byval x as Integer, Byval y as Integer) as Integer
          >>Public Function Add(Byval x as Double, Byval y as Double) as Double
          >>>
          >>Really I think I'm wanting something like
          >>>
          >>Public Function Add(Byval x as INumeric, Byval y as INumeric) as
          >>INumeric
          >>>
          >>but I'm not sure how to implement this without wrapping every number
          >>into some sort of class?
          >>
          >Unfortunatel y there is no such constraint defined in the .NET Framework.
          >>
          >Numerics and generics - @ Head
          ><URL:http://msmvps.com/blogs/bill/archive/2007/12/22/numerics-and-generics.aspx>
          >
          >
          Herfried
          >
          wrt the above link, you don't happen to know which Brian Backman
          article/presentation/whatever Bill was referring to, do you?
          sorry --- that should be "Brian Beckman"

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Help re Generics

            "Simon Woods" <simonjwoods@ho tmail.comschrie b:
            >>Numerics and generics - @ Head
            >><URL:http://msmvps.com/blogs/bill/archive/2007/12/22/numerics-and-generics.aspx>
            >>
            >wrt the above link, you don't happen to know which Brian Backman
            >article/presentation/whatever Bill was referring to, do you?
            >
            sorry --- that should be "Brian Beckman"
            Maybe he is referring to this video, which I haven't watched, so I am not
            sure:

            Microsoft Videos: Brian Beckman Does Higher Algebra with Visual Basic
            <URL:http://preview.microso ft.com/video/videoDetails.as px?video=c41fa2 cb-7fc0-4b42-be45-9d551cb68bca>

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            Working...