0

In the below code, I am hoping to end up with both classA and Main to both thing that Main's name is "GoodBye" However, in the end, classA still things Main's name is 'Hello' while Main know's it's name has been changed to 'GoodBye'. How to I allow classA to see that Main's name has been changed to GoodBye?

Is this not allowed in C# and is deemed as unsafe and therefore you have to wrap the code inside of 'unsafe' blocks?

After testing I understand that using the reference allows classA to modify Main's name but doens't allow classA to see when Main's name has been changed.

class Program
{
    static void Main(string[] args)
    {
        string MyName = "Hello";

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        Console.WriteLine($"");

        Console.WriteLine($"Creating ClassA");
        ClassA classA = new ClassA(ref MyName);

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        classA.WriteOutMainName();

        MyName = "GoodBye";
        Console.WriteLine($"");
        Console.WriteLine($"Main changed it's name to: {MyName}");
        Console.WriteLine($"");

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        classA.WriteOutMainName();

    }
}

public class ClassA
{
    string MainName;
    public ClassA(ref string mainName)
    {
        MainName = mainName;
        mainName = "Dog";
    }

    public void WriteOutMainName()
    {
        Console.WriteLine($"ClassA thinks Main's name is: {MainName}");
    }
}

The output for the above is the following:

Main thinks Main's name is: hello

Creating ClassA
Main thinks Main's name is: hello
ClassA thinks Main's name is: hello

Main changed it's name to: GoodBye

Main thinks Main's name is: GoodBye
ClassA thinks Main's name is: hello
Press any key to continue . . .
Fractal
  • 1,748
  • 5
  • 26
  • 46
  • 1
    Make a class, give it a `Name` property, then pass an instance of that class to your `ClassA`. Voila, `ClassA` now has a reference to the owner of the Name, and can get its name at any time. –  Jul 10 '19 at 14:14
  • Possible duplicate of [How do I assign by "reference" to a class field in c#?](https://stackoverflow.com/questions/2980463/how-do-i-assign-by-reference-to-a-class-field-in-c) – Joshua Robinson Jul 10 '19 at 14:20
  • 1
    Hi Amy, that worked great. If you restate your comment as an answer I can mark this as the solution. Thanks. – Fractal Jul 10 '19 at 14:28
  • 1
    Feel free to answer it. You've already done so in your question, just move that into an answer and you're done. –  Jul 10 '19 at 15:18

1 Answers1

0

Update after testing Amy's statement above:

The below works as desired:

class Program
{
    static void Main(string[] args)
    {
        //string MyName = "Hello";

        NameHolder MyName = new NameHolder();
        MyName.Name = "hello";

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        Console.WriteLine($"");

        Console.WriteLine($"Creating ClassA");
        ClassA classA = new ClassA(MyName);

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        classA.WriteOutMainName();

        MyName.Name = "GoodBye";
        Console.WriteLine($"");
        Console.WriteLine($"Main changed it's name to: {MyName.Name}");
        Console.WriteLine($"");

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        classA.WriteOutMainName();

    }
}

public class ClassA
{
    NameHolder MainName;
    public ClassA(NameHolder mainName)
    {
        MainName = mainName;
    }

    public void WriteOutMainName()
    {
        Console.WriteLine($"ClassA thinks Main's name is: {MainName.Name}");
    }
}

public class NameHolder
{
    private string name = "";
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

The output is the following:

Main thinks Main's name is: hello

Creating ClassA

Main thinks Main's name is: hello
ClassA thinks Main's name is: hello

Main changed it's name to: GoodBye

Main thinks Main's name is: GoodBye
ClassA thinks Main's name is: GoodBye
Press any key to continue . . .

EDIT 3: you can simplify your NameHolder class if you are not doing anything special/extra within the get and set blocks:

public class NameHolder
{
    public string Name {get; set;}
}
Fractal
  • 1,748
  • 5
  • 26
  • 46