473,322 Members | 1,620 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.

Instancing classes with a combination of string and integer possible?

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

Jan 23 '07 #1
2 1678
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)

<Ch*******@gmx.dewrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
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
Jan 23 '07 #2
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.INameCr eationService 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)

<Ch*******@gmx.dewrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
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
Jan 23 '07 #3

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

Similar topics

3
by: AsuWoo | last post by:
hi, I want to implement a function that prints all possible combinations of a characters in a string,eg. input "123"into a textbox, add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
11
by: Jim | last post by:
Have you seen any NNTP classes that I may use or build upon to build a simple newsreader/downloader? Is there such a class in the .Net framework that I have overlooked? If not, inclusion of RFC...
3
by: ArmsTom | last post by:
I was using structures to store information read from a file. That was working fine for me, but then I read that anything stored in a structure is added to the stack and not the heap. So, I made...
25
by: Brian | last post by:
Can some one please tell me what I'm doing wrong. I'm trying to create a class called Dog, but Visual Basic tells me that I can't enter Wolf.age....why is this? Public Class Form1 Public Class...
5
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
8
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to...
8
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
An example of a slightly more complicated class might be to have a collection of first names, and a collection of last names in your class. The IEnumerable functions then could return the complete...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.