Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2007

Tip: Looking for answers? Try searching our database.

Jagged arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Femi - 25 Nov 2007 04:28 GMT
Hello,

Can anyone help? I get the error message below:

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

Code snippet:

Class Test{

       string [][] MyString = new string [3][];
       MyString[0] = new string[5];
}

Thanks
Arne Vajhøj - 25 Nov 2007 04:34 GMT
> Can anyone help? I get the error message below:
>  
[quoted text clipped - 8 lines]
>         MyString[0] = new string[5];
> }

Those two lines world work inside a method but not outside.

Arne
Marc Gravell - 25 Nov 2007 08:41 GMT
Further to Arne's reply, you could also use array-initializer syntax
(below), but it seems unlikely that you would generally know these
numbers at compile-time; as such, the constructor is a good place to
consider.

string[][] MyString = { new string[5], new string[2], new
string[10] };

(note: only verified on C#3; my 2.0 machine is unavailable)

Marc
Femi - 25 Nov 2007 09:41 GMT
Thank you all.

What gets me is that I followed the example on MSDN
http://msdn2.microsoft.com/en-us/library/2s05feca(VS.80).aspx

Why isn't this documented
> Further to Arne's reply, you could also use array-initializer syntax
> (below), but it seems unlikely that you would generally know these
[quoted text clipped - 7 lines]
>
> Marc
Marc Gravell - 25 Nov 2007 10:02 GMT
> Why isn't this documented
I'm assuming you mean "that you can't use regular code outside of a
method" - this is documented in many, many places. In particular the
language spec.

Re the sample - it doesn't say that "jaggedArray" is a field. The code
as presented would be perfectly valid as contents of a method, with
"jaggedArray" as a local variable.

Marc
Alun Harford - 25 Nov 2007 16:06 GMT
> Thank you all.
>
> What gets me is that I followed the example on MSDN
> http://msdn2.microsoft.com/en-us/library/2s05feca(VS.80).aspx
>
> Why isn't this documented

It is.
In the language specification. Section 17 defines what a class can contain.

Informally, a class contains a class-body, which contains 0...n
class-member-declarations, which can be any of:

constant-declaration
field-declaration
method-declaration
property-declaration
event-declaration
indexer-declaration
operator-declaration
constructor-declaration
finalizer-declaration
static-constructor-declaration
type-declaration

string [][] MyString = new string [3][];

This is a field-declaration. It happens to contain a variable-initializer.

MyString[0] = new string[5];

This is just an assignment, which is not valid here.

Alun Harford

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.