Resolved Is an object's name ever used?

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
Compiler did not complain about the following.

ControlDirAndFileExplorer_1.Name = "&$ControlDirAndFileExplorer_1"

If i want to code with this object I'd reference the varible's name (on the left above)

I'd call the string on the right the object's name.
What would you call t?
But what use is it? Is it ever used programiticaly?
What are the rules about its format.


P.S.
This site use to spell chek my post.
Do you know why it nolonger dose that.


I meant to post on VB.NET. Please move if you can.
 
Last edited:
Solution
See the Remark section in documentation about one use case: Control.Name Property (System.Windows.Forms) | Microsoft Learn

Here is another, looking up a control in the Controls collection by name: Control.ControlCollection.Item[] Property (System.Windows.Forms)
Similar can also be seen with TreeView.Nodes.Find where the string key to search is the TreeNode.Name.

You can have many variables referencing the same object, so those are just variables and not the name of the control. When you add a control in designer a field (variable) is generated based on the name, since it makes sense to use that later in code and know what is what. There are rules for names of such identifiers: Declared Element Names - Visual Basic...

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,460
Location
Norway
Programming Experience
10+
See the Remark section in documentation about one use case: Control.Name Property (System.Windows.Forms) | Microsoft Learn

Here is another, looking up a control in the Controls collection by name: Control.ControlCollection.Item[] Property (System.Windows.Forms)
Similar can also be seen with TreeView.Nodes.Find where the string key to search is the TreeNode.Name.

You can have many variables referencing the same object, so those are just variables and not the name of the control. When you add a control in designer a field (variable) is generated based on the name, since it makes sense to use that later in code and know what is what. There are rules for names of such identifiers: Declared Element Names - Visual Basic
I meant to post on VB.NET. Please move if you can.
They are different sites, so no.
 
Solution

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
I was suprised that the compiler allowed "&$ControlDirAndFileExplorer_1"
You shouldn't be. It's just a string. The compiler wouldn't look at the contents of that string any more than it would any other. If there is any validation to be done on the contents of a string property value, that would be done at run time, not at compile time. You couldn't use that name in the designer because it could not be used as an identifier, i.e. as a field name. Using it at run time has no such limitation.
 
Top Bottom