Structures

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
If I have a structure

Structure ABC
Dim A As Integer
Dim B As Integer
Dim C As String
End Structure

Dim _struct() As Structure


Instead of saying
_struct(0).A = 1
_struct(0).B = 1
_struct(0).C = "Value"

How can I say something like
_struct(0) = (1, 1, "Value") (this syntax gives me error)

Also for this structure how can I lookup the value in C based on some value for A and B. Say I want to lookup the value of _struct.C where _struct.A=1 and _struct.B=1
 
You need to give your structure a method to perform the first operation. In .NET structures can have methods just like classes can. The second operation would require that you test the A and B values of every element of the array until you find one with matching values. You could give the structure another method to help with this. It might look something like this:
VB.NET:
Public Function GetCForMatchingAAndB(ByVal a As Integer, ByVal b As Integer, ByRef c As String) As Boolean
    If Me.A = a AndAlso Me.B = b Then
        c = Me.C
        Return True
    Else
        Return False
    End If
End Function
 
Ok. But how am I going to loop through my structure array inside the structure definition?

I may have my sturcture like this. Do I need to loop for each structure value in the code and call GetErrorMessage ?

Structure StandardErrorMessage
Dim ErrorType As ErrorType
Dim ErrorCode As Integer
Dim ErrorMessage As String

Public Function GetErrorMessage(ByVal ErrorTypeValue As ErrorType, ByVal ErrorCodeValue As Integer) As String
If Me.ErrorType = ErrorTypeValue And Me.ErrorCode = ErrorCodeValue Then
Return Me.ErrorMessage
End If
End Function
End Structure
 
adwaitjoshi, you first question, add a parameterized contructor to the structure:
VB.NET:
Structure ABC
Dim A,B As Integer
Dim C As String
 
   Public Sub New(A As Integer, B As Integer, C as string)
      Me.A = A
      Me.B = B
      Me.C = C
   End Sub
End Structure
Now you can:
dim one as new ABC(1,2,"test")
or
_struct(0) = new ABC(1,2,"test")
 
Back
Top