create new object of type... seems like it a reference?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jmrieman

    create new object of type... seems like it a reference?

    probably something really stupid but.... I have created a 2 classes

    public class PegBoard
    {
    public int Status;
    }
    public class BFSPegBoardNode
    {
    public PegBoard[,] BFSBoard;
    public int rowFrom;
    public int colFrom;
    }

    I have created a new object of this type
    BFSPegBoardNode CurrentNode = new BFSPegBoardNode ();
    push it on a Queue
    MyMoveQueue.Enq ueue(CurrentNod e);
    then deQueue later on
    CurrentNode = (BFSPegBoardNod e)(MyMoveQueue. Dequeue());

    What is happening is that the objects that exist on the queue are being
    changed when i edit CurrentNode

    Keep in mind... i am looking at CurrentNode... Enqueueing... Still edit
    CurrentNode... then trying to push a seaparate instance.. is this not how it
    works? is the queue just a queue of pointers or is my object declaration
    wrong?



  • KH

    #2
    RE: create new object of type... seems like it a reference?

    That's right - your queue contains pointers to the objects. In C# classes are
    reference types, passed by reference, and structs are value types, passed by
    value by defaut.


    "jmrieman" wrote:
    [color=blue]
    > probably something really stupid but.... I have created a 2 classes
    >
    > public class PegBoard
    > {
    > public int Status;
    > }
    > public class BFSPegBoardNode
    > {
    > public PegBoard[,] BFSBoard;
    > public int rowFrom;
    > public int colFrom;
    > }
    >
    > I have created a new object of this type
    > BFSPegBoardNode CurrentNode = new BFSPegBoardNode ();
    > push it on a Queue
    > MyMoveQueue.Enq ueue(CurrentNod e);
    > then deQueue later on
    > CurrentNode = (BFSPegBoardNod e)(MyMoveQueue. Dequeue());
    >
    > What is happening is that the objects that exist on the queue are being
    > changed when i edit CurrentNode
    >
    > Keep in mind... i am looking at CurrentNode... Enqueueing... Still edit
    > CurrentNode... then trying to push a seaparate instance.. is this not how it
    > works? is the queue just a queue of pointers or is my object declaration
    > wrong?
    >
    >
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      RE: create new object of type... seems like it a reference?

      KH <KH@discussions .microsoft.com> wrote:[color=blue]
      > That's right - your queue contains pointers to the objects. In C# classes are
      > reference types, passed by reference, and structs are value types, passed by
      > value by defaut.[/color]

      No, reference type expressions are passed by value by default as well.
      It's just that the value of a reference type expression is a reference.

      See http://www.pobox.com/~skeet/csharp/parameters.html

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: create new object of type... seems like it a reference?

        jmrieman <jmrieman@hottm ail.com> wrote:[color=blue]
        > probably something really stupid but.... I have created a 2 classes
        >
        > public class PegBoard
        > {
        > public int Status;
        > }
        > public class BFSPegBoardNode
        > {
        > public PegBoard[,] BFSBoard;
        > public int rowFrom;
        > public int colFrom;
        > }
        >
        > I have created a new object of this type
        > BFSPegBoardNode CurrentNode = new BFSPegBoardNode ();
        > push it on a Queue
        > MyMoveQueue.Enq ueue(CurrentNod e);
        > then deQueue later on
        > CurrentNode = (BFSPegBoardNod e)(MyMoveQueue. Dequeue());
        >
        > What is happening is that the objects that exist on the queue are being
        > changed when i edit CurrentNode
        >
        > Keep in mind... i am looking at CurrentNode... Enqueueing... Still edit
        > CurrentNode... then trying to push a seaparate instance.. is this not how it
        > works? is the queue just a queue of pointers or is my object declaration
        > wrong?[/color]

        The queue is a queue of references - all classes are reference types,
        whereas structs are value types.

        See http://www.pobox.com/~skeet/csharp/parameters.html and
        http://www.pobox.com/~skeet/csharp/memory.html for a bit of discussion
        around the subject. Some day I'll get round to writing an article just
        on types themselves...

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...