简体   繁体   中英

How do I inherit from a base class and add another property?

I have a class that's used to construct a request object to pass to an API:

public class BaseRequest
{
    public string messageId { get; set; }
    public string timestamp { get; set; }
    public string updatedAtMin { get; set; }
    public int page { get; set; }

    public BaseRequest(DateTime updatedAtMin, int page)
    {
        this.messageId = Guid.NewGuid().ToString();
        this.timestamp = DateTime.UtcNow.ToString("o");
        this.updatedAtMin = updatedAtMin.ToString("o");
        this.page = page;
    }
}

However, there is an edge case where an optional parameter needs to be added to the request object. My first thought was to inherit from the BaseRequest class but I can't see how to create this and make it syntactically correct.

public class CaseRequest : BaseRequest
{
    public bool extraParameter { get; set; }

    public CaseRequest(bool extraParameter) : base()
    {
        this.extraParameter = extraParameter;
    }
}

Error: There is no argument given that corresponds to the required formal parameter 'updatedAtMin' of 'BaseRequest.BaseRequest(DateTime, int)

Can anyone please help? Esssentially, I want to create an instance of the BaseRequest with the usual constructor but adding in the extraParameter.

Many thanks, and apologies for my woeful ignorance!

Your base class' constructor has parameters:

public BaseRequest(DateTime updatedAtMin, int page)

But you're not passing any values for those parameters:

base()

Ideally you would expand your constructor to accept those parameters and pass them along:

public CaseRequest(DateTime updatedAtMin, int page, bool extraParameter) : base(updatedAtMin, page)
{
    this.extraParameter = extraParameter;
}

You could also pass default values if you don't want the user to set them. Or create a parameterless constructor in the base class. Etc. As long as you pass something to satisfy a base constructor.

Your problem is simple as the error indicates.

There is no argument given that corresponds to the required formal parameter 'updatedAtMin' of 'BaseRequest.BaseRequest(DateTime, int)

You define a constructor in BaseRequest. This eliminates the default parameterless constructor. Which means...

public CaseRequest(bool extraParameter) : base()

does not find base() because base () does not exist.

YOu MUST accept the other parameter and pass them into the defined base to be syntatically correct.

you can try the following

public class BaseRequest
{
    public string messageId { get; set; }
    public string timestamp { get; set; }
    public string updatedAtMin { get; set; }
    public int page { get; set; }

    public BaseRequest(DateTime updatedAtMin, int page)
    {
        this.messageId = Guid.NewGuid().ToString();
        this.timestamp = DateTime.UtcNow.ToString("o");
        this.updatedAtMin = updatedAtMin.ToString("o");
        this.page = page;
    }

    public BaseRequest() {
    }
}

public class CaseRequest : BaseRequest
{
    public bool extraParameter { get; set; }

    public CaseRequest(bool extraParameter) : base()
    {
        this.extraParameter = extraParameter;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM