0

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

1
  • 3
    sellOrderis a class. You'd probably want item[currentItem].sellOrderList[0].Position Commented Feb 13, 2017 at 10:17

2 Answers 2

2

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

Also sellOrderList is currently private.
Right, i will include this in the answer.
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.