Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,959 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,606 people online right now. Registration is fast and FREE... Join Now!




Structures... assigned by value or reference?

 
Reply to this topicStart new topic

Structures... assigned by value or reference?, nOOb question that Ive completely forgotten right now

hyperionza
16 May, 2008 - 04:02 PM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
This is what should be a really stupid question... but for some reason i cant do that in my current state of mind...

CODE

public Position Deal(ref State[] gs, ref GameInfo info)
        {
            //randomly select the first dealer of the game
            Position dealer = (Position)r.Next(0, 3);
            //initialise the game states for each player
            gs = new State[4];
            VisibleCards vc = new VisibleCards();
            vc.cardsInPlay = new Card[4];
            vc.cardsPlayed = new Card[52];
            for (int i = 0; i < 4;i++)
            {
                gs[i].cardsInHand = new Playable[13];
                gs[i].visibleCards = vc;
                gs[i].Player = (Position)i;
                gs[i].Tricks=0;
                gs[i].nodeOfPlay = null;
                gs[i].Info = info;
            }


the question is: if i change the .Info or .visibleCards property of any of the 4 States, will it reflect in all of the others as well? would it behave as a static data type would in c++? its 2AM and im stuffed

State, GameInfo and VisibleCards are all structures.

Thanks all and sundry who will reply
Goodnight
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Structures... Assigned By Value Or Reference?
16 May, 2008 - 07:48 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,016



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
I'm guess you have some C++ experience? You're doing two things you may wish to avoid. Structs are bad, refs are bad; never use them again. wink2.gif Or, rather, you never have to use them unless you're talking to some external DLL COM+ mess. For your own stuff, there's never really any need. Also, all those arrays... collections are your friend, even in C++.

Every State struct in the array points to the same instance of VisibleCards, so if you say:
CODE

gs[0].visibleCards.cardsInPlay = new Card[4];


The rest of the your gs[x].visibleCards will reflect the change, as you've stated it. However, if you should say something like:
CODE

gs[1].visibleCards = new VisibleCards();


You will be referencing a different instance in that one element. Hope that makes sense.

User is online!Profile CardPM
+Quote Post

hyperionza
RE: Structures... Assigned By Value Or Reference?
17 May, 2008 - 02:58 AM
Post #3

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
thanks, thats exactly what i intend, that all gs[x].visibleCards are the same, so that each agent can see the same set of visible cards as each other agent. their own cards however remain private somewhere else.

The only reason im using structs is that they are for a set of variables and objects, to group them. otherwise i dont normally use them. never really played with collections that much, i like my arrays for simple collections of a small number of items. but ill look into it if ive got time, right now its scarce. as soon as set gets too large i tend to go for search trees of sorts, depending on what i need, currently got a linked general tree for knowledge representation... but i havent finished the agents yet so i havent tested it crazy.gif

thanks again.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:23AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month