none
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)_

    Question

  • My intention is just to assign the value of 2 to the 0th or 1st element of this 8 byte array.  What am I missing here? thx.

    Byte[] toSocket = new Byte[7];
    toSocket[0] = 2 ; //msgID        Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) 
    Monday, November 16, 2009 2:50 PM

Answers

  • You can't have non-declaration statements at the class level.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by hazz Monday, November 16, 2009 4:21 PM
    Monday, November 16, 2009 3:57 PM

All replies

  • The code you posted is valid for C#.  I don't think the code itself is what is causing the problem.  I suspect you might have tried to do something like this:

    Byte[4] toSocket

    or,

    void Foo ( Byte[4] args )

    which is not valid.

    Michael Taylor - 11/16/09
    http://p3net.mvps.org
    Monday, November 16, 2009 3:00 PM
    Moderator
  • I tried this code.
    I compiles, it should be something different.

    (I compiled this code
      static void Main(string[] args)
            {
                Byte[] toSocket = new Byte[7];
                toSocket[0] = 2; //msgID
            }

    )
    Monday, November 16, 2009 3:02 PM
  • I attempted this assignment in the following context which caused the error: 

     

    public partial class Form1 : Form

    {

     

                  Byte[] toSocket = new Byte[7];

     

                   toSocket[0] = 2; //msgID            error here

    When I moved the assignment statement into a button click event, there was no error;

     

    private void button1_Click(object sender, EventArgs e)

        {

                toSocket[0] = 2;

    //msgID                no error here when I move this from class scope

    Monday, November 16, 2009 3:32 PM
  • You can't have non-declaration statements at the class level.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by hazz Monday, November 16, 2009 4:21 PM
    Monday, November 16, 2009 3:57 PM
  • thank you,
    Monday, November 16, 2009 4:21 PM