473,385 Members | 1,548 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.

Does C# have a way to do something like the EVAL() function in Jav

I am wondering if anyone has done something like this? I want to find a label
based on the passed in information. It would be awesome to find something
like this:

private void myvoid (int i)
{
string lbl = "label" + i;
Label mylabel = (Label)lbl;

lbl.Text = "I have found the label on my form by this method!";
}

Anyone got a method?
Thanks!
Nov 17 '05 #1
4 1336
Rob,

There is, but it isn't easy. It would basically require you to create
complete class definitions in code, and then compile them at runtime and
access them. If the classes don't implement some sort of interface, then
you are going to have to use Reflection to get all of your results.

All in all, I would recommend against trying to do this. What are you
trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rob Lykens" <Ro*******@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
I am wondering if anyone has done something like this? I want to find a
label
based on the passed in information. It would be awesome to find something
like this:

private void myvoid (int i)
{
string lbl = "label" + i;
Label mylabel = (Label)lbl;

lbl.Text = "I have found the label on my form by this method!";
}

Anyone got a method?
Thanks!

Nov 17 '05 #2
Is something like that you want??
private object FindObject (string strObjectName, string strObjectType)
{
foreach (Control controlObj in this.Controls)
{
if ((controlObj.GetType().ToString() == strObjectType) && (controlObj.Name
== strObjectName))
{
return controlObj;
}
}
return null;
}

private void button1_Click(object sender, System.EventArgs e)
{
object obj = FindObject (textBox1.Text, "System.Windows.Forms.Label");
if (obj != null)
{
MessageBox.Show(((Label)obj).Text);
}
else
{
MessageBox.Show("object not found");
}
}
"Rob Lykens" wrote:
I am wondering if anyone has done something like this? I want to find a label
based on the passed in information. It would be awesome to find something
like this:

private void myvoid (int i)
{
string lbl = "label" + i;
Label mylabel = (Label)lbl;

lbl.Text = "I have found the label on my form by this method!";
}

Anyone got a method?
Thanks!

Nov 17 '05 #3
Well Done Daniel! Well done!

I wanted to see if I could set this object to the passed in string without
an iteration through the controls array, but it looks like it's the only way.

Thanks for the sample code. That was fun.

Cheers!

Rob

P.S. Interesting that the MVP goes way out of the way to do something fairly
simple here... Hmmmmmmm...

"Daniel Santana" wrote:
Is something like that you want??
private object FindObject (string strObjectName, string strObjectType)
{
foreach (Control controlObj in this.Controls)
{
if ((controlObj.GetType().ToString() == strObjectType) && (controlObj.Name
== strObjectName))
{
return controlObj;
}
}
return null;
}

private void button1_Click(object sender, System.EventArgs e)
{
object obj = FindObject (textBox1.Text, "System.Windows.Forms.Label");
if (obj != null)
{
MessageBox.Show(((Label)obj).Text);
}
else
{
MessageBox.Show("object not found");
}
}
"Rob Lykens" wrote:
I am wondering if anyone has done something like this? I want to find a label
based on the passed in information. It would be awesome to find something
like this:

private void myvoid (int i)
{
string lbl = "label" + i;
Label mylabel = (Label)lbl;

lbl.Text = "I have found the label on my form by this method!";
}

Anyone got a method?
Thanks!

Nov 17 '05 #4
If you know the ID of the control you are looking for you do not need to
iterate through the controls collection.

There is a FindControl method that does this for you.

Mike Hanson
MCSD, MCSE+I, MCDBA, SCP

"Rob Lykens" wrote:
Well Done Daniel! Well done!

I wanted to see if I could set this object to the passed in string without
an iteration through the controls array, but it looks like it's the only way.

Thanks for the sample code. That was fun.

Cheers!

Rob

P.S. Interesting that the MVP goes way out of the way to do something fairly
simple here... Hmmmmmmm...

"Daniel Santana" wrote:
Is something like that you want??
private object FindObject (string strObjectName, string strObjectType)
{
foreach (Control controlObj in this.Controls)
{
if ((controlObj.GetType().ToString() == strObjectType) && (controlObj.Name
== strObjectName))
{
return controlObj;
}
}
return null;
}

private void button1_Click(object sender, System.EventArgs e)
{
object obj = FindObject (textBox1.Text, "System.Windows.Forms.Label");
if (obj != null)
{
MessageBox.Show(((Label)obj).Text);
}
else
{
MessageBox.Show("object not found");
}
}
"Rob Lykens" wrote:
I am wondering if anyone has done something like this? I want to find a label
based on the passed in information. It would be awesome to find something
like this:

private void myvoid (int i)
{
string lbl = "label" + i;
Label mylabel = (Label)lbl;

lbl.Text = "I have found the label on my form by this method!";
}

Anyone got a method?
Thanks!

Nov 17 '05 #5

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

Similar topics

9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
2
by: John | last post by:
The following code works OK in IE 6.0 but does not work in Netscape 7. The image does not shift when one scrolls down but stays stationary in Netscape. Please help Thank you John function...
7
by: Todd Cary | last post by:
I inherited an application where the Flyout works in Firefox but not in IE. My JavaScript background is sparse, so I am not sure how to approach the problem. Any suggestions are welcomed! The...
4
by: namemattersnot | last post by:
Re, SCENARIO: User clicks a link and a javascript function creates dynamic content in <div id="content"></div> using: document.getElementById("content").innerHTML = something In the page's...
16
by: nephish | last post by:
Hey there, i have been learning python for the past few months, but i can seem to get what exactly a lamda is for. What would i use a lamda for that i could not or would not use a def for ? Is...
2
by: Jim Carr | last post by:
Upon entering the site www.FutureByDesign-Music.com with IE6, my clipboard is erased and then disabled in all other Windows XP applications. Navigating to another site returns clipboard...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
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: 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...

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.