User Defined Class

C

CoRrRan

I am trying to create a class in Visual Basic 2005 environment to hold
all information closely together. There are two variables I want to use,
and they look like this:
**********************************************************************
clsLayerZ1
|
+-> Headers
| |
| +-> Header1 = Text1 (String)
| +-> Header2 = Text2 (String)
| +-> ....
+-> LC(0)
| |
| +-> ID = LC ID (Integer)
| +-> Entity(0)
| | |
| | +-> ID = Entity ID (Integer)
| | +-> Loads
| | |
| | +-> Load1 = Value1 (Double)
| | +-> Load2 = Value2 (Double)
| | +-> Load3 = Value3 (Double)
| | +-> ....
| +-> Entity(1)
| | |
| | +-> ID = Entity ID (Integer)
| | +-> Loads
| | |
| | +-> Load1 = Value1 (Double)
| | +-> ....
| +-> ....
+-> LC(1)
| |
| +-> ID = LC ID (Integer)
| +-> ....
+-> ....
**********************************************************************


I currently have tried to do it this way:
**********************************************************************
Public Class Layer

Private _iLCs As Integer
Private _iHeaders As Short
Private _iEntities As Long

Sub New(ByVal iLCs As Integer, ByVal iEntities As Long, ByVal
iHeaders As Short)
_iLCs = iLCs
_iEntities = iEntities
_iHeaders = iHeaders
ReDim _LoadCase(_iEntities)
End Sub

Public LoadCase(_iLCs) As LoadCase(_iEntities)
Public Headers(_iHeaders) As String

End Class

Public Class LoadCase

Private _iEntities As Long

Sub New(ByVal iEntities As Long)
_iEntities = iEntities
End Sub

Public LCID As Long
Public Entities(_iEntities) As Entity(iLoadComponents)

End Class

Public Class Entity
Private _iLoadComponents As Short
Sub New(ByVal iLoadComponents As Short)
_iLoadComponents = iLoadComponents
End Sub
Public EntityID As Long
Public LoadsInfo(_iLoadComponents) As Double

End Class
**********************************************************************

Unfortunately, this method is not working for me. I probably have some
fundamental flaws in the code I'm trying to use, but I haven't got any
idea how to do this properly.

Another problem that I have: these variables are to be used in multiple
procedures, but one of these procedures generates the required
dimensions for the various sub-arrays in the class I want to create.

Hopefully someone is able to help me by perhaps providing me with a
decent example of how this is done.

Regards, CoRrRan
 
C

Chris Dunaway

I am trying to create a class in Visual Basic 2005 environment to hold
all information closely together. There are two variables I want to use,
and they look like this:
**********************************************************************
clsLayerZ1
|
+-> Headers
| |
| +-> Header1 = Text1 (String)
| +-> Header2 = Text2 (String)
| +-> ....
+-> LC(0)
| |
| +-> ID = LC ID (Integer)
| +-> Entity(0)
| | |
| | +-> ID = Entity ID (Integer)
| | +-> Loads
| | |
| | +-> Load1 = Value1 (Double)
| | +-> Load2 = Value2 (Double)
| | +-> Load3 = Value3 (Double)
| | +-> ....
| +-> Entity(1)
| | |
| | +-> ID = Entity ID (Integer)
| | +-> Loads
| | |
| | +-> Load1 = Value1 (Double)
| | +-> ....
| +-> ....
+-> LC(1)
| |
| +-> ID = LC ID (Integer)
| +-> ....
+-> ....
**********************************************************************

I currently have tried to do it this way:
**********************************************************************
Public Class Layer

Private _iLCs As Integer
Private _iHeaders As Short
Private _iEntities As Long

Sub New(ByVal iLCs As Integer, ByVal iEntities As Long, ByVal
iHeaders As Short)
_iLCs = iLCs
_iEntities = iEntities
_iHeaders = iHeaders
ReDim _LoadCase(_iEntities)
End Sub

Public LoadCase(_iLCs) As LoadCase(_iEntities)
Public Headers(_iHeaders) As String

End Class

Public Class LoadCase

Private _iEntities As Long

Sub New(ByVal iEntities As Long)
_iEntities = iEntities
End Sub

Public LCID As Long
Public Entities(_iEntities) As Entity(iLoadComponents)

End Class

Public Class Entity
Private _iLoadComponents As Short
Sub New(ByVal iLoadComponents As Short)
_iLoadComponents = iLoadComponents
End Sub
Public EntityID As Long
Public LoadsInfo(_iLoadComponents) As Double

End Class
**********************************************************************

Unfortunately, this method is not working for me. I probably have some
fundamental flaws in the code I'm trying to use, but I haven't got any
idea how to do this properly.

Another problem that I have: these variables are to be used in multiple
procedures, but one of these procedures generates the required
dimensions for the various sub-arrays in the class I want to create.

Hopefully someone is able to help me by perhaps providing me with a
decent example of how this is done.

Regards, CoRrRan

Don't use arrays, instead use a List(Of LoadCase) and List(Of Entity)
respectively (If you dont have VB 2005, then use an ArrayList
instead). Then you don't have to pre-allocate the size. You can use
the .Add method to add entries to the list.

Something like this:

Public Class Layer

Sub New()
_loadCases = New List(Of LoadCase)
_headers = New List(Of String)
End Sub

Private _loadCases As List(Of LoadCase)
Public ReadOnly Property LoadCases As List(Of LoadCase)
Get
Return _loadCases
End Get
End Property

Private _headers As List(Of String)
Public ReadOnly Property Headers As List (Of String)
Get
Return _headers
End Get
End Property

End Class

You can use the class like this:

Public Class Form1 As Form

Private _layer As Layer

Public Sub New()
_layer = New Layer
End Sub

Private Sub Button1_Click(...)
Dim lc As new LoadCase

_layer.LoadCases.Add(lc)
End Sub
End Class

Hope this helps a little.

Chris
 
C

CoRrRan

Chris said:
Don't use arrays, instead use a List(Of LoadCase) and List(Of Entity)
respectively (If you dont have VB 2005, then use an ArrayList
instead). Then you don't have to pre-allocate the size. You can use
the .Add method to add entries to the list.

Something like this:

Public Class Layer

Sub New()
_loadCases = New List(Of LoadCase)
_headers = New List(Of String)
End Sub

Private _loadCases As List(Of LoadCase)
Public ReadOnly Property LoadCases As List(Of LoadCase)
Get
Return _loadCases
End Get
End Property

Private _headers As List(Of String)
Public ReadOnly Property Headers As List (Of String)
Get
Return _headers
End Get
End Property

End Class

You can use the class like this:

Public Class Form1 As Form

Private _layer As Layer

Public Sub New()
_layer = New Layer
End Sub

Private Sub Button1_Click(...)
Dim lc As new LoadCase

_layer.LoadCases.Add(lc)
End Sub
End Class

Hope this helps a little.

Chris

Ah, that looks really promising. Thank you for your response Chris! I'll
let you know whether this results in something that's workable.

CoRrRan
 

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