andas1951
andas1951

Reputation: 65

ToString() method return different result based on object

I am working on an app where you can create a Subscription and you can either choose an Individual or Company Subscription which are both classes. But I am having some trouble with the override ToString(). Is it possible to have some kind of an if statement that can say if the object is create as an Individual Subscription return the string formatted for an Individual, else return the one for the Company. Here is my code to make it clearer:

public class Subscription: ObservingClass
{

    private IndividualApplicant _individualApplicant;
    private CompanyApplicant _companyApplicant;
    private SubsAmount _subsAmount;

    public IndividualApplicant IndividualApplicant
    {
        get { return _individualApplicant; }
        set
        {
            _individualApplicant = value;
            OnPropertyChanged();
        }
    }

    public CompanyApplicant CompanyApplicant
    {
        get { return _companyApplicant; }
        set
        {
            _companyApplicant = value;
            OnPropertyChanged();
        }
    }

    public SubsAmount SubsAmount
    {
        get { return _subsAmount; }
        set
        {
            _subsAmount = value;
            OnPropertyChanged();
        }
    }

    public Subscription()
    {
        IndividualApplicant = new IndividualApplicant();
        CompanyApplicant =  new CompanyApplicant();
    }

    public override string ToString()
    {
        return "Subscription Amount: " + SubsAmount + "\n" + IndividualApplicant.ToString();
    }


}

Upvotes: 0

Views: 194

Answers (2)

Jan Paolo Go
Jan Paolo Go

Reputation: 6522

create a Subscription and you can either choose an Individual or Company Subscription which are both classes

Then your Subscription class should look like this..

public class Subscription : ObservingClass
{
    private Applicant _applicant;
    public Applicant Applicant
    {
        get
        {
            return _applicant;
        }
        set
        {
            _applicant = value;
            OnPropertyChanged();
        }
    }
    public Subscription(Applicant applicant)
    {
        Applicant = applicant;
    }

    public override string ToString()
    {
        return "Subscription Amount: " + SubsAmount + "\n" + Applicant.ToString();
    }

    private SubsAmount _subsAmount
    public SubsAmount SubsAmount
    {
        get { return _subsAmount; }
        set
        {
            _subsAmount = value;
            OnPropertyChanged();
        }
    }
}

With this, Subscription only knows that it has an Applicant which is an abstract class (or make it an interface if you like)

Now, IndividualApplicant and CompanyApplicant should inherit Applicant.

From there, override ToString for both the IndividualApplicant and CompanyApplicant

Upvotes: 1

TomTom
TomTom

Reputation: 62093

Your problem is much worse than that.

can either choose an Individual or Company Subscription which are both classes.

This would indicate inhertance - 2 classes (IndividualSubscription, CompanySubscription) - and then... where is the problem? both override Tostring as they want and need.

But your class model is extremely bad.

public CompanyApplicant CompanyApplicant { get { return _companyApplicant; } set { _companyApplicant = value; OnPropertyChanged(); } }

WTF? THat should be Applicant - a CompanySubscription may throw an ArgumentExcveption when an IndivdualApplicant is inserted, but it makes little sense and totally convolutes all logic to have different properties for them depending on subtype.

Upvotes: 2

Related Questions