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

Get all objects of a certain type

hi,

I have a form which contains a large number of datagridview objects.
At one point in my code, I need to validate all datagridviews except
the one I am working with. I'm actually validating a row in one
datagridview after I am sure that all other dgvs are valid. This is to
do with issues regarding child parent relationships between dgv.

What would really help me is if there was a way of filling an array
with references to all datagridviews within my form class. I could of
course have all datagridviews in an array but I'm actually using the
designer and it declares datagridviews as individual objects.

Any suggestions?

Barry.
Oct 2 '08 #1
7 7624
On Oct 2, 9:49*am, Magnus.Morab...@gmail.com wrote:
I have a form which contains a large number of datagridview objects.
At one point in my code, I need to validate all datagridviews except
the one I am working with. I'm actually validating a row in one
datagridview after I am sure that all other dgvs are valid. This is to
do with issues regarding child parent relationships between dgv.

What would really help me is if there was a way of filling an array
with references to all datagridviews within my form class. I could of
course have all datagridviews in an array but I'm actually using the
designer and it declares datagridviews as individual objects.

Any suggestions?
This is a good example of the designer generating code which (contrary
to the name!) doesn't show good design.

I suggest you create the array (or list, preferably) *anyway* and
populate it within the constructor. You'll still have the other
variables around, unfortunately, but at least you'll still have the
list available.
Of course, you'll need to hand-edit the code every time you add a new
view...

Jon
Oct 2 '08 #2
On 2 Okt, 10:57, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 2, 9:49*am, Magnus.Morab...@gmail.com wrote:
I have a form which contains a large number of datagridview objects.
At one point in my code, I need to validate all datagridviews except
the one I am working with. I'm actually validating a row in one
datagridview after I am sure that all other dgvs are valid. This is to
do with issues regarding child parent relationships between dgv.
What would really help me is if there was a way of filling an array
with references to all datagridviews within my form class. I could of
course have all datagridviews in an array but I'm actually using the
designer and it declares datagridviews as individual objects.
Any suggestions?

This is a good example of the designer generating code which (contrary
to the name!) doesn't show good design.

I suggest you create the array (or list, preferably) *anyway* and
populate it within the constructor. You'll still have the other
variables around, unfortunately, but at least you'll still have the
list available.
Of course, you'll need to hand-edit the code every time you add a new
view...

Jon
Thanks for the reply
>>Of course, you'll need to hand-edit the code every time you add a new
view...

That of course is my problem. What happens if I forget, or more
likely, those who inherit the code forget...
Oct 2 '08 #3
On 2 Okt, 10:57, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 2, 9:49*am, Magnus.Morab...@gmail.com wrote:
I have a form which contains a large number of datagridview objects.
At one point in my code, I need to validate all datagridviews except
the one I am working with. I'm actually validating a row in one
datagridview after I am sure that all other dgvs are valid. This is to
do with issues regarding child parent relationships between dgv.
What would really help me is if there was a way of filling an array
with references to all datagridviews within my form class. I could of
course have all datagridviews in an array but I'm actually using the
designer and it declares datagridviews as individual objects.
Any suggestions?

This is a good example of the designer generating code which (contrary
to the name!) doesn't show good design.

I suggest you create the array (or list, preferably) *anyway* and
populate it within the constructor. You'll still have the other
variables around, unfortunately, but at least you'll still have the
list available.
Of course, you'll need to hand-edit the code every time you add a new
view...

Jon
I guessing I can't do something along these lines in C# -

