473,407 Members | 2,315 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,407 software developers and data experts.

Dynamically Reference object name

I am trying to dynamically create the object name..... but i can't get
it to work...
From This (works)

------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();
To This (errors)
----------------------------------------------------------------
for (int i = 1,i<6,i++)
{
label+i.Text = newDeck[i-1].ToString();
}

May 30 '06 #1
3 1838
TheLostLeaf wrote:
I am trying to dynamically create the object name..... but i can't get
it to work...
From This (works)

------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();
To This (errors)
----------------------------------------------------------------
for (int i = 1,i<6,i++)
{
label+i.Text = newDeck[i-1].ToString();
}


You will need to reference the label by name or index.

ie this.Controls["label" + i.ToString()"].Text = ...

I am not sure if the string indexer is new to .net 2.0 so you might have
to iterate through the controls to find the one that matches based on
the name if using .net 1.*.
HTH
JB
May 30 '06 #2
Works perfect! Thanks a bunch!

May 30 '06 #3

Hi John
I have the "same" problem, but I'll use this for ASPX application
I use .Net 2.0 and this doesn't work... could you help me ?

Thanks in advance.
//Luca
"John B" wrote:
TheLostLeaf wrote:
I am trying to dynamically create the object name..... but i can't get
it to work...
>From This (works)
------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();
To This (errors)
----------------------------------------------------------------
for (int i = 1,i<6,i++)
{
label+i.Text = newDeck[i-1].ToString();
}

You will need to reference the label by name or index.

ie this.Controls["label" + i.ToString()"].Text = ...

I am not sure if the string indexer is new to .net 2.0 so you might have
to iterate through the controls to find the one that matches based on
the name if using .net 1.*.
HTH
JB
Jul 21 '06 #4

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

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
9
by: pablo | last post by:
Dear NGers, I would like to change the alt-text with the changing of the image during a mouseover action. Can document.images.altView be changed dynamically? TIA, pablo
4
by: DotNetJunkies User | last post by:
Hi, Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
3
by: Tom | last post by:
Hi All : I'm VB.Net Beginner I try to create object Dynamically : In my testing, I create 2 .net project One is MyApp (Type is windows application) with one button name "Button1" The another...
7
by: mef526 | last post by:
I would like to reference a dynamically created control and I know the name. I would like to use the following: Dim strName as String = "txtControl1" ' This is the ".Name" used when textbox was...
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.