473,411 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Trying to better understand class design and types

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
2 1297
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
51
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
19
by: Mary Pegg | last post by:
There's got to be a better way: if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; if (isset($c)) echo $c."<br>"; but...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.