473,471 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB6 to C# Architecture Question - Best Practice?

In VB6, we created a number of ActiveX DLLs that all shared a similar
interface. The main application would load these in dynamically (late-bound.)
This worked well for our situation because we could update the DLLs
independently of the main EXE, and performance was not a problem.

We want to do the same thing in C# (at least test it), but we are not
exactly clear what the best practice is. We would prefer to do something
similar, but we are open to any advice.

In VB6 terms, we have a shared class which serves as the interface to our
DLLs. To keep the example simple, I created this:

‘ VB6 Class Interface
Public Function DoSomething() As Boolean
DoSomething = True
End Function

Each DLL would be compiled with the same class.

A simple example of how we would use this is here:

Private Sub Command1_Click()
Dim objClass As Object
Dim strClass As String
Dim bUseFirstClass As Boolean

bUseFirstClass = False

If bUseFirstClass = True Then
strClass = "prjTestA.clsExample"
Else
strClass = "prjTestB.clsExample"
End If
Set objClass = CreateObject(strClass)

Debug.Print objClass.DoSomething()

Set objClass = Nothing

End Sub
What is the best way to do something similar in C#? We started looking at
reflection, but I want to make sure that is considered the best way to
approach this, or if there is some other words of wisdom we should follow.
Any sample code would be most appreciated!

Thank you for your time and advice.

Mo

Jan 31 '07 #1
4 2871
In C#, you could have (converted directly via Instant C#):
private void Command1_Click()
{
object objClass = null;
string strClass = null;
bool bUseFirstClass = false;

bUseFirstClass = false;

if (bUseFirstClass)
{
strClass = "prjTestA.clsExample";
}
else
{
strClass = "prjTestB.clsExample";
}

System.Type objClassType = System.Type.GetTypeFromProgID(strClass);
objClass = System.Activator.CreateInstance(objClassType);

Debug.Print objClassType.InvokeMember("DoSomething",
System.Reflection.BindingFlags.InvokeMethod, null, objClass, null);

objClass = null;
}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
"moflaherty" wrote:
In VB6, we created a number of ActiveX DLLs that all shared a similar
interface. The main application would load these in dynamically (late-bound.)
This worked well for our situation because we could update the DLLs
independently of the main EXE, and performance was not a problem.

We want to do the same thing in C# (at least test it), but we are not
exactly clear what the best practice is. We would prefer to do something
similar, but we are open to any advice.

In VB6 terms, we have a shared class which serves as the interface to our
DLLs. To keep the example simple, I created this:

‘ VB6 Class Interface
Public Function DoSomething() As Boolean
DoSomething = True
End Function

Each DLL would be compiled with the same class.

A simple example of how we would use this is here:

Private Sub Command1_Click()
Dim objClass As Object
Dim strClass As String
Dim bUseFirstClass As Boolean

bUseFirstClass = False

If bUseFirstClass = True Then
strClass = "prjTestA.clsExample"
Else
strClass = "prjTestB.clsExample"
End If
Set objClass = CreateObject(strClass)

Debug.Print objClass.DoSomething()

Set objClass = Nothing

End Sub
What is the best way to do something similar in C#? We started looking at
reflection, but I want to make sure that is considered the best way to
approach this, or if there is some other words of wisdom we should follow.
Any sample code would be most appreciated!

Thank you for your time and advice.

Mo
Feb 1 '07 #2
"moflaherty" <mo********@discussions.microsoft.comwrote in message
news:3E**********************************@microsof t.com...
In VB6, we created a number of ActiveX DLLs that all shared a similar
interface. The main application would load these in dynamically
(late-bound.)
This worked well for our situation because we could update the DLLs
independently of the main EXE, and performance was not a problem.

We want to do the same thing in C# (at least test it), but we are not
exactly clear what the best practice is. We would prefer to do something
similar, but we are open to any advice.

In VB6 terms, we have a shared class which serves as the interface to our
DLLs. To keep the example simple, I created this:

' VB6 Class Interface
Public Function DoSomething() As Boolean
DoSomething = True
End Function

Each DLL would be compiled with the same class.

A simple example of how we would use this is here:

Private Sub Command1_Click()
Dim objClass As Object
Dim strClass As String
Dim bUseFirstClass As Boolean

bUseFirstClass = False

