4

How do I store and return a List of Strings?

I have written a function which returns a collection of strings and I want to prepare a COM for it, and need to consume it (to get the returned list) in VC++ where I can extend some functionality using that list of strings.

2
  • Please provide more information - store permanently oder just "build a list/return that list"? "Return" like as the result of a method? Commented Jun 24, 2009 at 7:18
  • 4
    It is really unclear what you mean; either this is trivially simple, or deceptively deep, depending on your intent... Commented Jun 24, 2009 at 7:18

6 Answers 6

16

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
4

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

3 Comments

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

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");

Comments

2

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)

Comments

1

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

How to return a collection of strings from C# to C++ via COM interop

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.