简体   繁体   English

C#类(数组?)问题

[英]C# Class (array?) issues

I'm kinda new to coding C# and I was trying to find a way to easely access values like: 我对C#编码有点陌生,我正试图找到一种轻松访问值的方法,例如:

item[currentItem].sellOrder[0].Position item [currentItem] .sellOrder [0]。位置

But since I've never worked much with classes and C# I'm a bit lost and have no idea why this isn't working or what I'm doing wrong. 但是由于我从没在类和C#上做过很多工作,所以我有点迷茫,不知道为什么这不起作用或我做错了什么。 Can someone give me a bit of help? 有人可以给我些帮助吗?

public class Item {
    public int Pos {get;set;}
    public string Name {get;set;}
    public int Placement {get;set;}
    public int socount = 0;
    public class sellOrder {
        public int Position {get;set;}
        public int Quantity {get;set;}
        public float Price {get;set;}
        public sellOrder(int p, int q, float v) { Position = p; Quantity = q; Price = v; }
    }
    sellOrder[] sellOrderList;
    public Item(int p, string n) {  Pos = p; Name = n; Placement = -1;  }
    public void addSellOrder(int p, int q, float v) {
        sellOrderList[socount] = new sellOrder(p, q, v);
        socount++;
    }

}

Sample: 样品:

Item[] item;
item = new Item[1];
item[0] = new Item(0, "testitem");
item[0].addSellOrder(1, 2, 10.2);

but when I try to access item[currentItem].sellOrder[0].Position I get: 但是当我尝试访问item [currentItem] .sellOrder [0] .Position时,我得到:

error CS0119: 'Item.sellOrder' is a type, which is not valid in the given context 错误CS0119:“ Item.sellOrder”是一种类型,在给定的上下文中无效

Thank you in advance, Regards 预先感谢您,问候

Your collection is stored in the property sellOrderList , so use this property for access: 您的收藏存储在属性sellOrderList ,因此请使用此属性进行访问:

item[currentItem].sellOrderList[0].Position

Edit : To allow access to sellOrderList from outside the Item class, you have to make this property public (or potentially internal ). 编辑 :要允许从Item类外部访问sellOrderList ,必须将此属性设置为public (或可能是internal )。 Maybe you also want to provide a getter and setter for this. 也许您还想为此提供一个吸气剂和吸气剂。

public sellOrder[] sellOrderList { get; set; }

You could encapsulate a list and expose it as array (just for readonly propouses). 您可以封装列表并将其公开为数组(仅适用于只读属性)。 For sample (see the comments. I've refactored your code too): 示例(请参阅注释。我也重构了您的代码):

public class Item {
    public int Pos { get; set; }
    public string Name { get; set; }
    public int Placement { get; set; }        

    // define a private list
    private List<SellOrder> _sellOrders;    

    // define a property to encapsulate and return the list as an array, just for readonly
    public SellOrder[] SellOrderList { get { return _sellOrders.ToArray(); } }

    public Item(int p, string n) {  
        Pos = p; 
        Name = n; 
        Placement = -1;  

        // init the list on the constructor
        _sellOrders = new List<SellOrder>();
    }

    // add elements on the list
    public void addSellOrder(int p, int q, float v) {
        _sellOrders.Add(new sellOrder(p, q, v));
    }

    public class SellOrder {
        public int Position { get; set; }
        public int Quantity { get; set; }
        public float Price { get; set; }
        public sellOrder(int p, int q, float v) { 
           Position = p; 
           Quantity = q; 
           Price = v; 
        }
    }
}

To access it, you could just use: 要访问它,您可以使用:

var position = item[currentItem].SellOrderList[index].Position;

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

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