-4

I am trying to create an array of lists of structs. currently I would initialise the list of structs like so:

 List<myStruct> myData = new List<myStruct>();

But when I try to create an array (example being an array with 1 element), ie

 List<myStruct>[] myData = new List<myStruct>[1];

 myData[0] = new List<myStruct>();

I get an error saying

myData is a field but is being treated like a type.

I've looked at this answer but don't understand what the difference is: Answer

Is there some fundamental difference to how C# treats structs, compared to integers?

Thanks for your help.

Included for clarity, my code in full:

namespace My_Project
{  
    using TradingTechnologies.TTAPI;

    public partial class Form1 : Form
    {
        List<TimeAndSalesData>[] myData = new List<TimeAndSalesData>[10];
        //Here is where I believe I am making a mistake:
        myData[1] = new List<TimeAndSalesData>();
        //I get the error myData is a field but is being used as a type.


        public Form1()
        {
            InitializeComponent();
        }
    }
}
namespace TradingTechnologies.TTAPI
{
    // Summary:
    //     Represents a single trade transaction for an Instrument
    public struct TimeAndSalesData
    {
        public TimeAndSalesData(Instrument instrument, bool isOverTheCounter, Price tradePrice, Quantity tradeQuantity, TradeDirection direction, DateTime timestamp);

        // Summary:
        //     Gets the side for a trade
        public TradeDirection Direction { get; }
        //
        // Summary:
        //     Gets the Instrument associated with this trade transaction
        public Instrument Instrument { get; }
        //
        // Summary:
        //     Gets whether the trade is over-the-counter (OTC)
        public bool IsOverTheCounter { get; }
        //
        // Summary:
        //     Gets the time the trade occurred
        public DateTime TimeStamp { get; }
        //
        // Summary:
        //     Gets the price at which this trade occurred
        public Price TradePrice { get; }
        //
        // Summary:
        //     Gets the quantity traded in this trade transaction
        public Quantity TradeQuantity { get; }
    }
}
Community
  • 1
  • 1
KenInLondon
  • 33
  • 1
  • 1
  • 4
  • 3
    Is the difference between `myStruct` and `TimeAndSalesData` intentional? – BradleyDotNET Jun 18 '14 at 17:06
  • 1
    Do you have a field called `myStruct`, instead of a type called `myStruct`? It's naming convention would indicate that it's a variable, not a type. – Servy Jun 18 '14 at 17:08
  • 1
    Are you sure this is your exact code? In conjunction with the error message it looks wrong in several ways. – David Jun 18 '14 at 17:09
  • 1
    At the very least post valid and coherent code. – H H Jun 18 '14 at 17:22
  • Show the entire block of code - your method from which you have taken such a snippet – Eugene Podskal Jun 18 '14 at 18:43
  • 1
    my original code was incorrect it should have read: List[] myData = new List[1]; – KenInLondon Jun 18 '14 at 18:43
  • Show the definition of myStruct and nonetheless give the entire methods body. – Eugene Podskal Jun 18 '14 at 18:44
  • 1
    @user3752393 The first thing to do is search the error you're getting. If the compiler says "you're attempting for xyz, but I'm capable of only abc", you search the exact phrase. There must be a thousand threads already. Here is one: [is-a-field-and-used-like-a-type-error](http://stackoverflow.com/questions/16578844/is-a-field-and-used-like-a-type-error) – nawfal Jun 19 '14 at 05:31

1 Answers1

1

You have the code just floating around inside the class definition. It needs to be inside of a method or constructor of some sort:

public partial class Form1 : System.Windows.Forms.Form
{
    public Form1()
    {
        InitializeComponent();

        List<TimeAndSalesData>[] myData = new List<TimeAndSalesData>[10];
        myData[1] = new List<TimeAndSalesData>();
    }
}

If you want myData to be a field and not a local variable, then you need to just declare it at the top level of the class, but initialize it in a constructor/method.

Servy
  • 202,030
  • 26
  • 332
  • 449