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

Retrieve instance name using Reflection

JW
Dear NG,

I am writing a .NET CF forms app. that needs to dynamically configure
buttons on a form. Since the forms designer doesn't like component arrays I
am using ordinary instance names for each button (btn0, btn1 etc.) as
private members of the form class, but need to reference these from a loop
that is iterating through the button config. information (array).

My question is how can I loop through a collection of buttons with names
like "btn0, btn1 etc." calling methods on each as the loop executes?
Pseudo-CS would look something like:

string[] buttonNames = new string[]{"foo", "bar", "etc"};
Button current;
for (int i=0; i<buttonNames.Length; i++)
{
current = this.ReflectionMagic.GetInstance("btn" + i);
current.Text = buttonNames[i];
}

Please note that I have diligently RTFM & STFW to no avail, and so any help
would be greatly appreciated.

Thanks,

James.

PS. Please remove anti-spam for email replies.
Nov 16 '05 #1
2 3665
James,
string[] buttonNames = new string[]{"foo", "bar", "etc"};
Button current;
for (int i=0; i<buttonNames.Length; i++)
{
current = this.ReflectionMagic.GetInstance("btn" + i);
current.Text = buttonNames[i];
}


Do you have to involve the name (as a string)? How about

Button[] buttons = {btn0, btn1};
foreach ( Button current in buttons )
{
...
}


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
JW
Mattias,

Thanks for your reply. It would seem that my example simplified the problem
a little too much.

Along the lines of the MVC design pattern, a FormsMarshall class requests a
value object containing form configuration information (including a string
array containing button text information) and passes that object to a
generic form. The form constructor then needs to be able to loop through
the array assigning the text to the buttons according to the array index.

Having said all this, on reflection (pun intended ;), your response has made
me think I am making things too complicated. I can see now that each
generic form, along with the button declarations for the form designer,
could have a button array declared at the same time for use by looping
configuration code.

Thanks again for your help.

James.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
James,
string[] buttonNames = new string[]{"foo", "bar", "etc"};
Button current;
for (int i=0; i<buttonNames.Length; i++)
{
current = this.ReflectionMagic.GetInstance("btn" + i);
current.Text = buttonNames[i];
}


Do you have to involve the name (as a string)? How about

Button[] buttons = {btn0, btn1};
foreach ( Button current in buttons )
{
...
}


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3

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

Similar topics

3
by: Angelos Karantzalis | last post by:
Hi y'all ... I've just come across a rather weird problem. i'm using the following code to get the names of the public properties of an object: public static string GetPropertyNames(object...
3
by: Antonio Paglia | last post by:
Imaginate you have one method that accept some arguments like: 1- a control 2- un Type (MyType) 3- el nombre de una Propiedad "PropertyName" Example: ...
9
by: Don | last post by:
Say I have a class like so: Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
8
by: news.microsoft.com | last post by:
How do I get the class name for a current instance. For example, if I want to know the Class Name for the current form, how do I get this programatically. Thanks
3
by: Chris Pratt | last post by:
I'm sorry to ask such a fundamental question, but is it possible to create a copy of an object in VB.Net 2005? I have an app which, on load, creates an instance of a class, into which it reads a...
4
by: =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= | last post by:
(using .net 2.0) Say you have a class structure like this: class Address .... end class class Person FirstName
2
by: =?Utf-8?B?Q2FybG9zIFNvc2EgQWxiZXJ0?= | last post by:
Hi guys, Using C# I have a class with a number of public properties and a public method. The idea is to call the method (externally), selecting one of those properties. Do some work with the...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
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: 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: 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...
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: 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
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
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,...

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.