Keeping it local

I understand that you are supposed to keep the code close to the control as in a radio button event as much as possible. I have this code…
DIM racefile As string
if me.active then
racefile = “bris”
End If

Have three radio buttons each with code similar to this, each has a different string based on it being selected. Question is, how do I get ‘racefile’ recognized else where in the program. Ie; I want to write ‘racefile’ to a file on the hard disk.

Thanks Milfredo

Code, perhaps.
Although many people suggest that the code should call a method, in order that you decouple the business rules from the UI…
It allows you to call that code from another area of the program easily

But this string definitely shouldnt be declared in the event, because you want it to be available elsewhere.

You might declare it as a property of the Application, and make your code

App.racefile = “bris”

Or you can declare racefile as a string property of the window. In which case just remove the Dim racefile from the event.

You might add a function to the window,

public function GetRacefileName() as string
if radiobutton1.value then
 return "bris"
end if

end function

so instead of setting the valu in the event, you just work out what it should be when required by looking at the values of the controls

Think of scope as layers on an onion. From innermost:

Local variable (Dim)
Property of subclassed control
Property of Window
Protected Property of Module / Property of App
Global Property of Module

Each one has more visibility, and more danger of misuse. Scope can be hard to understand at first, but finding the right balance makes all the difference.

Yep, scope is like an onion. It will make you cry.

This oop stuff is not easy to get my head around. So use to the top down in Basic. Thanks guys.

Jeff,

I tried to figure out what you meant by your suggestions. I eliminated the Dim statement in the actions and still get item doesn’t exist. I might need to go back to basic and stick with it. I don’t seem to understand where code is supposed to go. The books included are confusing. I tried adding your public function to the window and still got Item doesnt exist.

Xojo promotes this as the easiest language for a beginner to learn. I think they might be nuts. I don’t think I’m a complete idiot. I have a pretty complicated stand alone app that I built that quite a few people use everyday that I wrote in Liberty Basic.

Milfredo

It took me a while when I first started. Think “event driven”. You program starts, and nothing happens. It just squats there looking at you until the user clicks something - that is where and when you make something happen. When done, you either quit, or allow the program to squat there looking at you again.

Not like a procedural language where you start doing things and stop when you see “STOP RUN.” :wink:

What you want is a Property. These are described in User Guide Book 1:, Chapter 5: Classes, Section 1: About Classes. A Property is accessible to the entire class (which is your Window in this case).

Choose “Insert->Property” to add a property and then give it a name such as racefile and a type (String).

You might also find this info useful:

Thank you. On the road now be home tomorrow and will look at the info.