First I am sorry if I am creating a thread in the wrong category as I am not sure which is the best suitable place for this question:

Please provide me suggestions or any pointers.

I want to create a user defined data structure, which will be a 2 dimensional array such that each individual square of this 2-dimensional array will also store the score and the grades. ( Iam using VB.NET)

More details:

for example: There is an 2 dimensional array with 3 columns and 2 rows, I want to fill each of the

(0,0);(0,1);(0,2) with their own score (Integer type) and grades (String Type).

And this is same for the rest of the squares i.e. (1,0);(1,1);(1,2) and (2,0);(2,1);(2,2)

After creating such custom data structure: "CustomArray", I should be able to create the instance of this custom data structure (which will be a class) and code something like this:

Dim objCustomArray as  CustomArray

objCustomArray = New CustomArray

for i as Integer =0 to 3

 objCustomArray(0,i).SetScore(1)

objCustomArray(0,i).SetGrade("Good")

Next



for i as Integer =0 to 3

 Dim myVal as Integer = objCustomArray(0,i).GetScore()

Dim myGrade as String = objCustomArray(0,i).gettGrade()

Next

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

>with their own score (Integer type) and grades (String Type).

Do not use object type. Think about a class having score and grade fields,

Public Class TwoD
    Private _score As Integer
    Private _grade As String

    Public Sub New()
        _grade = String.Empty
    End Sub
    Public Sub New(ByVal _score As Integer, ByVal _grade As String)
        Me._grade = _grade
        Me._score = _score
    End Sub
    Public Property Score() As Integer
        Get
            Return _score
        End Get
        Set(ByVal value As Integer)
            _score = value
        End Set
    End Property
    Public Property Grade() As String
        Get
            Return _grade
        End Get
        Set(ByVal value As String)
            _grade = value
        End Set
    End Property
End Class

Here is how to declare two dim array of TwoD class,

'Three rows and 2 columns

'This statement create 6 (six) object variable of class TwoD
Dim a(2, 1) As TwoD

Now, it is time to create 6 objects. Take a look,

a(0,0)=new TwoD(1,"A")
a(0,1)=new TwoD(1,"A")
a(1,0)=new TwoD(0,"C")
a(1,1)=new TwoD(0,"C")
a(2,0)=new TwoD(0,"B")
a(2,1)=new TwoD(1,"C")

OR, you can following methods to create objects.

For i As Integer = 0 To a.GetUpperBound(0)
   For j As Integer = 0 To a.GetUpperBound(1)
         a(i, j) = New TwoD()
   Next
Next

a(0,0).Score=1
a(0,1).Grade="A"
.....

To print an array of object,

For i As Integer = 0 To a.GetUpperBound(0)
  For j As Integer = 0 To a.GetUpperBound(1)
    Console.Write(" (" & a(i, j).Score & "," & a(i, j).Grade & ")")
  Next
  Console.WriteLine() 'New Line
Next

Edited 6 Years Ago by __avd: n/a

Thanks for your reply.

I am trying to understand your code, so you are saying that the class TwoD will be like two dimensional array and can store score n grades and it can be manipulated like usual 2 dimensional array?

I did not completetly follow the step where you create objects using For Loop.

I tried to do this way but I get the Null exception error :

'Declare 2 dim array with 2 rows and 3 columns
Dim a(2,3) as TwoD

For i As Integer = 0 To 2
   For j As Integer = 0 To 3
         a(i, j) = New TwoD()
   Next
Next

'Get Null exception error at this code line
'Is this correct way to fill my custom 2 dim array?
For j as Integer = 0 to 3
 a(0,j).Score =0
 a(0,j).Grade ="none"
Next

For i as Integer = 0 to 2
 a(i,0).Score =0
 a(i,0).Grade ="none"
Next

>with their own score (Integer type) and grades (String Type).

Do not use object type. Think about a class having score and grade fields,

Public Class TwoD
    Private _score As Integer
    Private _grade As String

    Public Sub New()
        _grade = String.Empty
    End Sub
    Public Sub New(ByVal _score As Integer, ByVal _grade As String)
        Me._grade = _grade
        Me._score = _score
    End Sub
    Public Property Score() As Integer
        Get
            Return _score
        End Get
        Set(ByVal value As Integer)
            _score = value
        End Set
    End Property
    Public Property Grade() As String
        Get
            Return _grade
        End Get
        Set(ByVal value As String)
            _grade = value
        End Set
    End Property
End Class

Here is how to declare two dim array of TwoD class,

'Three rows and 2 columns

'This statement create 6 (six) object variable of class TwoD
Dim a(2, 1) As TwoD

Now, it is time to create 6 objects. Take a look,

a(0,0)=new TwoD(1,"A")
a(0,1)=new TwoD(1,"A")
a(1,0)=new TwoD(0,"C")
a(1,1)=new TwoD(0,"C")
a(2,0)=new TwoD(0,"B")
a(2,1)=new TwoD(1,"C")

OR, you can following methods to create objects.

For i As Integer = 0 To a.GetUpperBound(0)
   For j As Integer = 0 To a.GetUpperBound(1)
         a(i, j) = New TwoD()
   Next
Next

a(0,0).Score=1
a(0,1).Grade="A"
.....

To print an array of object,

For i As Integer = 0 To a.GetUpperBound(0)
  For j As Integer = 0 To a.GetUpperBound(1)
    Console.Write(" (" & a(i, j).Score & "," & a(i, j).Grade & ")")
  Next
  Console.WriteLine() 'New Line
Next

Edited 6 Years Ago by automata: n/a

This article has been dead for over six months. Start a new discussion instead.