tags:

views:

3869

answers:

6

Hi

can anybody tell me How to store and return List of Strings.

I am asked this Because i have written a function which returns Collection of strings and i

want to prepare a COM for that one and need to consume that COM(to get the returned list ) in

vc++ where i can extend some functionality using that list of strings.

i hope thius would be clear ...

Thanks in Advance

+5  A: 

List<string> or string[] are the best options.

Here is a sample method that returns list of strings :

public static List<string> GetCities()
{
  List<string> cities = new List<string>();
  cities.Add("Istanbul");
  cities.Add("Athens");
  cities.Add("Sofia");
  return cities;
}
Canavar
I did like the same. But i prepared a COM for this and want to get That return values in c++ where i have to use this returned values.But i am geting error that GetCities(as example) is not a method of corresponding interface. Guide me to resolve this one
Cute
+2  A: 

You can store a fixed list of strings as an array:

string[] myStrings = {"Hello", "World"};

Or a dynamic list as a List<string>:

List<string> myStrings = new List<string>();
myStrings.Add("Hello");
myStrings.Add("World");
Blixt
+2  A: 

In C# you can simply return List<string>, but you may want to return IEnumerable<string> instead as it allows for lazy evaluation.

Brian Rasmussen
(the lazy evaluation only really being useful if backed by a lazy source)
Marc Gravell
@Marc: Certainly, but if you return List<string> you don't have the option even with a lazy source.
Brian Rasmussen
@Marc - or if you want the option in the future of switching to a lazy source :)
Daniel Earwicker
+1  A: 

Yesterday you asked how to do this via COM interop! Why the step backward?

http://stackoverflow.com/questions/1032060/how-to-return-a-collection-of-strings-from-c-to-c-via-com-interop

Daniel Earwicker
I get a downvote for reminding the user to check the question they asked yesterday?! Great.
Daniel Earwicker
Still i am trying to do this one through "COM".But i am knowing my way is corrct or no
Cute
A: 
public static IList<string> GetStrings()
{
  foreach( var item in GetStringItems())
  yield return item;
}
Rony
+1  A: 

There are many ways to represent a list of strings in .NET, List<string> being the slickest. However, you can't return this to COM because:

  1. COM doesn't understand .NET Generics

  2. FxCop will tell you that it's bad practice to return an internal implementation of something (List) rather than an abstract interface (IList / IEnumerable).

Unless you want to get into really scary Variant SafeArray objects (not recommended), you need to create a 'collection' object so that your COM client can enumerate the strings.

Something like this (not compiled - this is just an example to get you started):

[COMVisible(true)]
public class CollectionOfStrings
{
  IEnumerator<string> m_enum;
  int m_count;

  public CollectionOfStrings(IEnumerable<string> list)
  { 
    m_enum = list.GetEnumerator();
    m_count = list.Count;
  }

  public int HowMany() { return m_count; }

  public bool MoveNext() { return m_enum.MoveNext(); }

  public string GetCurrent() { return m_enum.Current; }
}

(See http://msdn.microsoft.com/en-us/library/bb352856.aspx for more help)

JBRWilkinson