hiiiiii
i have some problem for developing the code for addtion of 3x3 matrix class
the code which i have develop is given below.
but this class is not executed in vb.net code.
dont know whats getting wrong in it as m nt getting the required result.

please help me in correcting the code as m new in vb.net...

Public Class Matrix3x3

    Private m_values(8) As Integer

    Sub New()
        ' initialise array:
        m_values = New Integer(8) {}
    End Sub
    ' expose a member of the array:
    Public Property Item(ByVal index As Integer) As Integer
        Get
            If index < 0 Or index > 8 Then Throw New ArgumentOutOfRangeException
            Return m_values(index)
        End Get
        Set(ByVal value As Integer)
            If index < 0 Or index > 8 Then Throw New ArgumentOutOfRangeException
            m_values(index) = value
        End Set
    End Property
    ' allow get/set by row/column
    Public Property Item(ByVal row As Integer, ByVal column As Integer) As Integer
        Get
            If row < 0 Or row > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            If column < 0 Or column > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            Dim index As Integer = (row * 3) + column
            Return m_values(index)
        End Get
        Set(ByVal value As Integer)
            If row < 0 Or row > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            If column < 0 Or column > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            Dim index As Integer = (row * 3) + column
            m_values(index) = value
        End Set
    End Property
    Public Function Add(ByVal matrixToAdd As Matrix3x3) As Matrix3x3
        ' declare a matrix to hold the result
        Dim result As New Matrix3x3
        ' loop through arrays and the members of the two matrixes, store in the result matrix
        For i As Integer = 0 To 8
            result.Item(i) = m_values(i) + matrixToAdd.Item(i)
        Next
        Return result
    End Function
    Public Overrides Function ToString() As String
        Dim msg As New System.Text.StringBuilder
        ' find the longest value when represented as string
        Dim longest As Integer = -1
        For i As Integer = 0 To 8
            If m_values(i).ToString.Length > longest Then longest = m_values(i).ToString.Length
        Next
        ' create the string
        longest += 1
        Dim width As String = longest.ToString
        For y As Integer = 0 To 2
            For x As Integer = 0 To 2
                msg.Append(String.Format("{0," & width & "}", m_values((y * 3) + x)))
            Next
            msg.Append(Environment.NewLine)
        Next
        Return msg.ToString
    End Function
End Class

this is vb.net code

Public Class Form1
    Dim a1 As New Matrix3x3
    Dim b1 As New Matrix3x3
    Dim c1 As New Matrix3x3

    Dim rand As New Random

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim m As New Matrix3x3
        Dim n As New Matrix3x3
        For row As Integer = 0 To 2
            For col As Integer = 0 To 2
                m.Item(row, col) = ((row * 3) + col) = Val(TextBox2.Text)
                n.Item(row, col) = rand.Next(0, 2000) = Val(TextBox3.Text)
            Next
        Next
a1=val(textbox1.text)
b1=val(textbox2.text)

c1=c1.add(a1,b1)

textbox3.text=c1.tostring
    End Sub

I want the result in textboxes where the inputs are given in textboxex manually...
here m,n are the rows and columns of matrix...

please help
thanx in advance

Hi,
I have added a converter from string to Matrix to your Matrix class. I have also modified your code a bit so that it can run. You Matrix.ToString method returns a multi-line string, so make sure that the textboxes are multiline and big enough to see three lines.

The string converter does minimal error checking, so you may want to improve on that shown. It allows you to separate the matrix values using NewLine, comma, or space characters.

Public Class Form1
    Dim a1 As New Matrix3x3
    Dim b1 As New Matrix3x3
    Dim c1 As New Matrix3x3

    Dim rand As New Random

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim m As New Matrix3x3
      Dim n As New Matrix3x3
      For row As Integer = 0 To 2
         For col As Integer = 0 To 2
             m.Item(row, col) = ((row * 3) + col)
             n.Item(row, col) = rand.Next(0, 2000)
         Next
      Next

      a1 = "1,1,1,1,1,1,1,1,1"
      TextBox1.Text = a1.ToString

      b1 = "2,2,2,2,2,2,2,2,2"
      TextBox2.Text = b1.ToString

      c1 = a1.Add(b1)

      TextBox3.Text = c1.ToString
    End Sub

End Class


Public Class Matrix3x3

    Private m_values(8) As Integer

    Sub New()
        ' initialise array:
        m_values = New Integer(8) {}
    End Sub
    ' expose a member of the array:
    Public Property Item(ByVal index As Integer) As Integer
        Get
            If index < 0 Or index > 8 Then Throw New ArgumentOutOfRangeException
            Return m_values(index)
        End Get
        Set(ByVal value As Integer)
            If index < 0 Or index > 8 Then Throw New ArgumentOutOfRangeException
            m_values(index) = value
        End Set
    End Property
    ' allow get/set by row/column
    Public Property Item(ByVal row As Integer, ByVal column As Integer) As Integer
        Get
            If row < 0 Or row > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            If column < 0 Or column > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            Dim index As Integer = (row * 3) + column
            Return m_values(index)
        End Get
        Set(ByVal value As Integer)
            If row < 0 Or row > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            If column < 0 Or column > 2 Then Throw New System.ArgumentOutOfRangeException("3x3 array only!")
            Dim index As Integer = (row * 3) + column
            m_values(index) = value
        End Set
    End Property
    Public Function Add(ByVal matrixToAdd As Matrix3x3) As Matrix3x3
        ' declare a matrix to hold the result
        Dim result As New Matrix3x3
        ' loop through arrays and the members of the two matrixes, store in the result matrix
        For i As Integer = 0 To 8
            result.Item(i) = m_values(i) + matrixToAdd.Item(i)
        Next
        Return result
    End Function
    Public Overrides Function ToString() As String
        Dim msg As New System.Text.StringBuilder
        ' find the longest value when represented as string
        Dim longest As Integer = -1
        For i As Integer = 0 To 8
            If m_values(i).ToString.Length > longest Then longest = m_values(i).ToString.Length
        Next
        ' create the string
        longest += 1
        Dim width As String = longest.ToString
        For y As Integer = 0 To 2
            For x As Integer = 0 To 2
                msg.Append(String.Format("{0," & width & "}", m_values((y * 3) + x)))
            Next
            msg.Append(Environment.NewLine)
        Next
        Return msg.ToString
    End Function
   Public Shared Widening Operator CType(ByVal str As String) As Matrix3x3
      Dim parts() As String = str.Split(New Char() {" "c, ","c, CChar(Environment.NewLine)}, System.StringSplitOptions.RemoveEmptyEntries)
      If parts.Length <> 9 Then
         MsgBox("Invalid String to Matrix Conversion")
         Return Nothing
      End If
      Dim returnval As New Matrix3x3
      Try
         For i As Int32 = 0 To 8
            returnval.Item(i) = Integer.Parse(parts(i))
         Next
      Catch ex As Exception
         MsgBox("Invalid String to Matrix Conversion")
         Return Nothing
      End Try
      Return returnval
   End Operator
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.