Ron
Ron

Reputation: 85

How to access a property from an inner class in an outer class?

here is some code :

public class TimerEventArgs : EventArgs
{
    public Timer Timer { get; }
    public ClockTimer.TimerTimes Times { get; }
    public DateTime? RunTime { get; }

    public TimerEventArgs(Timer Timer, ClockTimer.TimerTimes Times, DateTime? RunTime)
    {
        this.Timer = Timer;
        this.Times = Times;
        this.RunTime = RunTime;
    }
}

public class ClockTimer
{
    public class TimerTimes
    {
        public DateTime? Start { get; private set; }
        public TimeSpan? RunDuration { get; private set; }
        public DateTime? Stop { get; private set; }

        public TimerTimes()
            : this(null, null, null)
        {}

        public TimerTimes(DateTime? Start, TimeSpan? RunDuration, DateTime? Stop)
        {
            this.Start = Start;
            this.RunDuration = RunDuration;
            this.Stop = Stop;
        }
    }
    :
    private TimerTimes m_TimerTimes = null;
    :
    public virtual void Start()
    {
        // Start timer if not running.
        if (!IsRunning)
        {
            if (m_Timer == null)
                m_Timer = new Timer();
            m_Timer.Interval = Interval;
            m_Timer.Tick += new EventHandler(InnerTimerHandler);

            if (m_TimerTimes == null)
                m_TimerTimes = new TimerTimes();
            m_TimerTimes.Start = DateTime.Now;  //Property Start is inaccesssable!!

            m_Timer.Start();

            TimerEventArgs EventArgs = new TimerEventArgs(m_Timer, m_TimerTimes);

            OnTimerStarted(EventArgs);
        }
    }
    :
}

Is there a way to "set" a property from an inner class in an outer class, but not allow it to be set from outside? Only the outer class must be capable to set the inner class property.

Upvotes: 0

Views: 222

Answers (2)

rbr94
rbr94

Reputation: 2287

If you want to access a property from an outside class, use public for the operation you want to use. In your case you can only read the property get; and not write or change it with set:

public DateTime? Start { get; set; }

The same is applied to your nested class. If you want to access TimerTimes outside of ClockTimer leave it as public, otherwise set it to private to only permit ClockTimer to access it (shortended example):

public class ClockTimer {

    private class TimerTimes {
        public DateTime? Start { get; set; }
    }

    public ClockTimer() {
        var timerTimes = new TimerTimes();
        timerTimes.Start = DateTime.Now;
    } 
}

Upvotes: 0

grek40
grek40

Reputation: 13448

You can create a private inner class with public properties / methods that are accessible to the outer class but not to anything further outside. If you want part of the inner class to be public, derive the private inner class from some sort of public interface (which can be an interface, class or abstract class depending on your needs).

class Program
{
    static void Main(string[] args)
    {
        var t = new ClockTimer();
        t.Start();
        var date = t.TimerTimesInstance.Start; // getter Ok
        t.TimerTimesInstance.Start = DateTime.Now; // Error! setter denied
    }
}
public class ClockTimer
{
    public interface ITimerTimes
    {
        DateTime? Start { get; }
    }
    private class TimerTimes : ITimerTimes
    {
        public DateTime? Start { get; set; }
    }

    private TimerTimes m_TimerTimes = null;
    public virtual void Start()
    {
        m_TimerTimes = new TimerTimes();
        m_TimerTimes.Start = DateTime.Now;  //Property Start is assessible here
    }

    public ITimerTimes TimerTimesInstance { get { return m_TimerTimes; } }
}

As you see, i reduced the example a bit, but all necessary code should be in it.

Upvotes: 1

Related Questions