?
Solved

Casting a generic Object in C# to a custom Object

Posted on 2005-04-18
5
Medium Priority
?
318 Views
Last Modified: 2012-06-21
I have a situation where I need to declare a generic object globally in a Public class. Then later in a Switch statement I instantiate an instance of a custom DLL based on a value.  There are about 5 DLLs, all with the same public routines. The routines are
INSTANTIATE
CONNECT
NAVIGATE
SEND
CONFIRM
DISCONNECT

The code within these routine are all different (based on Protocols) but the Routine's names and return values are all the same.

Once the switch statement finds a match I try to cast my Global Generic Object to that of the Custom DLL object type.  It is not working.  Can anyone help me out?

Public class MyClass
{
   Private Object TransmissionObject;
   Private string Transit_Type;

   Public string TransitType
   {
       Get{return Transit_Type;}
       Set{Transit_Type = value;}
    }

    public void MyRoutine()
    {
      Switch(Transit_Type)
      {
            Case “FTP”:
            TransFTP.Session tObject = new TransFTP.Session();
                                TransmissionObject =  (TransFTP.Session )tObject;
            Break;
            Case “HTTP”:
                                TransHTTP.Session tObject = new TransHTTP.Session();
                                TransmissionObject =  (TransHTTP.Session )tObject;
            Break;
            Case “NTFS”:
                                TransNTFS.Session tObject = new TransNTFS.Session();
                                TransmissionObject =  (TransNTFS.Session )tObject;
            Break;
      }      
       }
}
0
Comment
Question by:mandalorian4
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13807329
You have a classic use of interface. Create an interface and have all the classes implement that interface.
0
 
LVL 3

Author Comment

by:mandalorian4
ID: 13808220
dharmesh_amity,

I have created an Interface object.  However, if the different DLLs implement the same object how will the interface DLL know which DLL to use?  Are you suggesting That the DLLs use different Routine names and have the interface implement all the routines?
0
 
LVL 6

Accepted Solution

by:
dharmesh_amity earned 1000 total points
ID: 13808443
No, I am not suggesting that.

Lets assume that you have interface IMyInterface which has declarations of the methods whose implementation will be the classes you provide.

Lets say one of that class is TransHTTP which implements all the methods of the interface, which is in the namespace TransHTTPNamespace.


Now while constructing the object you will have

IMyInterface tObject = new TransHTTPNamespace.TransHTTP();

Lets say your TransHTTP class is in some dll called MyHTTPDLL. You add the refence of this dll from by doing a add reference into your project. So now when you instantiate TransHTTP class you know exactly which class's object you are instantiating.

After this you can use tObject and at runtime the proper method implementation will be called as the compiler will know what type of object is in the reference tObject.

Hope this helps


0
 
LVL 3

Author Comment

by:mandalorian4
ID: 13815337
dharmesh_amity,

That was exactly what I needed. I tried that in the past but I was instantiating the Object through the interface incorrectly:

TransObject = (ITransSrvProcess) Activator.GetObject(typeof(ITransSrvProcess),null,null);

Thank you very much for the tip.


Mandalorian
0
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13816560
I am happy that I was helpful. Thanks

Dharmesh
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Starting up a Project
Screencast - Getting to Know the Pipeline

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question