Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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...
VB
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

VB
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
      row = TextBox2.Text
      col = TextBox3.Text
    m.Item(row, col) = ((row * 3) + col) 
    n.Item(row, col) = rand.Next(0, 2000) 
    a1 = a1.Add(n)
    add1txt.Text=a1.ToString
    Next
    Next
   
    End Sub

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

please help
thanx in advance
Posted
Updated 27-Jun-12 0:00am
v4
Comments
arizan 27-Jun-12 6:01am    
please tell me the code for m*n matrix..
how to give the loop for rows n columns in classes

1 solution

Please refer similar thread:
matrix algebra in VB.NET application[^]
 
Share this answer
 
Comments
arizan 27-Jun-12 4:19am    
But still not getting the required result....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900