1.

Solve : Visual Basic PAssing Arrays to Classes?

Answer»

Hello,
I am trying to pass an array from my form to a Class.
This is what is in my Form
Code: [Select]objFindName.NameArray(strNameArray) = mstrNameArray(24)
Here is what i have in my class
Code: [Select]Option Strict On
Option Explicit On
PUBLIC Class clsFindName
#Region "DECLARATION SECTION"
Private strSearchName As STRING
Private intCount As Integer
Private strNameArray(24) As String
#End Region
#Region "PROPERTY: Array"
Public WriteOnly Property NameArray(ByVal strNameArray() As Object) As String
Set(ByVal value As String)
strNameArray(24) = value(24)
End Set
End Property
#End Region
Nothing i have tried is working and Google provides some help, but i must be searching for the wrong thing or not knowing what i am doing. I have passed strings before and they work, just not the array.

Any help is appreciated.
Thanks Which version of VISUAL BASIC?
What error messages do you get?

Quote from: Bighonk1 on May 16, 2010, 11:32:40 PM

Hello,
I am trying to pass an array from my form to a Class.
This is what is in my Form
Code: [Select]objFindName.NameArray(strNameArray) = mstrNameArray(24)


when you assign an array, you have to assign an array. You aren't assigning an array- you are passing element 24 of the mStrNameArray (a string, I imagine) to the NameArray() property procedure. In this case, what the property procedure is receiving is two parameters- an array (I assume strNameArray exists). The only argument your property procedure accepts is the LEFT hand side (the value being assigned).

What you are doing with something like:

Code: [Select]class.arrayproperty(12)=value

is not valid; since your "arrayproperty" property doesn't accept any arguments other then the left hand side.

The way you would assign an array is as a whole:

class.arrayproperty=arrayvariable

Or, in your case, you can add an argument to your property procedure (I think, I'm not as familiar with .NET as with VB6, but I imagine if VB6 let's you VB.NET does):

[code]
Public WriteOnly Property NameArray(ByVal strNameArray() As Object) As String
Set(ByVal vIndex as Integer, ByVal value As String)
strNameArray(vindex) = vdata
End Set
End Property
[/code]


Discussion

No Comment Found