简体   繁体   English

如何在C#中创建引用集合

[英]How can I create a collection of references in C#

Ok I am having a cross language hiccup. 好吧,我正在跨语言打h。 In C# with it's great collections like List and I have: 在C#中,它具有像List这样的很棒的集合,我有:

a Map class with properties of: 具有以下属性的Map类:
List <byte[]> Images;
List <Tile> Tiles;

a Tile Class of: 图块类别:
byte[] ImageData;
int X;
int Y;

Now I want to add an image to the Map class and have the ImageData property of the Tile Classes to "reference" it. 现在,我想向Map类添加图像,并让Tile类的ImageData属性“引用”它。 I have discovered I can't just assign it Images[0]. 我发现我不能只为其分配图像[0]。 You can't have a reference to an object of a List. 您不能引用列表的对象。

My fix was to create a Dictionary. 我的解决方法是创建字典。 Is this the best way or can I somehow have a "pointer" to a collection of objects? 这是最好的方法还是我可以以某种方式为对象集合提供“指针”?

  • in response to comments if i have a bunch of tiles which have imagedata pointing to the first byte array in the List and then i change that image in the list and grab the image in the Tiles and they are the previous image assignemnt. 作为回应,如果我有一堆瓦片,其图像数据指向列表中的第一个字节数组,然后我更改列表中的图像,并在瓦片中获取图像,它们就是先前的图像分配对象。 So assign tiles to have the first image in the list, then change that image, I now expect the tile sot reference the new image. 因此,将图块分配为列表中的第一个图像,然后更改该图像,现在我希望图块可以引用新图像。 They don't. 他们没有。

FINAL REVISION - Look at gladford3x's code (I haven't mastered formatting yet). 最终修订-看一下gladford3x的代码(我还没有掌握格式设置)。 last line in Main will be Main的最后一行是
myMap.Images[0] = image2; myMap.Images [0] = image2;
well when you call the myMap.Tiles[0].ImageData it will have the data from the first byte array 好,当您调用myMap.Tiles [0] .ImageData时,它将具有来自第一个字节数组的数据

I think it's the right way to go 我认为这是正确的方法

Using a named collection within Map class 在Map类中使用命名集合

public class Map 
{
    public List<byte[]> Images;
    public Dictionary<int, Tile> Tiles;//you could use an int or a string to name the item
}

so you could set image data: 这样就可以设置图像数据:

Map yourMap = new Map();
yourMap.Tile[0].ImageData = yourByteArray;

is that what you're doing? 那是你在做什么吗?

Now I want to add an image to the Map class and have the ImageData property of the Tile Classes to "reference" it 现在,我想向Map类添加图像,并让Tile类的ImageData属性“引用”它

List<Map> list = new List<Map>();
list.Add(new Map());
list[0].ImageData = new byte[];

// Access the list later on, the same Map object is referenced.

It is a reference type so it will update. 这是一个引用类型,因此它将更新。 You can copy the list and the original reference will still update. 您可以复制列表,原始参考仍将更新。 The class holds a value type (your byte array ImageData ) however that's stored on the heap so you don't need to worry about pointer management. 该类拥有一个值类型(您的字节数组ImageData ),但是该类型存储在堆中,因此您无需担心指针管理。

I'm not sure I fully understand. 我不确定我是否完全理解。 I read over the question a few times and here is what I've come up with. 我读了几次这个问题,这就是我的想法。 The following seems to work fine for me. 以下似乎对我来说很好。 I would recommend to use this as a proof of concept and implement the appropriate interfaces such as ICollection, IEnumerator, IList or whatever will serve your needs. 我建议将其用作概念证明,并实现适当的接口,例如ICollection,IEnumerator,IList或满足您需求的任何接口。


EDIT: 编辑:

static void Main(string[] args) 
{
    Map myMap = new Map(); 
    myMap.Images = new List<byte[]>(); 
    myMap.Tiles = new List<Tile>(); 

    byte[] image = new byte[] { 1, 2, 3 }; 
    byte[] image2 = new byte[] { 10, 20, 30 }; 
    myMap.Add(image);
    myMap.Add(image2);

    byte[] image3 = new byte[] {100,200,255};
    myMap[0]=image3;

    printallmaps(myMap);
    myMap.Tiles.ForEach(c=>printall(c));
} 


public class Map 
{
    public List<byte[]> Images  { get; set; } 
    public byte[] this[int i]
    {
        get
        {
            return Tiles[i].ImageData;
        }
        set
        {
            if(i >= this.Count)
            {
                this.Insert(i,value);
            }
            else
            {
                Tiles[i].ImageData = value;
            }
        }
    }
    public List<Tile> Tiles { get; set; }
    public int Count {get {return Tiles.Count;}}
    public void Add(byte[] image)
    {
        this[this.Count] = image;
    }
    public void Insert(int x, byte[] image)
    {
        Tile tile = new Tile();
        tile.ImageData = image;
        Tiles.Insert(x,tile);
    }
} 

public class Tile 
{ 
    public byte[] ImageData; 
    int x; 
    int y; 
} 

The issue here is that your Map manages the Images, but the Tiles use them. 这里的问题是您的地图管理图像,而图块使用它们。 If I understand correctly, you don't want the Tiles to know about the Map, so you'd prefer to reference the Image directly. 如果我理解正确,则您不希望Tiles知道地图,因此您希望直接引用Image。 What you could do in this case is create a TileImage class that contains the Image data. 在这种情况下,您可以做的是创建一个包含Image数据的TileImage类。 You keep a collection of TileImage instances in your Map and pass a TileImage to each Tile. 您在地图中保留了TileImage实例的集合,并将TileImage传递给每个Tile。 Now you have a reference which stays fixed for a given Tile. 现在,您有了对于给定图块保持不变的参考。 If you want to change the image for that type of tile, you now update the Image contents of the TileImage. 如果要更改该图块类型的图像,现在可以更新TileImage的图像内容。 This creates the level of indirection that you're after. 这将创建您需要的间接级别。


public class TileImage
{
    public byte[] ImageData;
}

public class Tile
{
    public TileImage Image;
    int x;
    int y;
}

In real code, of course, you should encapsulate with properties and ideally make things immutable where possible. 当然,在实际代码中,您应该封装属性,理想情况下,使属性保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM