Eric Snyder
Eric Snyder

Reputation: 1934

How to create parameters that accept any numeric type

In vb.net when creating methods and properties I am creating methods using decimal parameters. There are other numeric types like short, long, double, etc.

Is there a simple way to create parameters that cover all numeric types in a single property/method without creating an overloaded property/method for each numeric type?

Upvotes: 0

Views: 101

Answers (1)

You can create generic functions/methods:

Public Function MyFunction(Of T As IConvertible)(value As T) As T
    'Do something...
End Function

Public Sub MySub(Of T As IConvertible)(value As T)
    'Do something...
End Sub

The following types implements the IConvertible interface:

  • System.Boolean
  • System.Byte
  • System.Char
  • System.DateTime
  • System.DBNull
  • System.Decimal
  • System.Double
  • System.Enum
  • System.Int16
  • System.Int32
  • System.Int64
  • System.SByte
  • System.Single
  • System.String
  • System.UInt16
  • System.UInt32
  • System.UInt64

Upvotes: 1

Related Questions