473,395 Members | 1,452 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,395 software developers and data experts.

C# Help - Casting Question

Let's say that I have the following:

String name = "lblDescription";

The string assigned to the name variable is actually a label control on my
form with that name. Is there away in C# to the name and "cast" it to the
actual label control with that name?

Label l = (Label) name;

I know it doesn't work but can it be done?

Thanks,

Yosh
Nov 16 '05 #1
3 1282
to my knowledge this can't be done. Maybe I'm not understanding your
question. Do you want to assign the "Text" of the label?

--
Lateralus [MCAD]
"Yoshi" <yo***@home.com> wrote in message
news:1I*****************@fe2.texas.rr.com...
Let's say that I have the following:

String name = "lblDescription";

The string assigned to the name variable is actually a label control on my
form with that name. Is there away in C# to the name and "cast" it to the
actual label control with that name?

Label l = (Label) name;

I know it doesn't work but can it be done?

Thanks,

Yosh

Nov 16 '05 #2
Yoshi wrote:
Let's say that I have the following:

String name = "lblDescription";

The string assigned to the name variable is actually a label control on my
form with that name. Is there away in C# to the name and "cast" it to the
actual label control with that name?

Label l = (Label) name;

I know it doesn't work but can it be done?

Thanks,

Yosh

You can iterate through the controls on your form, checking the name
each time to see if it matches your string.

ie
foreach(Control ctl in this.Controls)
{
if(ctl.Name == "LabelName")
{
MyLabel = (Label)ctl;
break;
}
}

HTH
JB
Nov 16 '05 #3
Hi Yoshi,

Your question is not very clear...:(

Are you trying to find out the control with the name as specified in the
'name' string variable?
Ans: Iterate thru the controls listed in the form's Controls
collection....for each of them check whether the name property is equal to
'name' string. If yes, cast it to a Label and do your implementation.

Or are you simply trying to assign the value of the variable 'name' to the
Label control 'l'?
Ans: l.Text = name

HTH,

- Rakesh

"Yoshi" wrote:
Let's say that I have the following:

String name = "lblDescription";

The string assigned to the name variable is actually a label control on my
form with that name. Is there away in C# to the name and "cast" it to the
actual label control with that name?

Label l = (Label) name;

I know it doesn't work but can it be done?

Thanks,

Yosh

Nov 16 '05 #4

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

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.