Creating and reading from arrays

Post

Posted
Rating:
#1 (In Topic #1341)
Trainee
Okay. Long-time VB programmer. Founf Gambas, yay. Figured the easiest way to learn it was to take some of my old utility programs (REALLY simple) and convert them up. Come to the wiki and Gambas One when something's unclear or just odd. To wit:

I need to declare an array as string and then load it up with the variables.

Code (gambas)

  1. Dim arrSyllable As New String[185]
  2.  
  3. arrSyllable[1] = "a"
  4. arrSyllable[2] = "e"
  5. arrSyllable[3] = "i"
  6. ...
so that I can later do this:

Code (gambas)

  1.    For SyllableCounter = 1 To NumberOfSyllables
  2.        SyllableNumber = CInt(RandomNumber(184)) 'No more than 184 syllables in the array
  3.        Word = Word & arrSyllable(SyllableNumber)
  4.    Next 'SyllableCounter
  5.  
Gambas lets me do this, but never seems to load the strings into the array in the first place. Tried

Code (gambas)

  1. Public arrSyllable as new String[185]
  2.  
so I could try

Code (gambas)

  1. arrSyllable.Add[1] = "a"
  2. ...
  3.  
but it wouldn't let me.

So simple as this is, what am I doing wrong?

Dj
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

Homyakchik said


I need to declare an array as string and then load it up with the variables.

Code (gambas)

  1. Dim arrSyllable As New String[185]
  2.  
  3. arrSyllable[1] = "a"
  4. arrSyllable[2] = "e"
  5. arrSyllable[3] = "i"
  6. ...

Code (gambas)

  1. Dim arrSyllable as new String[] = ["a","b","c",.......]
  2.  

Homyakchik said

Iso that I can later do this:

Code (gambas)

  1.    For SyllableCounter = 1 To NumberOfSyllables
  2.        SyllableNumber = CInt(RandomNumber(184)) 'No more than 184 syllables in the array
  3.        Word = Word & arrSyllable(SyllableNumber)
  4.    Next 'SyllableCounter
  5.  

Code (gambas)

  1. ' Generate a string of random syllables
  2. For SyllableCounter as Integer = 0 to  NumberOfSyllables - 1
  3.   Word &= arrSyllable[Rand(184)]

Homyakchik said

Gambas lets me do this, but never seems to load the strings into the array in the first place. Tried

Code (gambas)

  1. Public arrSyllable as new String[185]
  2.  
so I could try

Code (gambas)

  1. arrSyllable.Add[1] = "a"
  2. ...
  3.  
but it wouldn't let me.

Code (gambas)

  1. Public arrSyllable as new String[185]
  2. arrSyllable.Add("a")
  3. ...
  4.  

Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
It'll take a little while to adjust from vb to gambas, they are syntactically similar and different in various ways but you'll soon get the hang of it.

Maybe something like this is what you need

Any local variables declared with Dim are only visible to the local method.
global variables are declared with Public or Private in the global space (top of file)

Variable contents can be intserted at definition or at runtime in a method.
There are various initiation methods.
For a Form like FMain.class you can use the _new() method or Form_Open()

Code (gambas)

  1. Public arrSyllable As String[] = [ "a", "e", "i", "etc"]
  2.  

or initiate it in the classes _new method

Code (gambas)

  1.  
  2. Public arrSyllable As String[]
  3.  
  4. Public Sub _new()
  5.  
  6.   arrSyllable = [ "a", "e", "i", "o" , + the 180 others]
  7.  
  8.  
  9.  


Or by adding to a initiated array

Code (gambas)

  1.  
  2. Public arrSyllable As New String[]
  3.  
  4. Public Sub _new()
  5.  
  6.   arrSyllable.Add("a")
  7.   arrSyllable.Add("e")
  8.   arrSyllable.Add("i")
  9.   arrSyllable.Add("o")
  10.   ' + the 180 others
  11.  
  12.  
  13.  

Or by adding to a initiated sized array (as you did)

Code (gambas)

  1.  
  2. Public arrSyllable As New String[184]
  3.  
  4. Public Sub _new()
  5.  
  6.   arrSyllable[0] = "a"
  7.   arrSyllable[1] = "e"
  8.   arrSyllable[2] = "i"
  9.   arrSyllable[3] = "o"
  10.   ' + the 180 others
  11.  
  12.  
  13.  

Have fun :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.