How to define a "Type"?

  • Thread starter Thread starter Kay
  • Start date Start date
K

Kay

Hi all,

In vb6, I can define a custom type like this:

Private Type uClient
sName As String
sMonday As Double
sMondayHeadCnt As Integer
End Type

Wondering is it possible to do the same thing in .net?

Thanks~~

Kay
 
Use a structure. Or better yet just define a class with properties.

Public Stucture uClient
Dim sName As String
Dim sMonday as Double
Dim sMondayHeadCnt AsInteger

End Structure

Public Class Client

Private mName As String
Private mMonday As Double
Private mMondayHeadCount As Integer

Public Sub New(name As String, monday As Double, mondayHeadCount as
Integer)
mName = name
mMonday = monday
mMondayHeadCount = mondayHeadCount

End Sub

Public Property Name() As String
. . .
End Property

Public Property Monday() As Double
. . .
End Property

Public Property MondayHeadCount() As Integer
. . .
End Property

End Class
 
Thanks a lot Ray ! ! ! !

Kay :D


Ray Cassick (Home) said:
Use a structure. Or better yet just define a class with properties.

Public Stucture uClient
Dim sName As String
Dim sMonday as Double
Dim sMondayHeadCnt AsInteger

End Structure

Public Class Client

Private mName As String
Private mMonday As Double
Private mMondayHeadCount As Integer

Public Sub New(name As String, monday As Double, mondayHeadCount as
Integer)
mName = name
mMonday = monday
mMondayHeadCount = mondayHeadCount

End Sub

Public Property Name() As String
. . .
End Property

Public Property Monday() As Double
. . .
End Property

Public Property MondayHeadCount() As Integer
. . .
End Property

End Class
 
No problem.

Personally I think the class method is cleaner and more OOP so I use it
more. It is a bit more work but when it comes to eventually wanting to do
things like provide methods that act on your type (ie: ToString, Equals,
CopyTo, etc...) you are going to end up going the class route anyway.

That is not to say that you cant build a structure with methods and
properties, you can do that too, but then really what is the difference
between a structure and a class :)

Just go with a class, inherit from System.Object like all the other base
class types do and you are all set.
 
Hi Ray and all....

Just another question about the structure...

If the structure's variable has array like this:

Public Stucture uClient
Dim sName() As String
Dim sMonday() as Double
Dim sMondayHeadCnt() AsInteger
End Structure

And when I try to access the data :
Msgbox (myClient.sName(i))
I got an "Object reference not set to an instance of an object"...

Is it because there're no value assigned in the array? If that's the case
how do I check whether the array is populated or not?

Thanks~~

Kay
 
Hi Ray,

Yeah I agree (espeically it's "a bit more work")... ha ha~

I just post another question in this thread(hope u or other can help me
out), one of the reason I stick with structure is I need an array of each
variables(i.e. Monday - Sunday and with each day's Headcount, the sName is
actually a Campaign name if it confuse you), and the structure is just a
temp "variable" used to populate a listview, but as you said, I may end up
with OOP anyway~

Kay
 
You can check the length of the array (ie: myClient.sName.Length) to see if
it is grater than 0.

....but I really think you need to consider refactoring this into a class and
access your data through properties.
 
Back
Top