Creating a Method for returning a List of Strings?

Ok, so I have a List of Strings availabe within an object. But there are more than just those strings in the object. But I only need those strings to be passed as a List<string> to method. How to get those strings only?

For Example, I have a following object _Home

With tons of tons of information in it to be able to get. But I only want the following strings:

_Home.String1
_Home.String2
_Home.String3
_Home.String4
_Home.String5
_Home.String6

Instead of creating a new List<string>() and adding each value into it separately, is there a way to create a method for this to be able to just do something like this?

List<String> theStrings = CreateStringList(_Home.String1, _Home.String2, _Home.String3, _Home.String4, _Home.String5, _Home.String6);

And than just pass theStrings into another method, like this: LoadData(theStrings)

I would like to be able to use this List as follows:

private void LoadData(List<string> theStrings)
{
    theStrings.String1;
    theStrings.String2;
    // etc. etc.
}

Answers


You can create a List<string> as follows:

var theStrings = new List<string>{ _Home.String1, 
                                   _Home.String2,
                                    _Home.String3,  
                                    _Home.String4,  
                                    _Home.String5
                          }; 

Update:

When passed to another method, this will give you a list of strings without names. You could use a dictionary if you need to access each string by name, but that makes the whole thing a little bit more complicated.

If you know the order in which the strings are provided however, you can access them by their index number (I understood from your comments that this was acceptable?):

public void YourMethod(List<string> theStringsParam){
    var firstString = theStringsParam[0];
    var secondString = theStringsParam[1];
    // etc...
}

It's possible that I'm missing something here, but what you stated you want:

List theStrings = CreateStringList(_Home.String1, 
_Home.String2, _Home.String3, _Home.String4, _Home.String5, _Home.String6);

Could probably be stated without the need for yet another function... this is what I'm thinking of:

var theStrings = new List<string>{_Home.String1, _Home.String2, 
_Home.String3, _Home.String4, _Home.String5, _Home.String6};

Maybe a Hashtable would work better?

Something like:

Hashtable theStrings = new Hashtable();
theStrings.Add("String1", _Home.String1);
theStrings.Add("String2", _Home.String2);

Or with using collection initializers:

Hashtable theStrings = new Hashtable() {
    {"String1", "String1"},
    {"String2", "String2"}
};

Then you could use it like this:

private void LoadData(Hashtable theStrings)
{
    theStrings["String1"];
    theStrings["String2"];
    // etc. etc.
}

Need Your Help

Create json on page load

php html json twitter-typeahead

I am experimenting with twitter typeaheads prefetch which fetches and processes data on initialization (json in my case).

How to add only one image by touch

objective-c cocoa-touch uiimageview uiimage touchesbegan

Working on adding images to a view by touch. So far my code allows me to add the same image I select as many times as I want when I touch the screen. I want to be able to just add an image one at a