for(object obj in this.GetMyLocalVariables)
if(obj.ValueType == typeof(DataGridView)
dataGridViewList.append(obj);

Oct 2 '08 #4
On Oct 2, 10:05*am, Magnus.Morab...@gmail.com wrote:
I guessing I can't do something along these lines in C# -

for(object obj in this.GetMyLocalVariables)
* if(obj.ValueType == typeof(DataGridView)
* * dataGridViewList.append(obj);
Not for local variables. You can do it for instance variables (using
typeof(Foo).GetFields) though... that may be good enough.
Or if they're added to the Controls collection, you could just iterate
through that.

Jon
Oct 2 '08 #5
I have similar requirements regularly - I just use a generic method to
recurse the Controls collection - something like:

class MyForm : Form
{
void SomeEvent(object sender, EventArgs args)
{
this.ApplyChildren<DataGridView>(dgv =>
{
// if not the current instance...
if (!ReferenceEquals(dgv, sender))
{
// ...validate this dgv
}
});
}
}

static void ApplyChildren<T>(this Control root, Action<Taction)
where T : Control
{
foreach (Control child in root.Controls)
{
ApplyChildren(child, action);
T typed = child as T;
if (typed != null) action(typed);
}
}

Marc
[C# MVP]
Oct 2 '08 #6
On 2 Okt, 12:23, Marc Gravell <marc.grav...@gmail.comwrote:
I have similar requirements regularly - I just use a generic method to
recurse the Controls collection - something like:

* * * * *class MyForm : Form
* * * * *{
* * * * * * *void SomeEvent(object sender, EventArgs args)
* * * * * * *{
* * * * * * * * *this.ApplyChildren<DataGridView>(dgv =>
* * * * * * * * *{
* * * * * * * * * * *// if not the current instance....
* * * * * * * * * * *if (!ReferenceEquals(dgv, sender))
* * * * * * * * * * *{
* * * * * * * * * * * * *// ...validate this dgv
* * * * * * * * * * *}
* * * * * * * * *});
* * * * * * *}
* * * * *}

* * * * *static void ApplyChildren<T>(this Control root, Action<Taction)
* * * * * * *where T : Control
* * * * *{
* * * * * * *foreach (Control child in root.Controls)
* * * * * * *{
* * * * * * * * *ApplyChildren(child, action);
* * * * * * * * *T typed = child as T;
* * * * * * * * *if (typed != null) action(typed);
* * * * * * *}
* * * * *}

Marc
[C# MVP]
Thanks for your help. Heres what I've ended up doing -

public Form1()
{
InitializeComponent();

List<DataGridViewdataGridViews = new
List<DataGridView>();
List<Controlcontrols = new List<Control>();

GetControls(this, ref controls);

foreach(Control control in controls)
{
if (control.GetType() == typeof(DataGridView))
dataGridViews.Add((DataGridView)control);
}
this.dataGridViews = dataGridViews.ToArray();
}

void GetControls(Control parentControl, ref List<Control>
controls)
{
foreach (Control control in parentControl.Controls)
{
controls.Add(control);
GetControls(control, ref controls);
}
}
Oct 2 '08 #7
Note that the "ref" here does nothing - it would work just as well
without it ("ref" would be handy if GetControls said "controls = new
List<Control>()").

Marc
Oct 2 '08 #8

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

Similar topics

2
by: Beta What | last post by:
I am seeing lots of posts (and websites like Chris Torek's) that claim that objects have type. They are wrong. Objects have no type, so everyone please stop spreading misinformation. Only...
8
by: CA | last post by:
Hi, I have a function where I would like to test whether an object is of a certain type. Here is my code so far. public bool HasValidType(Type t, object val) { try { if (t==typeof(double))
2
by: nford | last post by:
Is there a way to check if an object handles a certain type of event or if the object is hooked up to a certain event handler/call back function? For instance what would be the equivalent syntax...
0
by: Udo | last post by:
Hello, I want to use COM for automating an app via Python. On my PC are two versions of that app installed, therefore I have two type libraries. My questions are: 1. I had to manually create...
1
by: baburk | last post by:
SELECT name FROM sys.objects WHERE type = 'P' Here P denotes procedure. Then what for table. What are the type available?
6
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, Imagine, that I've got a string variable containing a value of each possible value type (string, int32, double, ...). Now I want to convert the string to an object of the right type,...
1
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, does anyone know, if it is possible (via reflection?) to instantiate a multidimensional array of a certain type, which is first known at run time? e.g. at runtime I have to create an...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
1
by: SvenV | last post by:
Hello, I'm creating an application and want to build some kind of logging system. I want to first create my list of events in database with a certain code. Then when the operation occurrs I want...
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
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
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...
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.