By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Trying to better understand class design and types

P: n/a
I know in C++ you can create class templates so the data type can be defined
during the declaration of an instance of a class. I was trying to do
something like that for a VB.NET class cut am not really sure where to
start.

Here is an example for a simple DataArray class definition that sets up an
array used for numerical data and adds some additional functionality to the
array. The overloaded new methods work okay, although I'm not sure if it's
the right way to go about doing this, but for the other methods do I have to
write an overloaded method for each data type, or can I set these up to
identify the type of array that was created for this instance of the class?

Public Class DataArray
Public Data As Array
Public Sub New(ByVal dt As Array)
Data = dt
End Sub

Public Sub New(ByVal Size As Integer, ByVal DataType As Type)
Data = Array.CreateInstance(DataType, Size)
End Sub

Public Function Max() As Double
' find maximum value of the area
Dim sortedData As Array
sortedData = Array.CreateInstance(GetType(Double),
Data.GetUpperBound(0) + 1)
Data.CopyTo(sortedData, 0)
Array.Sort(sortedData)
Return sortedData(sortedData.GetUpperBound(0))
End Function
End Class
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In Visual Studio 2005 you will be able to use generics to create a template
for a class or method where you can pass a type as an argument to the
class/method at runtime.

For now, you can emulate this future behavior to some extent by adding
overloaded methods to the class you have started.

--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Dave" <dave@4_rem_ove_scotts.com> wrote in message
news:e8****************@TK2MSFTNGP15.phx.gbl...
I know in C++ you can create class templates so the data type can be
defined during the declaration of an instance of a class. I was trying to
do something like that for a VB.NET class cut am not really sure where to
start.

Here is an example for a simple DataArray class definition that sets up an
array used for numerical data and adds some additional functionality to
the array. The overloaded new methods work okay, although I'm not sure if
it's the right way to go about doing this, but for the other methods do I
have to write an overloaded method for each data type, or can I set these
up to identify the type of array that was created for this instance of the
class?

Public Class DataArray
Public Data As Array
Public Sub New(ByVal dt As Array)
Data = dt
End Sub

Public Sub New(ByVal Size As Integer, ByVal DataType As Type)
Data = Array.CreateInstance(DataType, Size)
End Sub

Public Function Max() As Double
' find maximum value of the area
Dim sortedData As Array
sortedData = Array.CreateInstance(GetType(Double),
Data.GetUpperBound(0) + 1)
Data.CopyTo(sortedData, 0)
Array.Sort(sortedData)
Return sortedData(sortedData.GetUpperBound(0))
End Function
End Class

Nov 21 '05 #2

P: n/a
In Visual Studio 2005 you will be able to use generics to create a template
for a class or method where you can pass a type as an argument to the
class/method at runtime.

For now, you can emulate this future behavior to some extent by adding
overloaded methods to the class you have started.

--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Dave" <dave@4_rem_ove_scotts.com> wrote in message
news:e8****************@TK2MSFTNGP15.phx.gbl...
I know in C++ you can create class templates so the data type can be
defined during the declaration of an instance of a class. I was trying to
do something like that for a VB.NET class cut am not really sure where to
start.

Here is an example for a simple DataArray class definition that sets up an
array used for numerical data and adds some additional functionality to
the array. The overloaded new methods work okay, although I'm not sure if
it's the right way to go about doing this, but for the other methods do I
have to write an overloaded method for each data type, or can I set these
up to identify the type of array that was created for this instance of the
class?

Public Class DataArray
Public Data As Array
Public Sub New(ByVal dt As Array)
Data = dt
End Sub

Public Sub New(ByVal Size As Integer, ByVal DataType As Type)
Data = Array.CreateInstance(DataType, Size)
End Sub

Public Function Max() As Double
' find maximum value of the area
Dim sortedData As Array
sortedData = Array.CreateInstance(GetType(Double),
Data.GetUpperBound(0) + 1)
Data.CopyTo(sortedData, 0)
Array.Sort(sortedData)
Return sortedData(sortedData.GetUpperBound(0))
End Function
End Class

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.