473,386 Members | 1,969 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,386 software developers and data experts.

StringCollection.ToString() method

Naz
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
....
....
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks

Nov 17 '05 #1
8 15749
Naz wrote:
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
...
...
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks


If you just need the string appended together look at the stringbuilder
class. If you need them seperate a string collection is good or an
array of strings, depends how you need to access them.

Chris
Nov 17 '05 #2
Naz wrote:
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?
Doesn't look it. I think you'll have to use StringCollection.CopyTo(),
and then String.Join().
Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?


It's OK. It's a strongly typed wrapper for an ArrayList, so Add() ops
are reasonably efficient.

--

www.midnightbeach.com
Nov 17 '05 #3
Naz
Thanks for your response. I don't think will add any value to copy
entries from string sollection into one-dimensional array of strings.
Strings array do not have any Join() method. Seems like I have iterate
though all entries. Here is the code to copy into strings array...

String[] myArray = new String[stringCollection.Count];
stringCollection.CopyTo(myArray, 0);

Nov 17 '05 #4
Naz
"Jon Shemitz", I misunderstood your suggestions. You proably suggest
the following code. The suggestions works fine. I don't have to iterate
through all entries in StringCollection. Thanks.

String[] myArr2 = new String[stringCollection.Count];
stringCollection.CopyTo(myArr2, 0);
Console.WriteLine(string.Join(System.Environment.N ewLine, myArr2, 0,
myArr2.Length));

Nov 17 '05 #5

"Naz" <na******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
<snip>
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?


Hi,
I am not sure what you were doing wrong.
This works.
--------------------------------------------------------------------------
using System;
using System.Collections.Specialized;

class TEST
{

public static void Main(string[] args)
{
StringCollection sc = new StringCollection();
sc.Add("line1");
sc.Add("line2");
sc.Add("line3");

foreach(string entry in sc)
{
Console.WriteLine(entry);
}
}
}
------------------------------------------------------------
It produces :
line1
line2
line3

Hope this helps
Bill


Nov 17 '05 #6
Naz
Bill, I want to avoid iterate through the entries in stringcollection
and looking for any built in method which will return all entries. The
StringCollection in my program may have thousands of line.

Nov 17 '05 #7
"Naz" <na******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Bill, I want to avoid iterate through the entries in stringcollection
and looking for any built in method which will return all entries. The
StringCollection in my program may have thousands of line.


Sorry, I misunderstood the issue.

Is there any reason particular reason you wish to avoid iterating through
the collection?
Any method that did it in one shot would be iterating internally anyway?

I don't see the point of this:
-----------------------------------------------------------------------------
String[] myArr2 = new String[stringCollection.Count];
stringCollection.CopyTo(myArr2, 0);
Console.WriteLine(string.Join(System.Environment.N ewLine, myArr2, 0,
myArr2.Length));
-----------------------------------------------------------------------------
over this
-----------------------------------------------------------------------------
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

-----------------------------------------------------------------------------
The explicit loop is much cleaner and easier to understand.

Are you concerned about performance?

I am confused
Bill
Nov 17 '05 #8
I believe what you want is
using System.Text;

StringBuilder stringCollection = new StringBuilder();

stringCollection.Append("line1");
stringCollection.Append(Environment.NewLine);
stringCollection.Append("line2");
stringCollection.Append(Environment.NewLine);
stringCollection.Append("line3");
stringCollection.Append(Environment.NewLine);

Console.Write(stringCollection.ToString());

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Naz" <na******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
...
...
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks

Nov 17 '05 #9

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

Similar topics

3
by: Jonathan | last post by:
I tried using Type::GetType on S"System.Collections.Specialized.StringCollection", but it returned NULL. The only way it worked was when I used the fully qualified name, including the assembly...
2
by: Dennis Myrén | last post by:
Hi. I want to know why it is not possible to specify (or determine) the capacity for a System.Collections.Specialized.StringCollection. I guess the StringCollection would be the better choice...
2
by: Dan | last post by:
Hi, Is it possible to iterate through a StringCollection ? I have the following code but "row" always seems to contain the values of rowValues2 Thanks for any input, Danny
3
by: Ben Fidge | last post by:
Hi What are the options for sorting a StringCollection? I'm quite surprised that StringCollection doesn't have any sorting capability built in! Thanks Ben
1
by: Juan Francisco Figueroa Perez | last post by:
Hi all: I have a StringCollection property like this: public class FormExito : Form { ................ ................ private StringCollection FEditar;
0
by: Dinesh Rathi | last post by:
Hi I have a managed dll which I am using in my unmanaged application through its com interfaces. Now can I use StringCollection/ String class through com interfaces ? One of my class has a member...
3
by: Rob | last post by:
I've searched for info on how to drag and drop a group of strings (or any other object) from one control to another. Looked through articles by Dino Esposito, checked the Forms books by Sells and...
2
by: eSapient | last post by:
I have a case where a C# dll is being consumed by an unmanaged C++ executable. One of the dll methods returns a StringCollection object. What are the requirements for the C++ executable in terms of...
2
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
Why should I use StringCollection as opposed to List<string>? Thank you.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.