473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

make code simple

My project has many class (function), it need to create a new instance each
time by the id value, how can i write a code as much simple?
Actually, all cases is doing the same thing.... , but is possible to make it
simple??? Thanks in advanced.

my code as below:
=============== =========
switch (Id)
{
case "Class01":
com.project.uti l.Class01 c1 = new com.project.uti l.Class01();
c1.function1();
break;
case "Class05":
com.project.uti l.Class05 c1 = new com.project.uti l.Class05();
c5.function();
break;
case "Class08":
com.project.uti l.Class05 c1 = new com.project.uti l.Class05();
c8.function();
break;
}
=============== ==========
May 4 '06 #1
1 1505
Hi Beachboy,

Normally this can be solved with the "Parameteri zed Factory Pattern" as
shown in the Design patterns book.

But, your in C# (Sharp) so you can use a easier way to do this, you can
use reflection to create your class. This will require that the class
names be partly named the same as the Id you want to use to construct
the class.

There are disadvantages to this! It can make error finding harder in a
larger software project. There's so many other ways to solve this
problem as well. But this is a convient and quick way.

I think the following C# code explains what I mean. in the Main see
that you could change the string to be a different class, think of this
as your ID in your method switch (Id). When I cast to Class2 it could
be the superclass of a number of different classes each of which are
subclasses of Class2 created with the
Type.GetType("C onsoleApplicati on1.Class2Subcl ass1"); and
Activator.Creat eInstance...

using System;
using System.Reflecti on;

namespace ConsoleApplicat ion1
{
class Class2
{
public void test()
{
Console.WriteLi ne("Hello world");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Type type = Type.GetType("C onsoleApplicati on1.Class2");
object obj = Activator.Creat eInstance (type);
Class2 instanceOfClass 2 = (Class2) obj;
instanceOfClass 2.test();
Console.ReadLin e();
}
}
}

Thanks! Philip

beachboy wrote:
My project has many class (function), it need to create a new instance each
time by the id value, how can i write a code as much simple?
Actually, all cases is doing the same thing.... , but is possible to make it
simple??? Thanks in advanced.

my code as below:
=============== =========
switch (Id)
{
case "Class01":
com.project.uti l.Class01 c1 = new com.project.uti l.Class01();
c1.function1();
break;
case "Class05":
com.project.uti l.Class05 c1 = new com.project.uti l.Class05();
c5.function();
break;
case "Class08":
com.project.uti l.Class05 c1 = new com.project.uti l.Class05();
c8.function();
break;
}
=============== ==========


May 7 '06 #2

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

Similar topics

82
10670
by: Peter Diedrich | last post by:
The site design is pretty simple: ============================================ | Head | ============================================ | | | | | | | | | left | center | right | | | | | | | | |
12
3290
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
28
2933
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions! PEP: 359 Title: The "make" Statement Version: $Revision: 45366 $ Last-Modified: $Date: 2006-04-13 07:36:24 -0600 (Thu, 13 Apr 2006) $
7
2686
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about the goals -- the make statement is mostly syntactic sugar for:: class <name> <tuple>: __metaclass__ = <callable>
4
2119
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r) and the Language Objects Library) that I have converted over to C#. It works okay. However, in the C# version, one has to build the class library into the generated application, because I haven't structured this one thing right. I would like to...
15
6505
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
6
15772
Sagittarius
by: Sagittarius | last post by:
I will first try to describe my problem in words. I have a simple program, written in C++, that needs to send a single bytearray via a UDP socket to a microprocessor, which returns an answer, also via UDP. Everything works just fine - exept when packet loss occurs (or nothing is recieved at the recieving socket). If packet loss occurs, the program locks up, and it needs to be restarted! Here comes the question:
16
1867
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" Ok. It's a broad, and maybe a very silly question to ask, but still. I mean, what good has it brought to us? What advantages immutable strings have against mutable ones?
14
12426
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be non-zero. ***** Here is an example of what I need to do: #define YEAR_1 2005 #define YEAR_2 2007 #define YEARS (YEAR_2 - YEAR_1 + 1)
0
8375
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
8290
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
8815
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...
0
8707
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7306
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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
4149
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
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

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.