An explanation on using me.(whatever)

Hi All.

I need to understand why when I use this code in a radioGroup under selection changed:

Select case me.SelectedIndex

case 0

System.DebugLog “Selected Daily”

case 1

System.DebugLog “Selected Weekly”

case 2

System.DebugLog “Selected Monthly”

end Select

My code compiles, but if I use:

Select case RadioGroup1.SelectedIndex

case 0

System.DebugLog “Selected Daily”

case 1

System.DebugLog “Selected Weekly”

case 2

System.DebugLog “Selected Monthly”

end Select

it fails.

And as a secondary (minor) thing, is it possible to that NO radio button is selected at startup? By the nature of radiobuttons, I figure at least ONE must be active.

Regards

This should work – it works for me.

And yes, it is possible for none of the buttons of a RadioGroup to be selected. As explained in the documentation you can set SelectedIndex to -1 for no selection.

It should work if you are placing the code into a control that is on a window. On the other hand, if you are working on a subclass of RadioGroup, you need to use ‘me’ because you are referring to properties of an abstract class.

Hi there.

Thanks for the response. I am just using is In a RadioGroup Set with an Event Hander “Selection Changed.”

I am using a select case to handle which radio button is being selected so I can act on it.

Am I doing something wrong?

Regards

Why aren’t you happy using ‘me’? It’s more flexible because you can then rename the control and it keeps working.

What does the compile error say?

Hi All.
I found out what my issue was.

When I wanted to use windowMain.RadioGroup1. SelectedIndex

I failed to put in an index number. It should have been

windowMain.RadioGroup1(0).SelectedIndex

And Eric, I am fine using me.XXX I just didn’t grasp its subtlety.

Thanks to everyone for your help.

This compiles and runs fine for me under 2026r1.2, FWIW:

Select Case RadioGroup1.SelectedIndex
Case 0
Case 1
End Select

But as @Eric_Williams says, using “Me” is generally better programming practice.

A good way to look for issues like this is to verify that the IDE is auto-completing and/or that it can identify elements of your code when you click on them. For example if you click on “SelectedIndex” and it doesn’t know what that is, you know you’ve got a syntax error of some kind.

thanks Julia.

One other thing I can do is LEARN TO READ :smiley:

I think I saw it, but disregarded it after reading.

I’m proof of the saying “When someone makes something foolproof, they invent a better fool” :smiley:

Honestly, I’ve done that more often than I care to admit - forgotten to include the index when typing a property of a control that’s part of a Control Set - and I’m always like “Why isn’t the &^$*% auto-complete working??”

Here’s my personal rule on me vs self vs explicit naming:

  1. Never explicitly call by name unless you have no other choice. For example, all of a window’s controls and properties should be private, allowing you only the constructor and other methods to pass data into and out of the window. The window should be responsible for what to do with that data. This way, Window1 can know of Window2, but Window1 cannot directly ever call controls on Window2. This way the controls on Window2 can be changed (such as swap a checkbox for a radio when you need to add a third option) without affecting the code in Window1. Window2 still needs to explicitly call its controls by name of course, but that’s what I mean by no other choice.
  2. Me should only ever be used inside a control’s on-window event handler. I actually argued to remove its existence from every other bit of code, but it would break too much.
  3. `Self’ is used everywhere else.
  4. I always prefix with Self to avoid any ambiguity. This means calling Self.TextField1 instead of just TextField1. When I come back to the code later, it’s clear to me where that resides. While control names tend to be more obvious, this is especially handy with method names. Since I won’t use global methods or properties either, it becomes very clear where everything resides. I don’t have to go hunting for it.

And not a rule, but I wish we had This or Static or something like that so shared methods and module methods can reference their container. This is mostly for factory methods so I could use new this(param1, param2) instead of explicitly writing the class name.

windowMain.RadioGroup1(0).SelectedIndex

if you have a control set the event should give you the index too.

RadioGroup1(index).SelectedIndex

Is there a reason you are using RadioButtonGroups and RadioButton Sets together? RadioButtonGroups already contain multiple RadioButtons, and you can add more at any time. Groups have benefits, with easy identification of which is selected, shared events etc.

Sets have different benefits and can be useful in other ways. Good old RadioButtons still exist and can be used by adding a group and changing the super to RadioButton (desktop or other).

To have both a group and a set seems overkill, so I’m just curious.

FWIW, it also applies to any other kind of controls.

explanation on using me.(whatever)

In OOP classes of objects may have defined properties and methods. At their design, people can define certain “behaviors” like “call this method when someone click on this visual instantiated object”, that’s called an “event handler”. As the handler can be called from any instance of any object of that class like obj1.onclick(…) or obj2.onclick(…), imagine that internally the class define the onclick(..) as myClass.onclick(me As myClass, …) and when firing onclick() on any of those myClass objects, let’s take obj1 for instance, it calls it as myClass.onclick(obj1, …)

This “sender” parameter is implicit, you can’t see it in a Xojo method signature onclick() but the “me” sender is there and you can use it, and you know what object is being handled by this class handler method.

I hope that what I said makes sense to you as it makes to me. Talking and drawing is better than writing because the understanding differs depending on the previous expertize of the learner.