jeudi 15 septembre 2016

Check if object is defined after initialization in c#

I have the following object (class).

namespace Temp.Models
{
    public class CurrentClass
    {
        private double _firstCoefficient;
        private double _secondCoefficient;

        public double FirstCoefficient
        {
            get { return _firstCoefficient; }
            set { _firstCoefficient= value; }
        }
        public double SecondCoefficient
        {
            get { return _secondCoefficient; }
            set { _secondCoefficient= value; }
        }
    }
}

The following class utilizes the above object and therefore initializes the object as follows:

namespace Temp.Models
{
    public class MainClass
    {
        private CurrentClass _currentClass = new CurrentClass();

        public CurrentClass CurrentClass
        {
            get { return _currentClass; }
            set { _currentClass = value; }
        }
    }
}

At some point if certain conditions are met I would define the variables as follows:

MainClass currentObject = new MainClass();
//if conditions are met
currentObject.CurrentClass.FirstCoefficient = 0;
currentObject.CurrentClass.SecondCoefficient = 5;

But what if the conditions are never met and I never define the above variables. How and/or what is the best way to check if the object was never defined?

I can do the following check:

if(currentObject.CurrentClass.FirstCoefficient != 0 && currentObject.CurrentClass.SecondCoefficent != 0)

But the values can be defined as 0...So I am not sure how to go about this.

Any help is much appreciated!

Aucun commentaire:

Enregistrer un commentaire