How to access a variables and functions of a specific class from another class in C#


How to access variables and functions of a specific class from another class in C#

Please give an example.


Asked by:- MuhammadFaizan
1
: 2104 At:- 9/22/2017 5:58:39 PM
C# .Net







1 Answers
profileImage Answered by:- Sam

You can call a variable of first class from second class by creating Class constructor it is not a static class for static class you can use

public class First
{
    public string Name {get;set;}
}

//calling it from another class
class Program
{
    First frst= new First();
    static void Main(string[] args)
    {
        frst.Name = "Sam";
    }
}

Check MSDN
base class with property:

class Person
{
    private string name;  // the name field
    public string Name    // the Name property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

Check this url Auto-Implemented Properties (if advanced work on "name" isn't needed):

class Person
{
    public string Name { get; set; } // the Name property with hidden backing field
}

Accessing class property from another class

Person person = new Person();
person.Name = "Joe";  // the set accessor is invoked here                

System.Console.Write(person.Name);  // the get accessor is invoked here
1
At:- 9/23/2017 8:09:38 AM Updated at:- 9/25/2017 3:39:55 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use