Syntax error with shared members/properties

I’m just getting started on my first project which is an educational web project. Here’s my shared property:

Static pShapes(9) As String = (“SWS”, “SWD”, “SWC”, “SBT”, “SBS”, “SBH”, “SBD”, “LWS”, “LWD”, “LWC”)

and here’s my shared method:

Dim R As New Random
Dim ValueD As Double
Dim ValueI As Integer

ValueI = UBound(pShapes,1)
ValueD = R.InRange(0, ValueI ) //Get random number
ValueI = ValueD //Automatically converts to Integer.

return pShapes(ValueI)

I’m getting errors on the UBound call (parameters are not compatible with this function) and the return statement (This is not an array but you are using it as one)

I don’t understand either error. Can someone help me out?

First, to assign an array, you need the Array keyword and you don’t dimension the array yourself:

Static pShapes() As String = Array("SWS", "SWD", "SWC", "SBT", "SBS", "SBH", "SBD", "LWS", "LWD", "LWC")

Next, there is no second parameter for Ubound when dealing with a single-dimension array, so it should just be:

Valuel = Ubound(pShapes)

Finally, you don’t need the double-to-integer conversion, especially since InRange returns an integer anyway, so you can do this:

Valuel = R.InRange( 0, pShapes.Ubound )

Compare this version to yours and perhaps it will help you as you learn:

Static pShapes() As String = ( "SWS", "SWD", "SWC", "SBT", "SBS", "SBH", "SBD", "LWS", "LWD", "LWC" )

Static R As New Random // No need to create a new one with every call

Dim ValueI As Integer = R.InRange( 0, pShapes.Ubound ) //Get random number
return pShapes( ValueI )

Thanks for the help. I’ve made the changes you recommended but I’m still getting the errors on the UBound call (this time it’s this item does not exist) and the return statement (This is not an array but you are using it as one). It seems the method does not recognize the pShapes array. I’ve declared both the pShapes array and the GetRandom method inside my cShapes class. Both are shared. Is there something else I’ve neglected to do?

Are you saying that pShapes is actually a property of your class? If so, it should be defined as:

pShapes() As String

In the actual code (I assume the class Constructor?) you should not re-declare it with static, but simply assign a value to it:

pShapes = Array( … )

static or dim will turn something into a local variable, overriding any property of the same name.