Using Structures in Class Module

DaveP

Member
Joined
Jan 24, 2008
Messages
12
Programming Experience
1-3
I am trying to create a dll which will do a lot of complex calculations so first I need to create the exposed variables which will recieve data.

IF I create a structure which groups variables logically ie.

Public Structure DataRange
Public Min As Integer
Public Max As Integer
Public Val As Integer
End Structure

Public Class MyCalcs
Public MyValues As DataRange
End Class

I can expose MyValues but I also expose DataRange as well.
If on the other hand I do not make the structure public I get an error telling me that 'MyValues cannot expose DataRange through class MyCalcs'

How can I expose Myvalues.min without Datarange being exposed
 
If on the other hand I do not make the structure public I get an error telling me that 'MyValues cannot expose DataRange through class MyCalcs'

How can I expose Myvalues.min without Datarange being exposed
You have to add a 'min' property to the class.
 
The structure has a public 'min' field of type integer, but does the class have public 'min' field of type integer? No, it has only has a public 'MyValues' field of type DataRange. If you want to hide the DataRange type you have to declare the DataRange structure private and declare the 'MyValues' field of 'MyCalcs' class private and instead expose the fields/properties you like in 'MyCalcs' class.
 
Like so:

Public Structure DataRange
Public Min As Integer
Public Max As Integer
Public Val As Integer
End Structure

Public Class MyCalcs
Private MyValues As DataRange

Public Property Min() As Integer
Get
Return MyValues.Min
End Get

Public Property Max() As Integer
Get
Return MyValues.Max
End Get

Public Property Val() As Integer
Get
Return MyValues.Val
End Get

End Class
 
Why not remove the structure altogether:
VB.NET:
Public Class MyCalc
  Private m_MinInteger As Integer
  Private m_MaxInteger As Integer
  Private m_ValInteger As Integer

  Public OverLoads Sub New
    m_MinInteger = 0I
    m_MaxInteger = 0I
    m_ValInteger = 0I
  End Sub

  Public OverLoads Sub New (ByVal Min As Integer, ByVal Max As Integer)
    m_MinInteger = Min
    m_MaxInteger = Max
    m_ValInteger = 0I
  End Sub

  Public Property Min() As Integer
    Get
      Return m_MinInteger
    End Get
    Set (Value As Integer)
      m_MinInteger = Value
    End Set
  End Property

  Public Property Max() As Integer
    Get
      Return m_MaxInteger
    End Get
    Set (Value As Integer)
      m_MaxInteger = Value
    End Set
  End Property

  Public Property Val() As Integer
    Get
      Return m_ValInteger
    End Get
    Set (Value As Integer)
      m_ValInteger = Value
    End Set
  End Property
End Class

You can of course add stuff more easily later on this way too and with the Set portion of the property you can control the data coming in easily too
 
Back
Top