473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1698
Hi,

The question here is what do you mean by "instantiat ing 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.Creat eInstnace. 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******** *************@a 75g2000cwd.goog legroups.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 (INameCreationS ervice 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.Componen tModel.Containe r
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.Componen tModel.Componen t, 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.GetServi ce
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.Componen tModel.Design.S erialization.IN ameCreationServ ice 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.N ame + (container.Comp onents.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******** *************@a 75g2000cwd.goog legroups.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
3176
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 textbox,add "a","b","ab"in a listbox,could anyone help me out ?
11
3814
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
11
2215
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 documented protocols would certainly be a good idea for a class hierarchy in the .Net classes. You could adopt and extend the classes at will. Who knows......maybe even improve one and get a new RFC implemented.
3
1544
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 a class that stores the same information. The user selects any number of records from the file when the program loads & can then make changes. The records the user selects are added to an array and changes are made to the records in that array...
25
1488
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 DOG Dim COLOUR As String Dim AGE As Integer Dim NAME As String
5
1549
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 the identical structure and properties. the only difference between them is there methods and that one is a collection class and the other is not. currently the code starts like this:
8
1627
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 distinguish between different types of customers, for example private, business, other etc. This will allow me to filter customers based on their type. Should I
8
1431
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 set of all possible combinations of first and last name.
5
2650
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 method is not allowed. How do you do this if you really need a static class but also have to use these instance objects in them? If you need a simple example of what I am trying to do, it is below: imports Data.EternityRecordsEntities namespace...
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6088
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4052
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.