Double dimensional array in class

vks.gautam1

Well-known member
Joined
Oct 10, 2008
Messages
78
Location
Chandigarh, India
Programming Experience
Beginner
Double dimensional array in class

--------------------------------------------------------------------------------

Here i want to that user enter marks of 2 students.
I want to show the sum of marks of both the students seperately in list box.

Form on button click

VB.NET:
Public Class frmSum

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim i, j, result, no(1, 1) As Integer
        'Declare the Object of Class Sum
        Dim objsum As SUM
        objsum = New SUM
        For i = 0 To 1
            For j = 0 To 1
                no(i, j) = InputBox("Pls enter No ")
                no(i, j) = no(i, j)
            Next
        Next
        result = objsum.funsum(no)
        'Add the result in ListBox
        lstResult.Items.Add(result)
    End Sub
End Classclass


class code

VB.NET:
Public Class SUM
    Public Function funsum(ByVal no(,) As Integer) As Integer
        Dim result(1), i, j As Integer
        For i = 0 To 1
            For j = 0 To 1
                result(i) = no(i, j) + result(i)
            Next
        Next
        Return result(i)
    End Function
End Class
 
i want sum of two subjects marks of two students seprately in list box

student 1= total marks(subject1+subject2)

student 2= total marks(subject1+subject2)

it is giving following error
Index was outside the bounds of the array.
it is in class code ."Return result(i)"
 
it is giving following error "Index was outside the bounds of the array."
it is in class code ."Return result(i)"

And what is the value of i at this point in time? And does your array have an index at that value?

i.e. if i is 10, does your array have an element in location 10? probably not
 
I have get it right

Now values are added to listbox

example. 1) 20 , 2) 10 , 3) 15 , 4)5

now it is adding two values to list box.
1) 30=20+10
2) 20=15+5


here is class code
VB.NET:
Public Class SUM
    Public Function funsum(ByVal no(,) As Integer) As Integer
        Dim result(i), i, j As Integer
        For i = 0 To 1
            For j = 0 To 1
                result(i) = no(i, j) +(i)result
            Next
        Next
        Return result
      
    End Function
End Class

form code

VB.NET:
Public Class frmSum

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim i, j, result(), no(1, 1) As Integer
        'Declare the Object of Class Sum
        Dim objsum As SUM
        objsum = New SUM
        For i = 0 To 1
            For j = 0 To 1
                no(i, j) = Val(InputBox("Pls enter No"))
                no(i, j) = no(i, j)
            Next
            result = objsum.funsum(no)
            'Add the result in ListBox
            lstResult.Items.Add(result(0))
            lstResult.Items.Add(result(1))
        Next
      

    End Sub

    
End Class
 
Last edited:
Back
Top