Trying to better understand class design and types

D

Dave

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
 
M

Mike McIntyre

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top