locked
Shorthand Getters and Setters in C# RRS feed

  • Question

  • How to access the Short Hand getters and setters of a class in the main method of another class?

    for e.g.

    class Employee
        {
            int eno {get; set;}
            string ename {get; set;}
            double bSalary {get; set;}

        }

    now how do we access it in the main method of another class?

    class EmployeeManager

    {

            static void Main(string[] args)

    {

    ????

    }

    }

    Wednesday, September 26, 2012 6:35 PM

Answers

  • Set to public and try this

            public class Employee
            {
                public int eno { get; set; }
                public string ename { get; set; }
                public double bSalary { get; set; }
            }
    
            static void Main(string[] args)
            {
                Employee emp = new Employee();
                emp.eno = 1;
            }


    Web Developer

    Wednesday, September 26, 2012 6:40 PM

All replies

  • Set to public and try this

            public class Employee
            {
                public int eno { get; set; }
                public string ename { get; set; }
                public double bSalary { get; set; }
            }
    
            static void Main(string[] args)
            {
                Employee emp = new Employee();
                emp.eno = 1;
            }


    Web Developer

    Wednesday, September 26, 2012 6:40 PM
  • The employee class and the properties are not static so you have to access them so:

    Employee e = new Employee; // creating an Empoyee object
    e.ename = "test"; // setting the property
    string help = e.ename; // getting the property

    Wednesday, September 26, 2012 6:42 PM
  • The employee class and the properties are not static so you have to access them so:

    Employee e = new Employee; // creating an Empoyee object
    e.ename = "test"; // setting the property
    string help = e.ename; // getting the property

    You're right, but if he doesnt set his getters and setter variable to public, the intelisense cannot see this properties.

    Web Developer

    Wednesday, September 26, 2012 6:43 PM
  • Hello, you can create Static Property in Class Employee

    class Employee
        {
            Public Static int eno {get; set;}
        }
    
    Static void Main(string[] args)
    
    {
    
    Employee.eno //Accessing property
    
    } 
    HTH

    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Edited by Sachin P Wednesday, September 26, 2012 6:44 PM
    Wednesday, September 26, 2012 6:44 PM
  • Hello, you can create Static Property in Class Employee

    It's unlikely that those properties should be static; they simply need to be public and for an instance to be created.
    Wednesday, September 26, 2012 7:23 PM
  • Hi,

    I have a class Songsdetails with some getter and setter properties.


        public class Songsdetails

        {

              public string songname { get; set; }
              public string songpic { get; set; }
              public string songurl { get; set; }
              public string songcode { get; set; }
              public string songdescr { get; set; }
              public string songtitletags { get; set; }
              public string songmetatags { get; set; }
              public string songmetadesc { get; set; }
              public string songdate { get; set; }

        }
    }

       I have to call the songcode get property to Videopage.Xaml class.Following the above code i called songcode property to Videopage class.But i was unable to see properties in intelisense and soncode was also not getting.



     public partial class Videopage : PhoneApplicationPage
        {
            // Constructor
            public Videopage()
            {
                InitializeComponent();
            
                Songsdetails mysongdetails = new Songsdetails();
                string code = mysongdetails.songcode;

            string video = "http://www.youtube.com/embed/" +code;
                System.Diagnostics.Debug.WriteLine(video);
                this.webBrowser.Navigate(new Uri(video));

                    }

    Please anybodyhelp me were i made a mistake.Working on this from many weeks.And after i have play videos in the emulator also.

    Thanks in advance.

        
    Tuesday, July 1, 2014 10:10 AM