473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

declaring a string[] question

I have been trying to declare a string[] with a dynamic amount of elements (in C#). but everything i try gives me an error of some sort.

i have tried

Expand|Select|Wrap|Line Numbers
  1. string[] myString = new string[];
  2.  
wont compile and gives an error of "Array creation must have array size or array initializer".
Expand|Select|Wrap|Line Numbers
  1. string[] myString = null;
  2.  
will compile but will give an "Object reference not set to an instance of an object" error.
Expand|Select|Wrap|Line Numbers
  1. string[] myString;
  2.  
gives a compile error of "Use of unassigned local variable "myString""
none of these seem to work. help please.
Sep 24 '07 #1
6 1277
Shashi Sadasivan
1,435 Expert 1GB
You have to declare the size of the array.
Expand|Select|Wrap|Line Numbers
  1. string[] myString = new string[5];
you can ccreate it dynamically too i guess
Expand|Select|Wrap|Line Numbers
  1. string[] myString = new string[(i*4)];
cheers
Sep 25 '07 #2
Plater
7,872 Expert 4TB
There are many choices, if you truely CANNOT be certain of the size you will need, even at runtime, consider using something from the System.Collections namespace. (I use ArrayList, but there are plenty of others).
Once you have a populated ArrayList (or collection of some sort) they have a function that can be used to turn it into a pure array (such as string[])
Sep 25 '07 #3
r035198x
13,262 8TB
I have been trying to declare a string[] with a dynamic amount of elements (in C#). but everything i try gives me an error of some sort.

i have tried

Expand|Select|Wrap|Line Numbers
  1. string[] myString = new string[];
  2.  
wont compile and gives an error of "Array creation must have array size or array initializer".
Expand|Select|Wrap|Line Numbers
  1. string[] myString = null;
  2.  
will compile but will give an "Object reference not set to an instance of an object" error.
Expand|Select|Wrap|Line Numbers
  1. string[] myString;
  2.  
gives a compile error of "Use of unassigned local variable "myString""
none of these seem to work. help please.
If your C# textbook has no section for declaring and initializing arrays, then shame on its author and shame on the guy who recommended that book to you.
Sep 25 '07 #4
Shashi Sadasivan
1,435 Expert 1GB
If your C# textbook has no section for declaring and initializing arrays, then shame on its author and shame on the guy who recommended that book to you.
Sometimes its an advantage to have learnt C :P
You learn to debug that way.
Sep 25 '07 #5
I found a workaround for it, the project i am working on requires me to load data from a database into an array, so i first made a query to the table to find out how many records it held, then used that number in the declaration of the array.

it works for now. also the book i have been using is "Sams teach yourself c# in 21 days" though i've been using the net a lot more than the book (the book only barely scratches the surface of winforms).
Sep 26 '07 #6
r035198x
13,262 8TB
I found a workaround for it, the project i am working on requires me to load data from a database into an array, so i first made a query to the table to find out how many records it held, then used that number in the declaration of the array.

it works for now. also the book i have been using is "Sams teach yourself c# in 21 days" though i've been using the net a lot more than the book (the book only barely scratches the surface of winforms).
Don't connect to the database twice to get one set of data. Just use an ArrayList instead. If you really need to use an array you can create the array from the ArrayList. If Sam didn't talk about dynamic arrays in his book then shame on him.
Sep 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module (user-defined types). The point is that for one of...
3
by: darrel | last post by:
Often, I want to use a string in multiple functions on a page. I can declare and re-set the string in each function, or I can just delcare the string outside of the individual functions so that...
1
by: John Dann | last post by:
Is there a way of declaring an array of a structure where the structure has a constructor? So if I had a structure say Friend MyStructure prop1 as integer prop2 as string sub New (p1 as...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.