Newbie Question - Initializing variables

Ok, so newbie back with an other question. I’m like on page 55 of the intro to xojo programming, just or reference.

So, I like to play around a little after I finish a chapter. While I say newbie, I did use Turbo Pascal way back… like 30 years ago… When gui was not a thing and it was just me and a monochrome monitor and text. … lots of text… and I find some things coming back to me, in bits and pieces.

So…

I tried something like this:

Var mystring1, mystring 2 as string = “Test String”

I was able to determine that this would init both of them to “Test String”

but…

Var myString1 = “Test String 1” , myString2 =" Test String 2"
really, sends the debugger into a fit.
So… I have figured either…

  1. You can’t init two variables on the same line. But you can declare them.
  2. I am trying to do this wrong.

So… which is it?

Thanks

Donald

1 is true.

Since you are learning to develop software: don’t do a bulk of declarations at the beginning of the method. Don’t do:

dim bla as string
'some code
'some other code
'more code
bla = something

Instead do

'some code
'some other code
'more code
dim bla as string = something

Code is more readable and your brain needs to hold less information because you can see immediately what you are doing with the variable bla.

@Beatrix_Willius Thank you. :smiley:

@Beatrix_Willius One more question. What option did you use to get your example code to be formatted so nicely? I would have done something like that when I posted, but I couldn’t figure out what option to use when I posted.

Thanks again.

Donald

You can read the Discourse code formatting guide here. Take a look at the Block Code Formatting section. For Xojo blocks, start the block with:

```xojo
If me.Value <> "" then
  searchString = me.Value
  FontListBox.RemoveAllRows
  
  for counter = 0 to myFontCount -1
    theFontName = system.FontAt(counter)
    if theFontName.IndexOf(searchString) >=0 then
      fontlistbox.AddRow(theFontName)
    end if
  next
else
  FontListBox.RemoveAllRows
  for counter = 0 to myFontCount -1
    FontListBox.AddRow(system.FontAt(counter))
  next
end if

Test

@Anthony_G_Cyphers thanks :smiley:

1 Like