Help re Generics

  • Thread starter Thread starter Simon Woods
  • Start date Start date
S

Simon Woods

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
 
Simon Woods said:
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>
 
Simon said:
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"
 
Back
Top