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

Creating objects by name approach in .NET / C#

We are moving from VB6 to C# and we like to know a good approach in .NET /
C# for this:

In VB6 we have some objects (classes) with the same interface, i.e. a to
control specific printers. Based on the configuration we load the correct
object that belongs to the attached printer (example code):

Dim obj as PrinterObject
Dim MyPrinter as String

MyPrinter = ReadPrinterFromDatabase ()
Set obj = CreateObject(MyPrinter)
obj.Print "Hello World"

IMO there are two ways:

a) Create different classes for each printer and create the class, but I
don't see a way how to do this (obj = new getPrinterClassName(); ???)
unless we use some Case or If Then code. Less flexible because adding
printers needs a modification in the code.
b) Create the object from a DLL by giving the DLL name (of .NET objects??)

I don't know if there are some '.net' ways to to this, in fact we wish to
add/install some DLL or object (with or without registering) without
terminating the application. Then we can change the configuration and the
new object is loaded when used.

Regards,

Rene



Nov 15 '05 #1
3 1563
Hi Rene,

You may like to take a look at .NET Reflection for your requirements.
Refection allows you load classes dynamically by the name of the class type.
To get started, please take a look at Assembly.CreateInstance() or
Activator.CreateInstance().

Regards,
Aravind C
"Rene" <no@spam.nl> wrote in message
news:Op**************@TK2MSFTNGP10.phx.gbl...
We are moving from VB6 to C# and we like to know a good approach in .NET /
C# for this:

In VB6 we have some objects (classes) with the same interface, i.e. a to
control specific printers. Based on the configuration we load the correct
object that belongs to the attached printer (example code):

Dim obj as PrinterObject
Dim MyPrinter as String

MyPrinter = ReadPrinterFromDatabase ()
Set obj = CreateObject(MyPrinter)
obj.Print "Hello World"

IMO there are two ways:

a) Create different classes for each printer and create the class, but I
don't see a way how to do this (obj = new getPrinterClassName(); ???)
unless we use some Case or If Then code. Less flexible because adding
printers needs a modification in the code.
b) Create the object from a DLL by giving the DLL name (of .NET objects??)

I don't know if there are some '.net' ways to to this, in fact we wish to
add/install some DLL or object (with or without registering) without
terminating the application. Then we can change the configuration and the
new object is loaded when used.

Regards,

Rene


Nov 15 '05 #2
Have a XML file for your printer configuration, define in
that
<XML>
<Printer name ="HP Laserjet 1100" Assembly="HP1100.dll">
<xml>

and load the assembly on the run time using
reflection ... This way you added a layer for indirection
and adding new printer will not require recompile of your
whole code ..

Search MSDN for Activator to get the code to do the
reflection..

Sarosh
-----Original Message-----
We are moving from VB6 to C# and we like to know a good approach in .NET /C# for this:

In VB6 we have some objects (classes) with the same interface, i.e. a tocontrol specific printers. Based on the configuration we load the correctobject that belongs to the attached printer (example code):
Dim obj as PrinterObject
Dim MyPrinter as String

MyPrinter = ReadPrinterFromDatabase ()
Set obj = CreateObject(MyPrinter)
obj.Print "Hello World"

IMO there are two ways:

a) Create different classes for each printer and create the class, but Idon't see a way how to do this (obj = new getPrinterClassName(); ???)unless we use some Case or If Then code. Less flexible because addingprinters needs a modification in the code.
b) Create the object from a DLL by giving the DLL name (of .NET objects??)
I don't know if there are some '.net' ways to to this, in fact we wish toadd/install some DLL or object (with or without registering) withoutterminating the application. Then we can change the configuration and thenew object is loaded when used.

Regards,

Rene



.

Nov 15 '05 #3
Thanks for you answer, I read something about reflection not knowing it can
be used for this.

Rene

"rs*****@hotmail.com" <an*******@discussions.microsoft.com> schreef in
bericht news:05****************************@phx.gbl...
Have a XML file for your printer configuration, define in
that
<XML>
<Printer name ="HP Laserjet 1100" Assembly="HP1100.dll">
<xml>

and load the assembly on the run time using
reflection ... This way you added a layer for indirection
and adding new printer will not require recompile of your
whole code ..

Search MSDN for Activator to get the code to do the
reflection..

Sarosh
-----Original Message-----
We are moving from VB6 to C# and we like to know a good

approach in .NET /
C# for this:

In VB6 we have some objects (classes) with the same

interface, i.e. a to
control specific printers. Based on the configuration we

load the correct
object that belongs to the attached printer (example

code):

Dim obj as PrinterObject
Dim MyPrinter as String

MyPrinter = ReadPrinterFromDatabase ()
Set obj = CreateObject(MyPrinter)
obj.Print "Hello World"

IMO there are two ways:

a) Create different classes for each printer and create

the class, but I
don't see a way how to do this (obj = new

getPrinterClassName(); ???)
unless we use some Case or If Then code. Less flexible

because adding
printers needs a modification in the code.
b) Create the object from a DLL by giving the DLL name

(of .NET objects??)

I don't know if there are some '.net' ways to to this,

in fact we wish to
add/install some DLL or object (with or without

registering) without
terminating the application. Then we can change the

configuration and the
new object is loaded when used.

Regards,

Rene



.

Nov 15 '05 #4

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

Similar topics

5
by: Keiron Waites | last post by:
<script language="JavaScript" type="text/javascript"> <!-- var array1 = new Array(); var array1i = new Array(); array1 = array1i; alert(array1); // --> </script>
7
by: nog | last post by:
What's the best approach to creating many objects of a class? I have in mind using something analogous to a table to hold the data - which is in a form similar to (char name, char address, date...
9
by: cppaddict | last post by:
I have a method that uses a fairly large object. The choice is between having a local object in the method or a static member object that the method uses. ------CHOICE 1--------- int...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
8
by: Wilbur Slice | last post by:
I need to write some VB.NET objects that can be called from another environment (a Progress app, specifically). I'm having some difficulty getting this to work. My first assumption was that I...
1
by: Danny Liberty | last post by:
I need some opionions on an issue here... Suppose I want to keep a collection of objects, each need to be uniquely identified by a number. This number has no meaning as long as it's unique, so it...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.