If bUseFirstClass = True Then
strClass = "prjTestA.clsExample"
Else
strClass = "prjTestB.clsExample"
End If
Set objClass = CreateObject(strClass)

Debug.Print objClass.DoSomething()

Set objClass = Nothing

End Sub
What is the best way to do something similar in C#? We started looking at
reflection, but I want to make sure that is considered the best way to
approach this, or if there is some other words of wisdom we should follow.
Any sample code would be most appreciated!

Thank you for your time and advice.

Mo
I think I know what you are trying to do but I'm not sure. In VB6, you
didn't have proper inheritance, but in C#, you do. There is no reason you
cannot create an interface directly. In C#, you can create the interface,
compile it into a DLL, and then simply make a reference to that DLL from all
projects that need to implement the interface (no need to compile the
'psuedo-base-class' into every DLL... it's a real base class or interface
now).

So all the design patterns work just fine in C#. Examples of how to use
each of the design patterns in C# can be found at www.dofactory.com

For what you are trying to do, you are creating a plug in. So create the
interface that all of your subtypes will implement and create a factory that
is able to instantiate a type based on a string. The string can come from
reflection or it can come from a config file... either way, hide the
creation of your plug-in type in a factory method so that the calling code
won't have to know or care where your logic is for discovering the actual
type you are creating.

I hope that helps,
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Feb 1 '07 #3
Interesting product!
Feb 1 '07 #4
On Jan 31, 10:25 am, moflaherty <moflahe...@discussions.microsoft.com>
wrote:
In VB6, we created a number of ActiveX DLLs that all shared a similar
interface. The main application would load these in dynamically (late-bound.)
This worked well for our situation because we could update the DLLs
independently of the main EXE, and performance was not a problem.

We want to do the same thing in C# (at least test it), but we are not
exactly clear what the best practice is. We would prefer to do something
similar, but we are open to any advice.

In VB6 terms, we have a shared class which serves as the interface to our
DLLs. To keep the example simple, I created this:

' VB6 Class Interface
Public Function DoSomething() As Boolean
DoSomething = True
End Function

Each DLL would be compiled with the same class.

A simple example of how we would use this is here:

Private Sub Command1_Click()
Dim objClass As Object
Dim strClass As String
Dim bUseFirstClass As Boolean

bUseFirstClass = False

If bUseFirstClass = True Then
strClass = "prjTestA.clsExample"
Else
strClass = "prjTestB.clsExample"
End If

Set objClass = CreateObject(strClass)

Debug.Print objClass.DoSomething()

Set objClass = Nothing

End Sub

What is the best way to do something similar in C#? We started looking at
reflection, but I want to make sure that is considered the best way to
approach this, or if there is some other words of wisdom we should follow.
Any sample code would be most appreciated!

Thank you for your time and advice.

Mo
You didn't use an interface at all, except only superficially.
Otherwise your classes would have used the Implements keyword to
implement the interface. If you had used a "real" interface, you
would not have had to use a variable of type Object, but a variable of
the type of the interface class.

But as Nick Malik said, in C#, you can create a proper Interface type
and then implement it any classes you needed. You can use reflection
to check to see if a class implements the interface and instantiate it
if it does.

Chris

Feb 2 '07 #5

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

Similar topics

25
by: David Noble | last post by:
We've been developing a web site using 3-tier architecture for 18 months now. There is a common layer that defines the classes - using XML schemas. The data layer acts as a wrapper to 3 databases...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
41
by: laimis | last post by:
Hey guys, I just recently got introduced to data mappers (DTO mapper). So now I have a SqlHelper being used by DTOMapper and then business layer is using DTOMapper when it needs to persist...
2
by: mikelinyoho | last post by:
Does the following program architecture make sense? #include <stdio.h> #include "test1.c" #include "sample.cpp" int main(void){ output();
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
0
by: crm-tutorial | last post by:
Service-Oriented Architecture(SOA) - Principles & Practice (October 2006) The Principles of SOA,Loosely-Coupled Architecture,The SOA Solution - How it Works,The iBOLT Flow Editor, Composite...
24
by: Jon | last post by:
I'm working on a project and the lead developer wants to put all of the business logic on the database side. He wants the proc to do all of the logic and create XML on the fly, parse out data, etc,...
3
by: anthonykallay | last post by:
Hi there, I am building a website for a group that has 4 companies under it. The sites are fairly similar so i want to be able to use the same set of code in the App_Code folder.. I also want to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.