-2

How can I create a list or array where each element uses the ref keyword, so that if the value changes the list will still point to the new location in memory pointed to by the original reference?

Edit: I'm trying to create many objects whose only purpose is to hold references to other objects of different categories. look at this code from my static constructor:

normal = new Type("Normal", new Type[]{ghost}, new Type[]{rock, steel}, new Type[]{});
fighting = new Type ("Fighting", new Type[]{ghost}, new Type[]{flying, poison, bug, psychic, fairy}, new Type[]{normal, rock, steel, ice, dark});
flying = new Type ("Flying", new Type[]{}, new Type[]{rock, steel, electric}, new Type[]{bug, grass, fairy});
ground = new Type ("Ground", new Type[]{flying}, new Type[]{bug, grass}, new Type[]{poison, rock, steel, fire, electric});
rock = new Type ("Rock", new Type[]{}, new Type[]{fighting, ground, steel}, new Type[]{flying, bug, fire, ice});
bug = new Type ("Bug", new Type[]{}, new Type[]{fighting, flying, poison, ghost, steel, fire, fairy}, new Type[]{grass, psychic, dark});
ghost = new Type ("Ghost", new Type[]{normal}, new Type[]{dark}, new Type[]{ghost, psychic});
steel = new Type ("Steel", new Type[]{}, new Type[]{steel, fire, water, electric}, new Type[]{rock, ice, fairy});
fire = new Type ("Fire", new Type[]{}, new Type[]{ref Rock, ref Fire, ref Water, ref Dragon}, new Type[]{Bug, Ice, Steel, Grass});//TODO: move Ice back to the end
water = new Type ("Water", new Type[]{}, new Type[]{water, grass, dragon}, new Type[]{ground, rock, fire});
grass = new Type ("Grass", new Type[]{}, new Type[]{flying, poison, bug, steel, fire, grass, dragon}, new Type[]{ground, rock, water});
electric = new Type ("Electric", new Type[]{ground}, new Type[]{grass, electric, dragon}, new Type[]{flying, water});
psychic = new Type ("Psychic", new Type[]{dark}, new Type[]{steel, psychic}, new Type[]{fighting, poison});
ice = new Type ("Ice", new Type[]{}, new Type[]{steel, fire, water, ice}, new Type[]{flying, ground, grass, dragon});
dragon = new Type ("Dragon", new Type[]{fairy}, new Type[]{steel}, new Type[]{dragon});
dark = new Type ("Dark", new Type[]{}, new Type[]{fighting, dark, fairy}, new Type[]{ghost, psychic});
fairy = new Type ("Fairy", new Type[]{}, new Type[]{poison, steel, fire}, new Type[]{fighting, ice, dragon});

The problem is that the values in the arrays only work for objects that were created before the object currently being created. So, I'd like to pass the members of each array by ref so that they will point to the correct location in memory after being assigned.

Dustuu
  • 77
  • 2
  • 8

2 Answers2

0

Are you dealing with value types and wishing they were reference types?

If so, you have to do something unpleasant like wrap your value type in a class or do some boxing and pass that as a reference type.

Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75
  • I added an edit since you responded, could you possibly have a look at that? I think it will help make the question clearer. – Dustuu May 23 '14 at 10:36
  • Sorry, is the question "how do I create [dangling](http://msdn.microsoft.com/en-us/library/aa664769(v=vs.71).aspx) [pointers](http://msdn.microsoft.com/en-gb/library/y31yhkeb.aspx)" in C#? Are you sure about what you're doing? – Nathan Cooper May 23 '14 at 10:42
  • Well, before that code block all of those variables are created but not given a value. I'd like to create a list that contains the actual variable referenes so once the following variables are defined, the ones defined before them will point to the appropriate object the original variable now points to instead of pointing to null like they do now because the variables were null when they were passed by value. What I want is exactly the same as using the ref keyword, I just want to store a list of them. – Dustuu May 23 '14 at 10:48
  • @Dustuu You can't do that in C#. (Might be possible to do some hacks with unsafe pointers, though that is probably not the best approach) – MAV May 23 '14 at 10:48
  • @MAV Thanks, that's good to know. I just thought it made sense given the logic of the ref keyword. Kinda sad it doesn't work the way I thought, but I found a solution so it's all good. – Dustuu May 23 '14 at 10:57
0

You should make it a two step process instead of one so you get actual references to objects.

Your Type should have an interface like this:

public class Type
{
     public Type(string key)
     {
        // store key         
     }

     public void Init(Type[] first, Type[] second, Type[] third)
     {
         // store or handle the parameters
     }
}

Now your static constructor can go like this

normal = new Type("Normal");
rock = new Type ("Rock");
ghost = new Type ("Ghost");
steel = new Type ("Steel");
// other stuff here

normal.Init(new Type[]{ghost}, new Type[]{rock, steel}, new Type[]{});
// other inits here
rene
  • 41,474
  • 78
  • 114
  • 152
  • Ahh, yeah, I'd thought about doing something like that but it seemed like it would be redundant if I could just use the original references. I just finished implementing this, thanks a ton! – Dustuu May 23 '14 at 10:55