User Defined Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CoRrRan

    #1

    User Defined Class

    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(_iEnt ities)
    End Sub

    Public LoadCase(_iLCs) As LoadCase(_iEnti ties)
    Public Headers(_iHeade rs) 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(_iEnti ties) As Entity(iLoadCom ponents)

    End Class

    Public Class Entity
    Private _iLoadComponent s As Short
    Sub New(ByVal iLoadComponents As Short)
    _iLoadComponent s = iLoadComponents
    End Sub
    Public EntityID As Long
    Public LoadsInfo(_iLoa dComponents) 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
  • Chris Dunaway

    #2
    Re: User Defined Class

    On Jun 6, 9:34 am, CoRrRan <CoRrRan~NOSPAM ~@~NOSPAM~gmail .comwrote:
    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(_iEnt ities)
    End Sub
    >
    Public LoadCase(_iLCs) As LoadCase(_iEnti ties)
    Public Headers(_iHeade rs) 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(_iEnti ties) As Entity(iLoadCom ponents)
    >
    End Class
    >
    Public Class Entity
    Private _iLoadComponent s As Short
    Sub New(ByVal iLoadComponents As Short)
    _iLoadComponent s = iLoadComponents
    End Sub
    Public EntityID As Long
    Public LoadsInfo(_iLoa dComponents) 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.LoadCase s.Add(lc)
    End Sub
    End Class

    Hope this helps a little.

    Chris

    Comment

    • CoRrRan

      #3
      Re: User Defined Class

      Chris Dunaway wrote:
      On Jun 6, 9:34 am, CoRrRan <CoRrRan~NOSPAM ~@~NOSPAM~gmail .comwrote:
      >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(_iEnt ities)
      > End Sub
      >>
      > Public LoadCase(_iLCs) As LoadCase(_iEnti ties)
      > Public Headers(_iHeade rs) 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(_iEnti ties) As Entity(iLoadCom ponents)
      >>
      >End Class
      >>
      >Public Class Entity
      > Private _iLoadComponent s As Short
      > Sub New(ByVal iLoadComponents As Short)
      > _iLoadComponent s = iLoadComponents
      > End Sub
      > Public EntityID As Long
      > Public LoadsInfo(_iLoa dComponents) As Double
      >>
      >End Class
      >************** *************** *************** *************** ***********
      >>
      >Unfortunatel y, 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.LoadCase s.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

      Comment

      Working...