Shahab_HK Shahab_HK - 1 month ago 4x
C# Question

How to get a variable from multi-variable instance of a class?

i have a simple class like this:

public class r_event
string name;
double time;

public r_event()
name = "none";
time = 0;

public r_event(string name, double time)
{ = name;
this.time = time;

and i have made an instance in another class as below:

r_event event1 = new r_event("SH_S", 2);

but i want to assign just one of these two variable (event,time) to a local variable of my second class;

in fact i want to declare (for example) an string named "name" in second class and assign just "name" property of r_event class instance to name.
a thing like this:

string name =;

but it is not possible. How can i do this job?


You could make the fields you already have public by simply adding the keyword public. For example

public string name;
public double time;

This will allow you access them outside of the class. In truth it's better to create (public) properties than to use public fields. You could implement this by adding the following...

public string Name
    get { return name: }
    set { name = value; }

public double Time
    get { return time; }
    set { time = value; }

Hope this helps (and might I suggest you look into properties and accessibility/ access modifiers).