Instancing classes with a combination of string and integer possible?

C

Chicken15

Hi Group.

First of all I'm sorry for asking (maybe) such easy questions. But I'm
quite stuck now and couldn't come up with a solution by using my C#
book or googling. So it would be nice if someone could help me out.

Now first of all I want to describe the setting in a simpler (I'm using
Visual Studio 2005):
Form: One comboBox (dropdown list) and 2 buttons.
Classes: Class1 with a function do() which plainly returns a string.

Ok, what do I want to do with these?
The comboBox should show all current instances of Class1 (even if some
are instanced at runtime).
Button1 should create a new instance of Class1 with a fixed
instancename + a number (e.g. there is a string with the value
"testclass" and an increasing integer "i". Instances should be created
with a name combined from both -> string+integer -> testclass1 ,
testclass2 etc.)
Button2 should call a function from the currently in the comboBox
selected class. (e.g. "testclass2" is selected in the list -> click
calls "testclass2.do()" )

So my problems with this setting are:
- Is it possible in any way to instance a class with a name combined of
string and an integer? Maybe with some kind of boxing?
- Keeping the comboBox updated. Maybe ILists are the way to go here?
- Calling functions of the currently selected class in the comboBox.

Hope it gets clear with this explanation.
Thanks alot.
Jürgen
 
S

Stoitcho Goutsev \(100\)

Hi,

The question here is what do you mean by "instantiating a class with a given
name". Instances of a type doesn't have names per se. When we talk about
names in this context they could be: names of a variable that keeps
reference to the object in the memory, name of the class that needs to be
instantiated or a name (ID) that is stored in some property or field inside
the object as part of the internal state.

1. Name of the variable that keeps reference to the object in the memory -
it is pretty obvious that you cannot create a method field at runtime, but
what you can do is to have a key/value structure where you register all
created instnaces of given class under some name (in your case
string+integer). Good candiate for this would be a Hastable, Dictionary<> or
some other key/value data structure. Then you can look up an object by its
name and call methods on it.

2. Name of a class - you can instantiate objects by a class name as a string
using reflection or the Activator.CreateInstnace. Here you can also use
object factory design pattern.


3. The name (ID) is stored in the object internal state. In this case you
can use agian key/value data structure to map names to objects.



--
HTH
Stoitcho Goutsev (100)

Hi Group.

First of all I'm sorry for asking (maybe) such easy questions. But I'm
quite stuck now and couldn't come up with a solution by using my C#
book or googling. So it would be nice if someone could help me out.

Now first of all I want to describe the setting in a simpler (I'm using
Visual Studio 2005):
Form: One comboBox (dropdown list) and 2 buttons.
Classes: Class1 with a function do() which plainly returns a string.

Ok, what do I want to do with these?
The comboBox should show all current instances of Class1 (even if some
are instanced at runtime).
Button1 should create a new instance of Class1 with a fixed
instancename + a number (e.g. there is a string with the value
"testclass" and an increasing integer "i". Instances should be created
with a name combined from both -> string+integer -> testclass1 ,
testclass2 etc.)
Button2 should call a function from the currently in the comboBox
selected class. (e.g. "testclass2" is selected in the list -> click
calls "testclass2.do()" )

So my problems with this setting are:
- Is it possible in any way to instance a class with a name combined of
string and an integer? Maybe with some kind of boxing?
- Keeping the comboBox updated. Maybe ILists are the way to go here?
- Calling functions of the currently selected class in the comboBox.

Hope it gets clear with this explanation.
Thanks alot.
Jürgen
 
D

Dave Sexton

Hi Jürgen,

Here's one possbility:

Use the ComponentModel classes to your advantage by making class1 a
Component.

Another class (an ISite implementation) keeps track of the name for a single
component, an IContainer implementation (Container or a derived type, for
instance) keeps track of all of the instances of the component, and a naming
service (INameCreationService implementation or a custom interface) can
create and validate component names. The GUI is a simple matter of using
reflection to gather a list and invoke members on the selected class when a
button is clicked.

1. You could add a static field to class1 to keep track of all instances.
It could be a read-only instance of the System.ComponentModel.Container
class.
Note: you may want to create a class1Manager class that does all this
instead of using a static field on class1, but it will work either way.
2. class1 could derive from System.ComponentModel.Component, which will
provide two useful features:
A. Site property
I. Returns an ISite implementation that provides the classes unique
name within the context of your Container.
B. Canonical disposable pattern
I. When the class is disposed remove it from the static container.
3. In the constructor of class1 add the instance to the static container and
it will automatically create an ISite for it (assuming that you're using the
Container class).
4. Create a naming service that can be exposed through the class1.GetService
method (inherited from Component), which you can use to create a new name
for the Site of each new class within the Container. You can use the
System.ComponentModel.Design.Serialization.INameCreationService interface as
a foundation if you'd like, or create your own.
5. Hook them up together so that when adding a new component without
specifying a name the naming service will provide a default name such as,
[[ componentType.Name + (container.Components.Count + 1) ]], or something
like that.

There's more to it, but hopefully you'll have a place to start.

As for the GUI:

1. Use reflection to create a list of strings that contains the names of
each component in the static container
2. Bind the list to the ComboBox or just iterate each component, adding a
new item to the list for each [[ component.Site.Name ]].
3. Use reflection to invoke a member on the selected class, which can be
retrieved from the static container by name.

That's the gist of one possible solution.

HTH

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

Hi Group.

First of all I'm sorry for asking (maybe) such easy questions. But I'm
quite stuck now and couldn't come up with a solution by using my C#
book or googling. So it would be nice if someone could help me out.

Now first of all I want to describe the setting in a simpler (I'm using
Visual Studio 2005):
Form: One comboBox (dropdown list) and 2 buttons.
Classes: Class1 with a function do() which plainly returns a string.

Ok, what do I want to do with these?
The comboBox should show all current instances of Class1 (even if some
are instanced at runtime).
Button1 should create a new instance of Class1 with a fixed
instancename + a number (e.g. there is a string with the value
"testclass" and an increasing integer "i". Instances should be created
with a name combined from both -> string+integer -> testclass1 ,
testclass2 etc.)
Button2 should call a function from the currently in the comboBox
selected class. (e.g. "testclass2" is selected in the list -> click
calls "testclass2.do()" )

So my problems with this setting are:
- Is it possible in any way to instance a class with a name combined of
string and an integer? Maybe with some kind of boxing?
- Keeping the comboBox updated. Maybe ILists are the way to go here?
- Calling functions of the currently selected class in the comboBox.

Hope it gets clear with this explanation.
Thanks alot.
Jürgen
 

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

Top