473,670 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: String array

Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
Sep 19 '06 #1
10 2387
"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netwrot e in
message news:eD******** ******@TK2MSFTN GP04.phx.gbl...
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin
You're right, you can't fill it with more than one element, but as a string
array, the single element you CAN fill it with can be as many characters as
you want (within reason).
So:

string[] myStringArray = new string[0];
myStringArray[0]="A Nice Long string could quite happily go here...";
myStringArray[1]="Another string? UhOh, it is not going to like this...";

The second assignment would cause a runtime error as the array has only been
defined with a single element.

Maybe you are thinking of the fact that a string is kind of an array of
characters...?

Chris.
Sep 19 '06 #2
Hi Martin,

If you need to add and/or remove lines from an array, use an ArrayList or
a generic List<Tinstead.

For regular arrays size (length) is locked upon creation and cannot be
changed.
On Tue, 19 Sep 2006 15:39:05 +0200, Visual Systems AB (Martin Arvidsson)
<ma************ **@vsab.netwrot e:
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 19 '06 #3
"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netwrot e
in message news:eD******** ******@TK2MSFTN GP04.phx.gbl...
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?
Actually, you can't put anything in it...it has a size of 0
new string[1] would hold one string
new string[0] would hold zero strings

Suppose you need an array but you don't know how many elements it will
hold. For whatever reason you don't wish to use an ArrayList or some
other dynamic collection. a size zero array makes a better default than
a null reference. Once you know the data you can replace the empty array
with an array of size 0 containing real data

then you could write something like this without a check for null
foreach (string str in myStringArray)
{
Blah();
}

This will work even if the size of the array is 0.

Hope this helps
Bill

Sep 19 '06 #4
Martin

For a start you've created an array with no space, although that may have
been your intention. In C# you set the initial size of the array by setting
how many elements you want and not the upper bound ordinal. To "expand" the
array you'll need to actually create a new instance of the array with more
elements and copy the contents across.

string[] myArray = new string[1];

myArray[0] = "x";

string[] tempArray = new string[2];

myArray.CopyTo( tempArray, 0 );

myArray = tempArray;

myArray[1] = "y";

Not my best code and not very efficient, but hopefully you get the idea.

Personally, if I don't know the size of an array I either use an ArrayList
..net 1.x or, in .Net 2 a generic like List<string>

List<stringmyLi st = new List<string>();
myList.Add( "x" );
myList.Add( "y" );

//if you want it as a string[] array...

string[] myArray = myList.ToArray( );

..Net provides a whole bunch of different types of collections for you to use
built for different tasks. See the System.Collecti ons namespace
documentation.

HTH

Glenn

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netwrot e in
message news:eD******** ******@TK2MSFTN GP04.phx.gbl...
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

Sep 19 '06 #5
Hi,

you can't change the size of an array instance.
If you want a growning list you should use ArrayList or List<string>

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netschr ieb
im Newsbeitrag news:eD******** ******@TK2MSFTN GP04.phx.gbl...
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

Sep 19 '06 #6
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(ne w char[] ';');

will fill my array with x data from my data? How is that possible if the
string created can't contain any elements?

/Martin

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netskre v i
meddelandet news:eD******** ******@TK2MSFTN GP04.phx.gbl...
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

Sep 19 '06 #7
Hi, loo inside,

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netschr ieb
im Newsbeitrag news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(ne w char[] ';');

will fill my array with x data from my data?
No it wan't. It creates a new Array and asigns it to the variable.
The array created in the first statement will be lost (and soon
collected by the GC.

test this:
string[] array1 = new string[0];
string[] array2 = array1;
array1 = MyData.Split(ne w char[] {';'});

array2 will still be empty.
How is that possible if the string created can't contain any elements?

/Martin

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netskre v i
meddelandet news:eD******** ******@TK2MSFTN GP04.phx.gbl...
>Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin


Sep 19 '06 #8
Split creates a new instance of a string array of the appropriate size for
the parsed data.

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netwrot e in
message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Hmmm!

if i do like this...

string[] myStringArray = new string[0];
myStringArray = MyData.Split(ne w char[] ';');

will fill my array with x data from my data? How is that possible if the
string created can't contain any elements?

/Martin

"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netskre v i
meddelandet news:eD******** ******@TK2MSFTN GP04.phx.gbl...
>Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin


Sep 19 '06 #9

"ChrisM" <ch************ **@suedeyahoo.c omwrote in message
news:uv******** ******@TK2MSFTN GP06.phx.gbl...
"Visual Systems AB (Martin Arvidsson)" <ma************ **@vsab.netwrot e
in message news:eD******** ******@TK2MSFTN GP04.phx.gbl...
>Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

You're right, you can't fill it with more than one element, but as a
string array, the single element you CAN fill it with can be as many
characters as you want (within reason).
So:

string[] myStringArray = new string[0];
myStringArray[0]="A Nice Long string could quite happily go here...";
myStringArray[1]="Another string? UhOh, it is not going to like this...";

The second assignment would cause a runtime error as the array has only
been defined with a single element.

Maybe you are thinking of the fact that a string is kind of an array of
characters...?

Chris.
Oops that'll teach me to post without thinking. As has already been pointed
out, the declaration in the above code creates a zero length array.
Although it seems like your question has already been answered, to make what
I wrote correct, it should be:
string[] myStringArray = new string[1];

Chris.
Sep 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
17410
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4353
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
5387
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
22
17170
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
8807
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
14
15026
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
8
13944
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of the array can vary depending on the parameters passed. What I need to do is loop through the returned array and if applicable write the array element to the screen.
17
4654
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
11
17638
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
14
4069
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
8388
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.