Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)_
-
Monday, November 16, 2009 2:50 PMMy 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)
All Replies
-
Monday, November 16, 2009 3:00 PMModeratorThe 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:02 PM
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:32 PMI 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:57 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 4:21 PMthank you,

