1

This is my class I created:

public class FD
{
    private char[] L;
    private char[] R;
    public char[] myL
    {
        get { return L; }
        set { L = value; }
    }
    public char[] myR
    {
        get { return R; }
        set { R = value; }
    }
}

and my function:

public FD[] DetachedRight(FD element)
    {
        int count = element.myR.Length;
        FD[] FDtemp = new FD[count];
        char[] temp = element.myR;
        char[] OneChar = new char[1];
        for (int i = 0; i < count; i++)
        {
            FDtemp[i] = element;
            OneChar[0] = temp[i];
            FDtemp[i].myR(OneChar);
        }
        for (int j = 0; j < FDtemp.Length; j++)
        {
            MessageBox.Show(new string(FDtemp[j].myR()) + "==>" + new string(FDtemp[j].myR()), "Result");
        }
        return FDtemp;
    }

This is what I want: Example: input (element parameter in code) with value element.myL = FD and element.myR = XYZ

I need: output is an array type FD, in example, it has 3 element in array:

FDtemp[0].myL = FD and FDtemp[0].myR = X;

FDtemp[1].myL = FD and FDtemp[1].myR = Y;

FDtemp[2].myL = FD and FDtemp[2].myR = Z;

In fact, the result I achieved: FDtemp[0] like FDtemp[1] like FDtemp[2], all .myL = FD, all .myR = Z (value in last for loop).

Could you help me explain and solving this?

Combo: before, when I not use char[] temp = element.myR; in my function, OneChar[0] = temp[i]; replece by OneChar[0] = element.myR[i];, at second for loop, total length of element.myR is 1, in my example, it is element.myR = "X", "XY" not exist, why? I do not understand? Please help me explain that.

ElasticCode
  • 7,311
  • 2
  • 34
  • 45
Mr. Henry
  • 25
  • 2
  • 2
    You're putting a reference to the same object, `element`, in each slot in the array. Every slot in the array has a reference to *the same object*. You need to make a copy for each slot in the array. – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 16:24
  • You need to learn the difference between a "value type" (integers are a value type) and a "reference type" (any class is always a reference type). When you assign `5` to an `int` variable, you get a copy of `5`. When you assign `element` to a variable that is a reference to `FD`, there's no copy: That variable *refers* to the same instance of `FD` that `element` referred to. That's a value type. [Does this explanation help](https://stackoverflow.com/a/5057284/424129)? – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 16:28
  • thanks for your help, Ed Plunkett. It is helpful. I will try. – Mr. Henry Apr 13 '18 at 18:46
  • so, every time I want to assign `element` to a variable that is a reference to `FD`, I must create a temp variable to store value of `element`. Is that correct? – Mr. Henry Apr 13 '18 at 18:49
  • No. You can copy a *reference* to a `FD` from one variable to another all day long, and the reference to that `FD` will still be the same reference to the same object. You must create a copy of the `FD` object itself. You have to create a new `FD`. [Make this easy by writing a copy constructor for FD](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor). – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 18:50
  • Oh, Ed Plunkett, I really understand. – Mr. Henry Apr 13 '18 at 18:56
  • How to vote 'useful comment', This is the first time I use stackoverflow. – Mr. Henry Apr 13 '18 at 19:04
  • I think you'll need more pokeymon points before you can upvote comments, but your point is taken. Glad I could help. – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 19:18

0 Answers0