Web Dev Guy
Web Dev Guy

Reputation: 1799

How to call class without passing parameters using constructor in C#

I'm new to C# and oop. So what I'm trying to work out. Is there a way of doing something like this:

var acs = new Acs();

So when I create the class object above it uses the apikey and apiurl below without passing those parameters into the Acs()

public class Acs : ActiveCampaignService
{
    public Acs(string apiKey, string apiUrl, string apiPassword = null) : base(apiKey, apiUrl, apiPassword)
    {
        apiKey = "my_api_key";
        apiUrl = "my_api_url";
        //return when var acs = new Acs(); is used
    }
}

Also should:

public Acs(string apiKey, string apiUrl, string apiPassword = null)

Be:

private Acs(string apiKey, string apiUrl, string apiPassword = null)

Just not sure if this is possible. Cheers

Upvotes: 0

Views: 3178

Answers (4)

radarbob
radarbob

Reputation: 5101

I'm reading the question as every (new) object should have these two properties with the same values. So use static. Further they could (should) be in the base class since you're passing them to the base constructor.

public class ActiveCampaignService
{
   protected static ApiKey { get { return "my_api_key"; }}
   protected static ApiUrl { get { return "my_api_url"; }}

   public ActiveCampaignService (string apiPassword = null) : base (apiPassword)  { }
}

As shown, derived classes can access these static properties but not other client code.

public class Acs : ActiveCampaignService {

   public Acs ( string apiPassword = null) : base (apiPassword)
      someVariable = ActiveCampaignService.ApiUrl + " morePath";
}

Upvotes: 0

Rufus L
Rufus L

Reputation: 37070

You can use constructor chaining, which is where you create different constructors that take different arguments. Each one calls the next one, passing in default values for the arguments that weren't specified. So you start with an empty one, and you can also create one that doesn't take a password. Then you have one that takes everything, and that's the one that actually does something. This is called constructor chaining:

public class Acs : ActiveCampaignService
{
    // Assuming you have class properties to hold values passed in from constructor
    private string ApiKey { get; set; }
    private string ApiUrl { get; set; }
    private string ApiPassword { get; set; }

    // Default constructor takes no arguments. Passes default values to next constructor
    public Acs() : this ("my_api_key", "my_api_url")
    { }

    // This constructor takes two arguments and passes them to
    // the next constructor, passing null for the apiPassword
    public Acs(string apiKey, string apiUrl) : this(apiKey, apiUrl, null)
    { }

    // This is the final constructor that does something 
    // with the values, and calls the base constructor
    public Acs(string apiKey, string apiUrl, string apiPassword) 
        : base(apiKey, apiUrl, apiPassword)
    {
        ApiKey = apiKey;
        ApiUrl = apiUrl;
        ApiPassword = apiPassword;
    }
}

Upvotes: 1

Fabulous
Fabulous

Reputation: 2423

If you make your constructor private, you won't be able to instantiate the class. If you want to create your class the way you propose you'll have to use object initializers.

You will need a default constructor, that is one without any parameters, for my suggestion to work. Both constructors can live in the same class and the you can choose whichever one you want to call.

public Acs(string apiKey, string apiUrl, string apiPassword = null) : base(apiKey, apiUrl, apiPassword)
{
    apiKey = "my_api_key";
    apiUrl = "my_api_url";
}

public Acs()
{
    // anything you want here, or nothing.
}

public string ApiKey { get; set; }
public string ApiUrl { get; set; }

Then you create the object this way.

var acs = new Acs()
{
    ApiKey = "...",
    ApiUrl = "..."
};

I'd recommend that apiKey and apiUrl be public properties as I have done.

Upvotes: 1

TomServo
TomServo

Reputation: 7409

If you set the constructor to private, nothing will be able to call that constructor. If you set it to protected, only classes inheriting from Acs will be able to instantiate. Is that what you're driving at?

Upvotes: 0

Related Questions