473,403 Members | 2,270 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,403 software developers and data experts.

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.util.Class01 c1 = new com.project.util.Class01();
c1.function1();
break;
case "Class05":
com.project.util.Class05 c1 = new com.project.util.Class05();
c5.function();
break;
case "Class08":
com.project.util.Class05 c1 = new com.project.util.Class05();
c8.function();
break;
}
=========================
May 4 '06 #1
1 1490
Hi Beachboy,

Normally this can be solved with the "Parameterized 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("ConsoleApplication1.Class2Subclass1" ); and
Activator.CreateInstance...

using System;
using System.Reflection;

namespace ConsoleApplication1
{
class Class2
{
public void test()
{
Console.WriteLine("Hello world");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Type type = Type.GetType("ConsoleApplication1.Class2");
object obj = Activator.CreateInstance (type);
Class2 instanceOfClass2 = (Class2) obj;
instanceOfClass2.test();
Console.ReadLine();
}
}
}

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.util.Class01 c1 = new com.project.util.Class01();
c1.function1();
break;
case "Class05":
com.project.util.Class05 c1 = new com.project.util.Class05();
c5.function();
break;
case "Class08":
com.project.util.Class05 c1 = new com.project.util.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
by: Peter Diedrich | last post by:
The site design is pretty simple: ============================================ | Head | ============================================ | | ...
12
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. ...
28
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!...
7
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...
4
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)...
15
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...
6
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...
16
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?" ...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.