Dim - variable help needed

Hi,
In my main window’s open event, I have the following code:

Dim ToDisplay as String ToDisplay = "no"

Inside a button’s action event (in the same window), I have the following statement:

ToDisplay = "yes"

When I try to compile the project I receive a “This item does not exist” error??
Could someone please help, as I do not want to have to create lots of hidden empty TextFields, just for logic reasons.

P.S
Unrelated to my question above but when I download something from GitHub, I often see a .dm file?
What type of file is .dm, and what app do I need to read it?

Thank you all in advance.

Richard,

Variables created inside of a method are valid only in that method. More so, methods created in a confined scope (such as a method) are only available inside of that scope. For example:

Dim a As Integer = 0

If a = 0 Then
    Dim b As Integer = a + 1
End If

MsgBox Str(b) // INVALID... b is only available inside of the scope it is created in.

I’d really suggest you take a peek at: http://xojo.com/learn/ and read the free book Introduction to Programming with Xojo. It will help with this and many other issues you are sure to run into.

Good luck and above all, have fun!

Oh, to solve your immediate problem, create ToDisplay as a Property. That will be accessible anywhere within that window, but do take a peek at the link I provided, it really is a great resource. Will save yourself many frustrations such as this one.

To add to that, when you create a property on a Window, you do not need to Dim it, you would just use it. In fact, if you Dim it within a method, it will create a local variable and ignore the window property.

.dm files are typically audio files that are encoded with DRM protection (Digital Rights Management), such as specialized things like ring tones for certain cell phones, etc.

Kem and Jeremy,
So I insert > property for window1.
Then inside that new property I set it up so that:

Name = ToDisplay Type = String Default = no Scope = Public

Then I can simply type ToDisplay = “yes” from any button control within the same window.
Is that correct?

P.S
I am now reading the “Introduction to Programming with Xojo” book (which I never knew existed) :slight_smile:

Thank you all.

[quote=82853:@Richard Summers]Then I can simply type ToDisplay = “yes” from any button control within the same window.
Is that correct?[/quote]

Yes

Now you will start to see the light :wink:

I see it - I see it
:slight_smile:

So basically, a property created for a window is similar to a variable - meaning you can set and edit its value from within the same window.
Is it then possible, to create a property for the app - which then makes that property accessible anywhere in the app? Or is that just wishful thinking?

Thanks.

[quote=82858:@Richard Summers]So basically, a property created for a window is similar to a variable - meaning you can set and edit its value from within the same window.
Is it then possible, to create a property for the app - which then makes that property accessible anywhere in the app? Or is that just wishful thinking?
[/quote]

A variable declared in an event or a method, as a scope of that event or method. It is not available out of them.

A window public property is a variable that has a scope of the window ; it is available within all objects and methods within the window.

A module global property has a scope of the entire app. Add a module to your app and then add the property to it.

Be careful with global properties. It is so easy to modify them from anywhere that it can create difficult to track bugs. As a general rule, it is always better to keep variables within the smallest scope as possible.

For instance, I use the integer i for loops and the buff string all the time within the scope of methods. Sometimes as Dim, and their value starts as 0 and “” when the method is called, or as Static, and they keep their value within the scope of the method. If I used global variables instead, I could not use Static and the value could be modified anywhere. Also, each method would modify the global variables, and it would create a lot of confusion.

Keep reading, Richard !

Michel,
Thank you so much - your explanation has made it all clear now.

Things are starting to fall into place now :slight_smile:

THANK YOU !

That’s not correct, it is addressable from outside of the window.[quote=82862:@Michel Bujardet]Static, and they keep their value within the scope of the method[/quote]
Be careful: a static variable in a method of a class is the same for all instances of that class (it’s like a shared method of a class, but restricted to the method it is defined in).

[quote=82872:@Eli Ott]Michel Bujardet A window public property is a variable that has a scope of the window ; it is available within all objects and methods within the window.
That’s not correct, it is addressable from outside of the window.[/quote]

You are right. It can be addressed with the window name as prefix.

I was trying to keep it simple for Richard, though. Sometimes, too much details, how accurate, tend to mud comprehension :wink:

Same thing for classes : he is not there yet…

I have just updated my brain regarding prefixing the window’s name :slight_smile:

If your variable is going to hold “yes” or “no”, consider making it a Boolean instead of a String. Booleans can hold True or False, and you can use them this way:

ToDisplay = True
ToDisplay = False
if ToDisplay then …

As a String, you’d have to remember what values they hold and that can lead to bugs:

ToDisplay = "yes"
ToDisplay = "ys" // Typo
ToDisplay = "true" // Late at night and you forgot

To test them, you always have to compare them to some string, which is slightly slower:

if ToDisplay = "yes" then …

If the variable can hold more than those two values, but will still act as a type of switch (“yes”, “no”, “maybe”), look into Enums.

Good point Kem - thank you.