compile errors (object pointers?)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EntryTeam
    New Member
    • Aug 2009
    • 55

    compile errors (object pointers?)

    Code:
    PlayerStatus ps; 
    PlayerStatus ps1 = new PlayerStatus();
    ps = ps1; //<-
    This code causes compilation errors. Help me please to fix it.

    Invalid token ';' in class, struct, or interface member declaration (CS1519) - C:\Users\user1\ Documents\Sharp Develop Projects\v2\v2\ MainForm.cs:18, 11

    Invalid token '=' in class, struct, or interface member declaration (CS1519) - C:\Users\user1\ Documents\Sharp Develop Projects\v2\v2\ MainForm.cs:18, 6
    Another question:
    when i pass to a method class-argument, does method duplicate original argument, or uses a pointer to a real object?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    The problem might be in your PlayerStatus class... there shouldn't be anything wrong with the three lines of code you posted. Can you declare and use a variable of type PlayerStatus without the ps = psl?

    For your second question, there's a bit of trickyness here. For the most part, it does pass the reference to your object along... here's an example using a list of strings.

    Code:
    private void SomeMethod()
    {
      List<string> test = new List<string>();
      DoSomething(test);
    
      if (test != null && test.Count > 0)
      {
        foreach (string str in test)
        {
          Console.WriteLine(str);
        }
      }
      else
      {
        Console.WriteLine("List is empty!");
      }
    }
    
    private void DoSomething(List<string> list)
    {
        list.Add("item1");
        list.Add("item2");
        list.Add("item3");
        list.Add("item4");
        list.Add("item5");
    }
    In this example, the list will contain the items that were added in the method. However, if you change the DoSomething method to...

    Code:
    private void DoSomething(List<string> list)
    {
        list = new List<string>();
        list.Add("item1");
        list.Add("item2");
        list.Add("item3");
        list.Add("item4");
        list.Add("item5");
    }
    The list in SomeMethod won't be changed to the new one you created in DoSomething. In order to make this work, you need to explicitly pass the reference to test by doing the following...

    Code:
    private void DoSomething(ref List<string> list)
    Code:
    DoSomething(ref test)
    I can tell you that this is how it works, but I can't tell you why... I've just discovered this recently and frankly, it confuses me. Hopefully someone else on this forum can explain why we can pass an existing list and modify it in a method, but if we create a new instance of an object in a method it doesn't get passed back out.

    *Edit: After doing some googling, I think I understand why this is the case now. Per this MSDN article...

    A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref (or out) keyword.

    Comment

    • EntryTeam
      New Member
      • Aug 2009
      • 55

      #3
      I can tell you that this is how it works, but I can't tell you why... I've just discovered this recently and frankly, it confuses me. Hopefully someone else on this forum can explain why we can pass an existing list and modify it in a method, but if we create a new instance of an object in a method it doesn't get passed back out.
      Maybe this is because the compiler confuses argument with local variable (same name) ?

      Can you declare and use a variable of type PlayerStatus without the ps = psl?
      Yep, this works okay.

      So, this is my class, you say? There's nothing special about it, actually.

      I have one more class (WorldMap) which uses the "problemati c" PlayerStatus class. The PlayerStatus itself is primitive.

      Comment

      • EntryTeam
        New Member
        • Aug 2009
        • 55

        #4
        okay, i've found it:
        i had to move this code inside method to get rid of the errors.
        Code:
        ps = ps1;
        it's an old lame mistake i make every time =)))

        Comment

        • cloud255
          Recognized Expert Contributor
          • Jun 2008
          • 427

          #5
          Hi,

          This is really all about deep and shallow copies, which C# doesn't handle as well as an unmanaged language such as C++.

          The accepted solution to this problem is to have your class inherit from IClonable.

          In your implementation of the Clone() method you should perform binary serialization of the the object, de-serialize it and return the de-serialized object. Not very elegant is it?

          When you want a deep copy of your object you can then simply call the Clone method:

          Code:
           PlayerStatus ps; 
           PlayerStatus ps1 = new PlayerStatus();
           ps = ps1.Clone(); //ps is now a deep copy of ps1
          As for passing by reference and value:
          When you pass a parameter by value, the changes to that variable will only be in effect for the scope of the method. This is kind of like a new instance is created inside the method and is assigned the value of the parameter.

          When passing by reference you actually pass a pointer to the object. So any changes made to the object will also happen in the scope from where the function call was made. The variable never 'leaves' the original code block, the method is simply given access to alter the variable's memory block.

          See the below code for a more concrete idea:

          Code:
          void MainFormLoad(object sender, EventArgs e)
          		{
          			int x = 0;
          			byValue(x);
          			MessageBox.Show("Original Object: " + x.ToString()); //x is still 0
          			
          			byReference(ref x);
          			MessageBox.Show("Original Object:" + x.ToString()); //x is now 5
          		}	
          		
          		void byValue(int value)
          		{
          			value += 5;
          			MessageBox.Show("Inside value function: " + value.ToString());
          		}
          		
          		void byReference(ref int value)
          		{
          			value += 5;
          			MessageBox.Show("Inside reference function: " + value.ToString());
          		}
          I'm not too good at explaining stuff like this... forgive me if it is incoherent.

          Comment

          • EntryTeam
            New Member
            • Aug 2009
            • 55

            #6
            cloud255
            From now on, I'm going to use ref keyword intensivly, although, programm worked well anyway. I'll save lots of memory =))))
            Code:
            void byReference(ref int value)
            ref has to appear not only in method prototype, but also in method call, right? This is what compiler says))))
            ----
            Interesting code.
            Code:
            ps = ps1.Clone(); //ps is now a deep copy of ps1
            Actually, I only needed ps to be a reference/pointer, not copy the whole variable into ps.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              It may have gotten missed because it was at the bottom of an admittedly long, long post (sorry!) but yea I found the answer to my confusion at the following MSDN page. This page also explains your question about ref, EntryTeam. I hope it helps, I know it helped me!

              Also, what was the solution? You didn't really post enough code for me to get the context of what changed... I'm curious as to how you solved it :)

              Comment

              • EntryTeam
                New Member
                • Aug 2009
                • 55

                #8
                2GaryTexmo

                Originally posted by GaryTexmo
                Also, what was the solution? You didn't really post enough code for me to get the context of what changed... I'm curious as to how you solved it :)
                The error was appearing, 'cause this code was placed outside any method. I had this kind of errors before, just forgot 'bout it.

                Originally posted by EntryTeam
                okay, i've found it:
                i had to move this code inside method to get rid of the errors.
                Code:
                ps = ps1;
                it's an old lame mistake i make every time =)))
                P.s.:
                Originally posted by GaryTexmo
                It may have gotten missed because it was at the bottom of an admittedly long, long post (sorry!) but yea I found the answer to my confusion at the following MSDN page.
                It's my mistake, I posted reply before I finished reading your post ;)
                Thank you a lot for answering, GaryTexmo and cloud255. MSDN also appeared to be pretty helpful.

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  Oh sorry for not being clear, I just meant I didn't understand how that was your solution... I think I do now :)

                  Anyway, glad you got it resolved!

                  Comment

                  Working...