I am new at C# Let's say I have 4 Classes:
Holder:
public class Holder
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
SecondClass:
public class SecondClass
{
public void SecondClassMethod()
{
Holder holder = new Holder();
holder.Name = "John";
Console.WriteLine(holder.Name + " from SecondClass");
}
}
AnotherClass:
public class AnotherClass
{
public void AnotherClassMethod()
{
Holder holder = new Holder();
holder.Name = "Raphael";
Console.WriteLine(holder.Name + " from AnotherClass");
}
}
and final Program class:
class Program
{
static void Main(string[] args)
{
Holder x1 = new Holder();
SecondClass x2 = new SecondClass();
AnotherClass x3 = new AnotherClass();
x1.Name = "Nobody";
Console.WriteLine(x1.Name);
x2.SecondClassMethod();
Console.WriteLine(x1.Name);
x3.AnotherClassMethod();
Console.WriteLine(x1.Name);
Console.ReadLine();
}
}
Output after running program:
Nobody
John from SecondClass
Nobody
Raphael from AnotherClass
Nobody
My Question is: How I can get proper name (given by that other classes) using Program class ? Thing is I want to use that set,get on Holder class. I can't figure it out.
I want to get:
Nobody
John from SecondClass
John
Raphael from AnotherClass
Raphael
as output