String collection class

L

Lou

MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?

How do i reference the collection items by index number?

-Lou
 
M

Martin Honnen

Lou said:
MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?


I think that should work. Do you use the exact example from MSDN or have
you changed the code? Can you show us the context of your code? Do you
get a compile time error or runtime error?
 
S

sloan

When in doubt, break it out.

public static void PrintValues3( StringCollection myCol ) {
if (null==myCol)
{
return;
}

for ( int i = 0; i < myCol.Count; i++ )
string temp = myCol ;
string displayTemp = string.Format( " {0}", temp );
Console.WriteLine( displayTemp );
Console.WriteLine();
}
 
L

Lou

The Intelligence says
string StringDictionary[string key]
its the only overload
so when I try to use an int there it says:
"Error 1 The best overloaded method match for
'System.Collections.Specialized.StringDictionary.this[string]' has some
invalid arguments "

and there is no "Item" property to access which was one of the links
suggested.



Peter Duniho said:
MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?

How do i reference the collection items by index number?


Please post a concise-but-complete code example that reliably reproduces
the problem.

This shows that the indexer for StringCollection does in fact take an
integer:
http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection.item.aspx

Pete
 
L

Lou

i did find a work around and use the CopyTo method and copy the collecion to
an array
then i have access to the element by integer.

String[] myKeys = new String[colMediaFiles.Count];

//Copy the names locally

colMediaFiles.Keys.CopyTo(myKeys, 0);

strTestId = myKeys[m_lListPtr];


sloan said:
When in doubt, break it out.

public static void PrintValues3( StringCollection myCol ) {
if (null==myCol)
{
return;
}

for ( int i = 0; i < myCol.Count; i++ )
string temp = myCol ;
string displayTemp = string.Format( " {0}", temp );
Console.WriteLine( displayTemp );
Console.WriteLine();
}


Lou said:
MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?

How do i reference the collection items by index number?

-Lou

 
G

Göran Andersson

Lou said:
The Intelligence says
string StringDictionary[string key]
its the only overload
so when I try to use an int there it says:
"Error 1 The best overloaded method match for
'System.Collections.Specialized.StringDictionary.this[string]' has some
invalid arguments "

and there is no "Item" property to access which was one of the links
suggested.

So, you are not using a StringCollection at all, but a StringDictionary.

It would have helped if you posted the code that you actually use,
instead of some other code that you don't use... ;)

Use myCol.Values to access the values by index.
Peter Duniho said:
MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?

How do i reference the collection items by index number?

Please post a concise-but-complete code example that reliably reproduces
the problem.

This shows that the indexer for StringCollection does in fact take an
integer:
http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection.item.aspx

Pete

 
S

sloan

G'ack...

He's using StringDictionary but posted a sample with StringCollection?

That's 2 minutes of my life I'll never get back.



Peter Duniho said:
The Intelligence says
string StringDictionary[string key]
its the only overload
so when I try to use an int there it says:
"Error 1 The best overloaded method match for
'System.Collections.Specialized.StringDictionary.this[string]' has some
invalid arguments "

and there is no "Item" property to access which was one of the links
suggested.

When you post code that is not actually the code you're having problems
with, then it is impossible to provide a useful answer.

That's why I specifically wrote: "Please post a concise-but-complete code
example that reliably reproduces the problem".

In particular, while you _claimed_ to be using the StringCollection class,
according to your most recent reply you were in fact using the
StringDictionary class. That's a completely different class, with
completely different semantics. In particular, it's not possible to index
a StringDictionary by integer; it only allows access by key.

Note that you can get the Values collection of the StringDictionary and
enumerate them, but that still does not provide indexed access. There are
various ways to convert to something that is indexed, but ultimately you
will still have the problem that you are treating an unordered collection
as if it were ordered.

If you want an ordered collection, use StringCollection and not
StringDictionary.

In any case, I hope that you can see why it's so important that when you
post code, you post code that is actually relevant to your question. You
failed to do that here, and it prevented at least three different people
from being able to offer a specific, useful reply.

Pete
 
F

Family Tree Mike

Lou said:
MSDN says you can access an element like:
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol );
Console.WriteLine();
}

but when I try to us:
myCol
it fails and says invalid argument. It only takes in a string?

How do i reference the collection items by index number?

-Lou


This line:
Console.WriteLine( " {0}", myCol );

should probably be:
Console.WriteLine(string.Format(" {0}", myCol);

Mike
 
P

Pavel Minaev

This line:
          Console.WriteLine( "   {0}", myCol );

should probably be:
          Console.WriteLine(string.Format("  {0}", myCol);


Why, exactly? Console.WriteLine performs formatting itself, and always
did so. An extra call to Format will do absolutely nothing useful
there.
 
F

Family Tree Mike

This line:
Console.WriteLine( " {0}", myCol );

should probably be:
Console.WriteLine(string.Format(" {0}", myCol);


Why, exactly? Console.WriteLine performs formatting itself, and always
did so. An extra call to Format will do absolutely nothing useful
there.



Temporary (hopefully :)) stupidity...
 
L

Lou

I'm sorry, really really sorry.....
I am a C# newbie.

The usefull thing I learned is how to post accurate questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top