GrandpaB

Ií»ve created a class that has an array of doubles and a couple of integers. The methods that Ií»ve created return nothing! Ií»ve stepped through the code in the Add method. The values for matA and matB are correct and the correct values are stored in X just before the return statement. But, then the returned value in the calling program has nothing in it. The calling statement is:

matC.Add(matA, matB)

I doní»t understand why this isní»t working. Any insight you can provide would be greatly appreciated, thanks.

Code Snippet

Public Class Mat

Private _M(,) As Double 'An array of Double holds Matrix values

Private _Im As Int32 'The max value for the 1st index

Private _Jm As Int32 'The max value for the 2nd index

Public Sub New()

ReDim _M(0, 0)

_Im = 0

_Jm = 0

End Sub

Public Sub ReSize(ByVal Im, ByVal Jm)

ReDim _M(Im, Jm)

_Im = Im

_Jm = Jm

End Sub

Public Property M(ByVal i As Int32, ByVal j As Int32) As Double

Get

Return _M(i, j)

End Get

Set(ByVal value As Double)

_M(i, j) = value

End Set

End Property

Public ReadOnly Property Im() As Int32

Get

Return _Im

End Get

End Property

Public ReadOnly Property Jm() As Int32

Get

Return _Jm

End Get

End Property

Public Function Add(ByVal A As Mat, ByVal B As Mat) As Mat

Dim X As New Mat

If (A.Im = B.Im) And (A.Jm = B.Jm) Then

X.ReSize(A.Im, A.Jm)

For i As Int32 = 0 To X.Im

For j As Int32 = 0 To X.Jm

X.M(i, j) = A.M(i, j) + B.M(i, j)

Next

Next

End If

Return X

End Function

End Class




Re: Visual Basic Express Edition I caní»t return an Array from a method.

DMan1

How are you declaring matA, MatB, and MatC....without calling the resize method for each class you would be working with a (0,0) matrix.




Re: Visual Basic Express Edition I caní»t return an Array from a method.

GrandpaB

DMan1,

Thanks for your response. The calling program calls New and ReSize for each one of the matrices (MatA, MatB, and MatC) before calling the Add method. (FYI There is also another New(Im, Jm) constructor that allows the user to size the matrix as it is constructed.) When I stepped through the code the, X matrix was the same size as A and B, and the values in X.M were correct. After the Return X statement was executed, I examined the contents of MatC and found it to be in the same state as it was before the call to the Add method.

In the Add method if A and B do not have the same dimensions then X is not resized and a (0,0) matrix is returned.






Re: Visual Basic Express Edition I caní»t return an Array from a method.

JohnWein

I think you want to call Add with something like this:

Code Snippet

Dim C As Mat = A.Add(A, B)





Re: Visual Basic Express Edition I caní»t return an Array from a method.

GrandpaB

John,

Thanks, that worked, but can you explain what's going on. The calling syntax seems a bit odd:

C = A.Add(A, B)

It would make more sense to me if I could write:

C = Mat.Add(A, B)

Left to me own devices I came up with another solution. I defined a Sub Add like this:

Code Snippet

Public Shared Sub Add(ByVal A As Mat, ByVal B As Mat, ByVal Sum As Mat)

If (A.Im = B.Im) And (A.Jm = B.Jm) Then

Sum.ReSize(A.Im, A.Jm)

For i As Int32 = 0 To Sum.Im

For j As Int32 = 0 To Sum.Jm

Sum.M(i, j) = A.M(i, j) + B.M(i, j)

Next

Next

Else

Sum.ReSize(0, 0)

End If

End Sub

Then the calling program would write:

Mat.Add(A, B, C)

I prefer the syntax of the function if I could use Mat.Add. Any further thoughts would be appreciated, thanks again.






Re: Visual Basic Express Edition I caní»t return an Array from a method.

BadButBit

hi,

your final solution looks good to me except if you want the 'C' value of your calling function to be changed when it called

Mat.add(A, B, C)

then you'll need to change your add declaration to

Public Shared Sub Add(ByVal A As Mat, ByVal B As Mat, ByRef Sum As Mat)

instead of

Public Shared Sub Add(ByVal A As Mat, ByVal B As Mat, ByVal Sum As Mat)

the byRef Sum as Mat part means that you're not sending a copy of 'C' but a pointer to the address where 'C' is located so that the called method can fiddle with it... sort of speak. otherwise, the copy of C called Sum will be changed but when you get back to your calling function the original is still intact

hope that helps

BadButBit