We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge 4 - Colour buttons
#11
I would like to explain the issue of internal names in a hopefully understandable way:

AB says:
When creating a button, we assign its internal name to a variable.
The developer assigns the name of this variable himself. He can use the name that is most convenient and understandable to him.

That's correct, but 2 examples don't use the variable !

In litdev's and my demo the variable is used:
button = Controls.AddButton("",buttonX[i],buttonY[i])
If (Controls.LastClickedButton = button[i]) Then
ButtonRed = Controls.AddButton("",10,10)
If Controls.LastClickedButton = ButtonRed then

[i]The programs work independently of the internal button name


The variable is not used in the example by jr
Button1=Controls.AddButton("My Button1",0,0)
If Controls.LastClickedButton = "Button1" then

The string "Button1" is queried and not the content of the variable Button1.
Beginners might ask, why do I have to put the variable in quotation marks?

The variable is not used in the example by whturner


but[bb]=Controls.AddButton("",50*bb,gh-40)
lastbut=Text.GetSubTextToEnd(Controls.LastClickedButton,7)

It is assumed that the 7th position of the internal name always contains 1, 2 or 3.
Very mysterious for beginners.


Both  programs assume that the internal name never changes!

This could happen with an update of Small Basic (very unlikely).
But also as in my example, where the buttons have to be removed and recreated so that the text gets a different color.
 When a new button is created, the name is continuously incremented. I have expanded the example with a text window output:
    [/i][/i][/i][/i]
Reply
#12
I still not get clear what you said but I will explain how it works.
In csharp there is dictionary in which object like button are stored.
https://github.com/sb/smallbasic-editor/...Library.cs
View line number 31 to 36 the method add button use namedcounter which on every new button added store ir as button1, button2
And when needed this button are used by there I'd to get the object and modify property using dictionary
ZS
Reply
#13
Also in csharp they are stored like button1,button2
ZS
Reply
#14
Hi,

Like others, I'm not really sure what the issue is and don't think describing how it works will add anything.

In my example I set the button to the element of an array.

Code:
button[i] = Controls.AddButton("",buttonX[i],buttonY[i])

In your example you use separate variables for each button:

Code:
ButtonRed = Controls.AddButton("",10,10)

In these cases, they "work independently of the internal button name ".

In the example by JR, he does use the internal variable name - it works, but I would always prefer to store in a local user variable.  This looks like maybe a lucky oversight by JR.

Code:
Button1=Controls.AddButton("My Button1",0,0)
...
If Controls.LastClickedButton = "Button1" then

The only time I might consider using the internal name is to access a large number of shapes without referring to user variables, for example removing Turtle lines (that are shapes). 

Equally, and proably better would be:

Code:
Button1=Controls.AddButton("My Button1",0,0)
...
If Controls.LastClickedButton = Button1 then

Actually, the following would also work, but clearly is harder to follow:

Code:
Controls.AddButton("My Button1",0,0)
...
If Controls.LastClickedButton = "Button1" then

This is partly why I used arrays to store the buttons in my example.

EDIT

Note the [i] array index in "button[i] = Controls.AddButton("",buttonX[i],buttonY[i]) ", which you missed in your post that confused me a bit.
Reply
#15
(translated by Google translator)

Hello.  Shy
Now I'm sure I understood Scout's question correctly the first time.
For this case I have the following answer:
I think using an internal name can be considered just a life hack, a clever way to use ADDITIONAL information about an object.

Moreover, in some cases, using an internal name makes it much easier to achieve a goal.
Therefore, knowledge of the existence of an internal name is not necessary for the beginner, but is useful for the hobbyist as an additional detail in a Swiss folding knife.  Smile
[-] The following 1 user Likes AbsoluteBeginner's post:
  • litdev
Reply
#16
Hi z-s
thanks for the github-link of SmallBasic. 
I didn't know the source code was public!?
Below are a few interesting lines

internal sealed class ControlsLibrary : IControlsLibrary
    {
        private readonly NamedCounter counter = new NamedCounter();
        private readonly Dictionary<string, BaseControl> controls = new Dictionary<string, BaseControl>();

        public string AddButton(string caption, decimal left, decimal top)
        {
            string name = this.counter.GetNext("Button");
            this.controls.Add(name, new ButtonControl(name, caption, left, top, width: 80, height: 30));
            return name;
        }
       public string AddMultiLineTextBox(decimal left, decimal top)
        {
            string name = this.counter.GetNext("TextBox");
            this.controls.Add(name, new MultilineTextBoxControl(name, left, top, width: 200, height: 50));
            return name;
        }

        public string AddTextBox(decimal left, decimal top)
        {
            string name = this.counter.GetNext("TextBox");
            this.controls.Add(name, new TextBoxControl(name, left, top, width: 200, height: 20));
            return name;
        }

The names of the controls for the dictionary entry are assigned here.
But what if, as part of the program optimization, the name of MultiLineTextbox is changed from "TextBox" to "MultiTextbox"?
Or if a common counter is used instead of a separate counter for each name or the formatting is changed?
How the control library manages its dictionary entries is its sole responsibility and the application should only work with the results returned via a defined interface ("return name;").
It is the principle of object-oriented programming with encapsulation of classes that you should not know what is going on within the class.
I have not yet seen an official example in which the application works directly with the internal names, but only with the contents of the variables.

@litdev

In my previous post the indices of arrays clash with the formatting of the post. 
I can't fix it. Angry

In the future I will always have to include the source code as "Code". Smile
[-] The following 1 user Likes Scout's post:
  • AbsoluteBeginner
Reply
#17
At last I get the whole Story.
Scout means there should be a function for assign a name to a button when it is added so we don't need more primitive and it may help to increase program optimization.
But unfortunately there is no option in SB for that.
But you could use this two methods
First you can create your own extension for small basic that have this functionality or update small basic (work of month. Smile )
And you can easily use directly like "button1" for first button but that makes difficulty while writing code.

At final I am fully agreed with AB'S suggestions
ZS
Reply
#18
As AB says, its just an internal feature that can be fun to play with, but not required in most cases.  Example where it is useful.

Code:
count = -10
Turtle.Speed = 10
While ("True")
  Turtle.MoveTo(GraphicsWindow.MouseX,GraphicsWindow.MouseY)
  count = count+1
  If (count > 0) Then
    Shapes.Remove("_turtleLine"+count)
  EndIf
  Program.Delay(100)
EndWhile
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)