Hi,
im having trouble understanding how private methods are accessed, and overall how they are used.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SIT232_Ass1
{
    public class Plan
    {
        // declares constant
        private const int CHARGE_BLOCK = 30;

        // declates our private variables
        private decimal _MonthlyFee, _CallAllowance, _CallRate;


        //creates 3 read only properties
        public decimal MonthlyFee
        {
            get { return MonthlyFee; }
          
        }

        public decimal CallAllowance
        {
            get { return CallAllowance; }
        }

        public decimal CallRate

        {
            get { return CallRate; }
        }


        // custom or parameterised constructor - set the private with appropriate given parameter
        public Plan(decimal monthlyFee, decimal callAllowance, decimal callRate)
        {
            _MonthlyFee = monthlyFee;
            _CallAllowance = callAllowance;
            _CallRate = callRate;
        }

         public Plan(Plan plan)
        {
            _MonthlyFee = Plan.MonthlyFee;

        }

        public decimal CalculateCallCost()
        {
            
           decimal block = (Math.Ceiling(PhoneCall._Seconds/Plan.CHARGE_BLOCK));       
        }

        public override string ToString()
        {
            string.Format("{0} per month ({1} call allowance)", Plan.monthlyFee, Plan._CallAllowance);
        }

    }
}

This is one class in my project, for starters Im REALLY struggling to understand why I need to use so many different names for the one variable ie (MonthlyFee,_MonthlyFee and sometimes in other files there are up to 4 variations!
If one variation of the variable is changed does it change them all?

Heres the other file you will need to compile (im aware their are errors). Any help would be GREATLY appreciated

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SIT232_Ass1
{
    public class PhoneCall//Complete
    {

        //Declares private, read only attribute Date
        private string _Date;
        public string Date
        {
            get { return _Date; }
        }


        //Declares private,read only attribute Minutes
        private int _Minutes;
        public int Minutes
        {
            get { return _Minutes; }
        }


        //Declares private, read only attribute Seconds
        private int _Seconds;
        public int Seconds
        {
            get { return _Seconds; }
        }


        // custom or parameterised constructor - set the private with appropriate given parameter
        public PhoneCall(string date, int minutes, int seconds)
        {
            _Date = date;
            _Minutes = minutes;
            _Seconds = seconds;
        }


    }


    }

Recommended Answers

All 4 Replies

You don't have two names for your variables. Take _CallRate for example. It is a private variable of type decimal. You also have a special type of method, known as a Property, named CallRate that provides read only access to the variable _CallRate.

I don't know what you are referring to when you say that you have four versions of the same variable. If you could post the code maybe I can explain what they are.

Just a FYI, there is an easier why to create properties like the ones you have here:

public String Date {get; private set}
public int Minutes {get; private set}
public int Seconds {get; private set}

The compiler handles the creation of the internal private variables that you need (like your _minutes variable).

You also have a special type of method, known as a Property, named CallRate that provides read only access to the variable _CallRate.

When you say 'provides read only access to the variable, how does that happen? Does that mean that _CallRate is read only and private, while CallRate is both public and able to be written to and if one of them is changed is the other also changed?

Also any chance you could tell me why my code wont actually compile?
Thanks for any help you can give

No, _CallRate is a private variable and can only be accessed from within your class. The Property CallRate allows other classes to access the value of _CallRate, but only to read it, they can't set it. That's what properties are for, to allow other classes access to your privates :)

CallRate isn't changed, it's not a variable, it's a method (again a special type of method known as a Property). When you use CallRate it gets the value of _CallRate.

Line 58 of your first code block. ToString() has to return a value. Put return in front of your String.Format statement.

You also need to specify a return type for all your methods, and return some value from them.

Sorry, i feel like an idiot now haha. So then what is the best way to access a private variable from another class? / How can I set it up to allow me to access a private variable from another class.
As I currently get lots of errors such as 'An object reference is required for the non-static field, method, or property'
refering to

decimal block = (Math.Ceiling(PhoneCall._Seconds/Plan.CHARGE_BLOCK));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.