简体   繁体   中英

C# Class (array?) issues

I'm kinda new to coding C# and I was trying to find a way to easely access values like:

item[currentItem].sellOrder[0].Position

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. 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:

error CS0119: 'Item.sellOrder' is a type, which is not valid in the given context

Thank you in advance, Regards

Your collection is stored in the property sellOrderList , so use this property for access:

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 ). 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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