A job for reflection? Or is there a better way?

  • Thread starter Thread starter Gary Morris
  • Start date Start date
G

Gary Morris

Hello all,

This is my situation. I'm reading an XML file with a variable number of
nodes.
So far, all of the code has been reasonably easy, but now I'm up against a
wall trying to figure out the best way to proceed.

Upon reading in a valid node, I'm wanting to add a tabpage to a tab control
based on the name value of the node. Since all of the tabpages will have the
same child controls, I customized the tabpage and made a class that uses
tabpage as the base class and put all of the controls I need on it. This
works
great. Everytime I need a new page, I just instantiate a new instance and
add
it to the tab control. The problem now is naming the pages. I thought of an
array, which would be nice, but there won't always be the same number of
valid nodes. Not only that, but the user may want to add more nodes while
working with the files. When a new node is created, it gets it's own
tabpage,
so a fixed array is not a good option. My idea was to just name each tabpage
with the name of the node, but I am having a hard time with that. I did read
up on reflection, and that might be the best way to go, I just don't know
how
to do it. If anyone can help with ideas or other options for doing this, I'd
be
all ears. Alternatively, if someone knows a good article or an example of
how
this is done, please point me to it.

What I need to be able to do is something like:

XmlTabPage <Node.Name> = new XmlTabPage();
where <Node.Name> is the Node's actual text name.

I will be using the Tag property to index and refer to the tabpages, so the
only
time I need to do this is on creation.

Thanks.
 
What I need to be able to do is something like:

XmlTabPage <Node.Name> = new XmlTabPage();
where <Node.Name> is the Node's actual text name.

I don't see why you actually *do* need to do that. There's no need to
have the actual *variable* with the same name.

I suggest you create the tab page and just put it in a Hashtable, using
the node name as the key.
 
Jon Skeet said:
I don't see why you actually *do* need to do that. There's no need to
have the actual *variable* with the same name.

OK, I don't actually have to have THE node name, I just want some way
of creating each instance of the tabpage with a unique name. Since I've
never had the need to do anything like this, I am looking for any option.
I suggest you create the tab page and just put it in a Hashtable, using
the node name as the key.

Never used a hashtable, though I've seen many programs and code
snippets that do use them. Maybe it's time to look into it....???
 
Gary Morris said:
OK, I don't actually have to have THE node name, I just want some way
of creating each instance of the tabpage with a unique name. Since I've
never had the need to do anything like this, I am looking for any option.

Objects don't necessarily *have* names. Usually I find when people want
to give objects names, they're really after some way of retrieving an
object by a string key later on. That's where hashtables come in
useful.
Never used a hashtable, though I've seen many programs and code
snippets that do use them. Maybe it's time to look into it....???

That would be a good idea. I suggest you familiarize yourself with
ArrayLists as well Hashtables before going much further - they're both
pretty fundamental to writing many .NET applications. If you look up
"collection classes" in the MSDN index, you'll see there's a tutorial
and a couple of samples available. I haven't gone through them myself,
but reading them (and/or a Collections chapter in a .NET book) would be
a good idea.
 
option.

Objects don't necessarily *have* names. Usually I find when people want
to give objects names, they're really after some way of retrieving an
object by a string key later on. That's where hashtables come in
useful.

What I'm wondering now is, when I create a tabpage will I not need to use
a member variable with the "TabPage somename = new TabPage();" type
of construct? That's what I am hung on. I did notice that I could use the
same "name" in an event (like a button or menu) and it will go ahead and
create a new control without any error, but does that not confuse things
later on? All of the new controls look like clones, so how do I determine
who
is what? How does the program know who is what? I hope that makes sense.

In other words:
You can't say "int i = new int()" but one time in any given scope or the
compiler complains about it and won't compile. Wouldn't the same hold true
for any member? If I have 5 tabpages all created with "TabPage newTab =
new TabPage()" will that actually work? This is what I am hung on more than
anything.
That would be a good idea. I suggest you familiarize yourself with
ArrayLists as well Hashtables before going much further - they're both
pretty fundamental to writing many .NET applications. If you look up
"collection classes" in the MSDN index, you'll see there's a tutorial
and a couple of samples available. I haven't gone through them myself,
but reading them (and/or a Collections chapter in a .NET book) would be
a good idea.

I am familiar with both, just have never used them. I'm sure I've written
code
that would have benefited from one or the other, I just haven't seen the
need
until now!

And thanks for the quick reply. This could mean a good coding job for me if
I
can show that I know what I'm doing (and most of the time I do - I've been
programming since the Commodore, TI-994A, Atari XL, GWBASIC days). It
just sometimes overwhelms me, .NET is so huge.
 
Gary Morris said:
What I'm wondering now is, when I create a tabpage will I not need to use
a member variable with the "TabPage somename = new TabPage();" type
of construct? That's what I am hung on. I did notice that I could use the
same "name" in an event (like a button or menu) and it will go ahead and
create a new control without any error, but does that not confuse things
later on? All of the new controls look like clones, so how do I determine
who is what? How does the program know who is what? I hope that makes sense.

Just use a local variable. You can reuse that local variable. For
instance:

TabPage foo = new TabPage();
table["somename"] = foo;
foo = new TabPage();
table["someothername"] = foo;

In fact, you can get away without having a variable there at all:

table["somename"] = new TabPage();
table["someothername"] = new TabPage();
In other words:
You can't say "int i = new int()" but one time in any given scope or the
compiler complains about it and won't compile. Wouldn't the same hold true
for any member? If I have 5 tabpages all created with "TabPage newTab =
new TabPage()" will that actually work? This is what I am hung on more than
anything.

I think you've got fundamental problems in terms of understanding what
a variable is, and its scope. I would stop your current project for the
moment and get back to basics. Unfortunately it's a big topic to
discuss in a newsgroup post - I suggest you read a tutorial or C# book.
I am familiar with both, just have never used them. I'm sure I've written
code that would have benefited from one or the other, I just haven't seen the
need until now!

And thanks for the quick reply. This could mean a good coding job for me if
I can show that I know what I'm doing (and most of the time I do - I've been
programming since the Commodore, TI-994A, Atari XL, GWBASIC days). It
just sometimes overwhelms me, .NET is so huge.

But I still think you need to go back to basics - it's clear there's
some conceptual confusion there which needs clearing up, to be honest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top