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

Get object by name

Hi,
i get a NULLReferenceException when executing this:

CheckBox aBox = new CheckBox();
aBox = this.GetType().GetField("checkBox9").GetValue(this ) as CheckBox;

I have a Checkbox with the name checkBox9, and other code snippets gets
the object the same way.
Any ideas to get the object on an other way?

Thank You
regards
Alex

May 23 '06 #1
5 4587
You probably need to use the overloaded form of GetField to search for
private fields; IIRC the standard overloads only search for public members.

Marc
May 23 '06 #2
Example:

using System;
using System.Reflection;
using System.Diagnostics;

class Program {
private string _myField = "abc";
public void Test() {
FieldInfo fi = GetType().GetField("_myField");
Debug.WriteLine(fi == null ? "<null>" :
fi.GetValue(this).ToString());
fi = GetType().GetField("_myField", BindingFlags.NonPublic |
BindingFlags.GetField | BindingFlags.Instance);
Debug.WriteLine(fi == null ? "<null>" :
fi.GetValue(this).ToString());

}
public static void Main() {
new Program().Test();
}
}
May 23 '06 #3
Hi,

"alex" <ea***********@freenet.de> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi,
i get a NULLReferenceException when executing this:

CheckBox aBox = new CheckBox();
aBox = this.GetType().GetField("checkBox9").GetValue(this ) as CheckBox;

I have a Checkbox with the name checkBox9, and other code snippets gets
the object the same way.
Any ideas to get the object on an other way?


Is this a web or a win app?
In web app you can do this.FindControl( ControlName );

In a win app it does change GetField does work ONLY if the control is
defined as a field, IF the control is added dynamically it will not appear.
I don't think you have other option but do a search in the Controls
collection
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


May 23 '06 #4
Thanks for the tipps.
I changed the checkboxes from private to public
in the form.cs.
This is just a work around but it works for me

Thanks and
regards
Alex

May 23 '06 #5
Hi,

not sure how this can help though.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"alex" <ea***********@freenet.de> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Thanks for the tipps.
I changed the checkboxes from private to public
in the form.cs.
This is just a work around but it works for me

Thanks and
regards
Alex

May 24 '06 #6

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
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...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
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...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.