Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Type Casting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Analizer1 - 30 Oct 2007 18:32 GMT
Hi All...
i have a situation where i have 3 different Types (classes)
comming into a controling Class to Run Different Jobs
in my main loop i want to use 1 Field to hold a instance of the Class
currently Running example below

public class controler(
{
//   how to use the below process for all 3 different Type?
// this is ccalled each time a run is started in
//different threads from the service
//i want to use one field to hold the current Process
//instead of using 3 Fields and if block
   private Process
   public controler(int iToRun)
  {
       if (iToRun==1)
         {
             Process = new Object1() ;
          }
       else if (iToRun==2)
        {
            Process  = new Object2();
        }
      else
       {
            Process = new Object3();
        }
   }
public int Runjob()
{
       int iRet=0;
      if ( (iRet=Process.Execute())<=0)
      {
           //error occurred
        }
     else
      {
          //success
      }
}
}

Tia
Analizer1
Jon Skeet [C# MVP] - 30 Oct 2007 18:41 GMT
> i have a situation where i have 3 different Types (classes)
> comming into a controling Class to Run Different Jobs
> in my main loop i want to use 1 Field to hold a instance of the Class
> currently Running example below

Create an interface (e.g. IExecutable) with all the methods that the
different types have in common (e.g. Execute()) and make all of the
types implement that interface.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nicholas Paldino [.NET/C# MVP] - 30 Oct 2007 18:42 GMT
Analizer1,

   You should have all three objects implement a shared interface, and then
cast the objects to an instance of that interface.  Then, you can call the
methods on the interface.

   You could do this with reflection, but that's just messy.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi All...
> i have a situation where i have 3 different Types (classes)
[quoted text clipped - 41 lines]
> Tia
> Analizer1
Analizer1 - 30 Oct 2007 19:28 GMT
InterFace Worked Great....
Thanks alot

> Hi All...
> i have a situation where i have 3 different Types (classes)
[quoted text clipped - 41 lines]
> Tia
> Analizer1
sloan - 31 Oct 2007 20:37 GMT
Do a search on the "Command Design Pattern" as well, to learn some
caveats............

> Hi All...
> i have a situation where i have 3 different Types (classes)
[quoted text clipped - 41 lines]
> Tia
> Analizer1

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.