[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

calling constructors

Posted on 2004-11-04
4
Medium Priority
?
223 Views
Last Modified: 2010-04-15
Hello again Experts,

is it possible to call different constructors based on the type of object passed into it?

for example:

public class A
{
     public A()
     {
          //default constructor
     }

     public A(ClassZ myObj)
     {
           //do something based on class ClassZ
     }

    public A(ClassY myObj)
    {
           //do something based on class ClassY
    }
}


client code:

   ClassZ objZ = new ClassZ();
   ClassY objY = new ClassY();

   A objA = new A(objZ);
           or
   A objA = new A(objY);

thanks




0
Comment
Question by:brdrok
4 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 12496550
I think yeah. C# compiler would take care of it, as to which constructor to execute. This is nothing but method overloading
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12496565
Yes
0
 
LVL 9

Accepted Solution

by:
tinchos earned 200 total points
ID: 12496948
yes it is, and you can also call from one constructor to another one.

public class A
{
     public A()
     {
          //default initialization
     }

     public A(ClassZ myObj)
          : this()
     {
           //do something based on class ClassZ
     }

    public A(ClassY myObj)
          : this()
    {
           //do something based on class ClassY
    }
}

In that way if you call

new A( new ClassY() );

it would call the constructor that receives a ClassY object, then it would call the default constructor and then it would execute the rest of the constructor that receives a classYObject

Hope this helps

Tincho
0
 
LVL 7

Author Comment

by:brdrok
ID: 12498048
thanks...
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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

834 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