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

How to get an Object by it's name?

What I want to do:
I want to have a class which is reading an .ini file and manipulate the
application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Enc oding.UTF8,false);
string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with
other objects, like sqlConnectionString, or sqlSelectStatements, cause
they are no Controlls.
QUESTION:
Is there a way to get thru all objects in my Application, like i do it
with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!
Nov 16 '05 #1
3 1507

Michael Stahl wrote:
What I want to do:
I want to have a class which is reading an .ini file and manipulate the application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Enc oding.UTF8,false); string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with other objects, like sqlConnectionString, or sqlSelectStatements, cause they are no Controlls.
QUESTION:
Is there a way to get thru all objects in my Application, like i do it with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!

Reflection should do the trick

Nov 16 '05 #2
You could use reflection to go through all *types* in the application maybe but not all objects. No-one (except the runtime) keeps a list of all the allocated objects. If you want to do this you'll have toio store them in a hashtable or something similar. The problem though is that the hashtable will keep the objects alive and so they won't be GC'd so in fact you heed a hash table whose value (of the key/value pair) is a WeakReference and have special handling for eliminating entries with null values.

Regards

RIchard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Michael Stahl wrote:
What I want to do:
I want to have a class which is reading an .ini file and manipulate the application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new System.IO.StreamReader(INIFilePath,System.Text.Enc oding.UTF8,false); string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with other objects, like sqlConnectionString, or sqlSelectStatements, cause they are no Controlls.
QUESTION:
Is there a way to get thru all objects in my Application, like i do it with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!

Reflection should do the trick
Nov 16 '05 #3
voltaire schrieb:
Michael Stahl wrote:
What I want to do:
I want to have a class which is reading an .ini file and manipulate


the
application with the data from the ini file.

What I have now:
An ini file:

[textBox1]
Text=Hallo
Dock=none
ForeColor=red

A class with:

object iwill=null;
parentType = this.GetType();
System.IO.StreamReader sr=
new


System.IO.StreamReader(INIFilePath,System.Text.Enc oding.UTF8,false);
string str="";
while(sr.Peek()>=0)
{
str=sr.ReadLine().Trim();
//Get the Control I want to change
if(str.StartsWith("["))
{
foreach(Control con in this.Controls)
{
if("["+con.Name.ToString()+"]"==str)
{
iwill=con;
}
}
}
Type myType = iwill.GetType();
//Get the Property I want to change
PropertyInfo pi =myType.GetProperty(str.Split('=')[0]);
if( pi != null )
{
object result=null;
TypeConverter converter=
TypeDescriptor.GetConverter(pi.PropertyType);
result =converter.ConvertFrom(str.Split('=')[1]);
//Change the property
pi.SetValue(iwill,result,null);
}
}

With this i could change textBox1 properties like Dock, Text...
This is working with all my controlls in my Application, but not with


other objects, like sqlConnectionString, or sqlSelectStatements,


cause
they are no Controlls.
QUESTION:
Is there a way to get thru all objects in my Application, like i do


it
with the Controls in my example?
Must I do something completely different?

Hope someone knows what to do!!


Reflection should do the trick

I tried it with Reflection, and i get the objects, but how would i write
the line
pi.SetValue();
If i put the found object in
pi.SetValue(foundObject,string,null);
i get an
"Object type cannot be converted to target type"
cause foundObject is a Reflection Type not the Object that i want to change
Nov 16 '05 #4

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

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
11
by: Oleg Alistratov | last post by:
Is in javascript a method to get object's identifier as string? E.g., may I write function GetName thus, that var v; var s = GetName(v); has led to s == "v"
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
3
by: Brad | last post by:
I have several movies being presented to the web on a single page and each one is dependant on a link selected. On the .aspx page I have the following code for a single movie. ...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
1
by: gcmartijn | last post by:
I'm trying to extract something like this: <object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.