473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Assigning object names dynamically

GT
Hey

Is it possible to assign object names dynamically?
That is, when for instance creating a Label object

Label label1 = new Label();

can then the name of the Label object (label1) be assigned
by calling a method supplying the name?
Oct 17 '06 #1
3 2476
Actually, "label1" is NOT the name of the object; that is the local name of
a variable *pointing* at the object. Note that once compiled to IL,
variables don't have names.

The Label class probably does have a .Name property (depending on which
"Label" class this is...); this can be anything you want. When using the IDE
to place e.g. Controls onto Forms, then yes: the IDE keeps the two
identical. But that is only for the designer's benefit, not yours.

So yes, just: label1.Name = "something";

Marc
Oct 17 '06 #2
GT
Ok, thanks. But does using the 'name' property really enable you to refer to
the
object using the supplied name?
If you write
label1.Name = "something";

then I want to use the name "something" (which is a string and can be called
from somewhere else) and not "label1" (which is the assigned name of the
pointer to the object when creating the object) to refer to the Label object.

"Marc Gravell" wrote:
Actually, "label1" is NOT the name of the object; that is the local name of
a variable *pointing* at the object. Note that once compiled to IL,
variables don't have names.

The Label class probably does have a .Name property (depending on which
"Label" class this is...); this can be anything you want. When using the IDE
to place e.g. Controls onto Forms, then yes: the IDE keeps the two
identical. But that is only for the designer's benefit, not yours.

So yes, just: label1.Name = "something";

Marc
Oct 17 '06 #3
In short, no. You can, however, use Controls.Find() to achieve something
similar, but you will have to cast it yourself.

What you are talking about is the fields on the class, but fields are fixed
at compile time.

Marc
Oct 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ben | last post by:
Hi all, This may sound easy but I'm having trouble assigning values to array element. My problem is as follows: m = for o in m: # Here first o is 'Peter'.. I want to do something like this:...
22
by: ajay | last post by:
Why would a new of object be created without assigning it to any of variable? new A; ??? tx
4
by: vertigo | last post by:
Hello I need to create some objects durring program execution - but it's names are dynamically generated (depends on parameters). How can i do it ? Thanx Michal
3
by: Ricky W. Hunt | last post by:
I came across something by accident; assigning one object to another (as in object1 = object2) allows me to "work on" object2 just like it was object1. For instance, in order to be able to...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
3
by: teddarr | last post by:
I have an assignment I am working on that requires me to declare a new object (object 5, r5)and initialize it by assigning it a previous object (object 2, Rectangle r2(7.1, 3.2);). How do I do...
1
by: tsorgi | last post by:
I'm writing a data conversion application to support a system migration. The code is reading formatted text files and loading them into MS SQL 2005 database tables. I wanted to call the existing...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.