Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass as an argument an array with unknown size 1

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello everyone, I have an array without knowing it's size from the start.
And I need to pass it as an argument in a routine. I can know the actual size before I call the sub, but I don't know how to declare it as an argument, so I am getting error.

Public LonelyChckrs() As Checker
Dim Lonelies As Byte = 0
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then Lonelies += 1
Next
Dim ArrayLngth As Byte = Lonelies - 1
ReDim LonelyChckrs(ArrayLngth)
If Lonelies > 0 Then FillArrayWithLonelies(Lonelies, LonelyChckrs(ArrayLngth)) 'here I am getting Error 3
Value of type 'Backgammon.Checker' cannot be converted to '1-dimensional array of Backgammon.Checker'

and here is my routine
I am getting error from the first statement Error 4 Array bounds cannot appear in type specifiers.

Public Sub FillArrayWithLonelies(ByVal Lonelies As Byte, ByVal LonelyChckrs(lonelies-1) As Checker)
Dim j As Byte = 0
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then
LonelyChckrs(j) = New Checker
LonelyChckrs(j) = PcCheckerGates(i).stack.Peek
j = j + 1
End If
Next
End Sub

Any suggestions please? Any help will be much appreciated. Thank you so much in advanced.
 
You cannot pass an array ByVal; you must pass ByRef. You can pass an individual element of an array ByVal, but if you want to pass the entire array, it must be ByRef.

The line of code, "FillArrayWithLonelies(Lonelies, LonelyChckrs(ArrayLngth))" is not passing the entire array; it's only passing one element of the array - specifically the element with subscript (ArrayLngth).

There is a nice section in the help document on the subject of passing arrays as parameters describing what you can and cannot do, and how to do it.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
I managed to do it like that
ReDim LonelyChckrs(ArrayLngth)
If Lonelies > 0 Then FillArrayWithLonelies(Lonelies, LonelyChckrs)


Public Sub FillArrayWithLonelies(ByVal Lonelies As Byte, ByVal LonelyChckrs() As Checker)
Dim j As Byte = 0
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then
LonelyChckrs(j) = New Checker
LonelyChckrs(j) = PcCheckerGates(i).stack.Peek
j += 1
End If
Next
End Sub

However I am facing the same problem if I try to pass as an argument a structure that its field is an array. Impossible to pass the entire structure even byref even byval. Any suggestions???
 
For example
Public Structure CurrentSituation
Dim Checkers() As Checker
Dim CheckerGates() As GatesPositions
Dim TopGates() As ArrayList
End Structure

No effect even with (ByVal InitialUsrState(0) As CurrentSituation)
(ByVal InitialUsrState() As CurrentSituation)
(ByVal InitialUsrState As CurrentSituation)
even using instead of byval, byref

Public Sub StoreUsrBe4PcPlayedSituation(ByVal InitialUsrState(0) As CurrentSituation)

end sub

I wish I could do that :/
 
You're still showing it as a ByVal.

Also, you have an array of objects, where the object type is Checker. However, when you do the ReDim of the array, you are creating a new array of type checker, but the ReDim does not create a new instance of the checker object in each element. In other words, you may need to do something like this:

ReDim LonelyChckrs(2)
LonelyChckrs(0) = New Checker
LonelyChckrs(1) = New Checker
LonelyChckrs(2) = New Checker

Or, you can look into using ReDim Preserve.


--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
==> Public Sub StoreUsrBe4PcPlayedSituation(ByVal InitialUsrState(0) As CurrentSituation)
Again, that shows a ByVal parameter and the parameter is defined as an element, not an array. Remove the subscript for an array.

Public Sub StoreUsrBe4PcPlayedSituation(ByRef InitialUsrState() As CurrentSituation)

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
You are right the statement LonelyChckrs(j) = New Checker doesn't need to be there. I put it because I was afraid of null reference at runtime. The array of LonelyChckrs gets values from the top of PcCheckerGates each time if the stack count is equal to 1. LonelyChckrs is an empty array that I know it's length before I call the sub FillArrayWithLonelies. So, I pass it as an argument, as an entire array and the sub fills the LonelyChckrs from PcCheckerGates.

Public Sub FillArrayWithLonelies(ByVal Lonelies As Byte, ByVal LonelyChckrs() As Checker)
Dim j As Byte = 0
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then
LonelyChckrs(j) = New Checker
LonelyChckrs(j) = PcCheckerGates(i).stack.Peek
j += 1
End If
Next
End Sub
 
I am still getting error with Public Sub StoreUsrBe4PcPlayedSituation(ByRef InitialUsrState() As CurrentSituation)
Error 2 'PcInitialsituation' cannot expose type 'Module1.CurrentSituation' outside the project through class 'Form1'.
 
That looks to be a scoping problem. That it an issue where and how things are defined and referenced. Given the snippets posted, there is not enough information to delve into that much further.

==> I put it because I was afraid of null reference at runtime.
And that may continue because as I said in the previous post, the ReDim will resize the array, but each array element will be a null object, and objects inside the array element object will also be null objects, unless you ReDim Preserve. However, even with the preserve, if you increase the array size, all object references in the expanded portion of the array will be null references.


--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
I just, cut pasted the structure from the module that is declared to the form general declarations. I can't understand this. Do the variables declared in the form have bigger range and they have when they declared in a module??? I was thinking that when I declare variables in a module they are accessable from the entire project.
 
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then Lonelies += 1
Next
Dim ArrayLngth As Byte = Lonelies - 1

That's why I am saying ReDim LonelyChckrs(ArrayLngth)
If Lonelies > 0 Then FillArrayWithLonelies(Lonelies, LonelyChckrs)

when Lonelies are <=0 this FillArrayWithLonelies will never run

Is it maybe better to change it like that????

For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then Lonelies += 1
Next
Dim ArrayLngth As Byte = Lonelies - 1
If Lonelies > 0 Then FillArrayWithLonelies(Lonelies, LonelyChckrs)

Public Sub FillArrayWithLonelies(ByVal Lonelies As Byte, byref LonelyChckrs() As Checker)
ReDim LonelyChckrs(Lonelies - 1)
Dim j As Byte = 0
'gemizw ton pinaka twn lonely checkers
For i As Byte = 0 To PcCheckerGates.Length - 1
If PcCheckerGates(i).stack.Count = 1 Then
LonelyChckrs(j) = PcCheckerGates(i).stack.Peek
j += 1
End If
Next
End Sub
 
There is nothing wrong with the ReDim, nor with where you had it. I don't think you understand what I was saying about the behavior of ReDim with respect to an array of objects or to objects within the array elements.

If you have an array of integers and you redim the array, then you can immediately reference the new array elements. The individual elements with all have a value of 0, but you won't get a runtime error if you look at them. However, if you redim an array of objects, then you cannot immediately reference the new array elements. You will get a null object reference if before assigning them a new value or creating a new instance of the object.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
Wise men speak because they have something to say, fools because they have to say something. - Plato
 
I understand what you mean!!!! I tested it with integers and I saw that when I pass it byval the values are 0 when I return to the form load after the routine ends and I saw that when I passed it byref I didn't have the 0 values because it's by reference on memory. I will see what I will do about objects!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top