0

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.
}
7
  • This isnt going to work, strings are immutable so you wont be able to change them inside your function in the way I suspect you want to Commented Mar 13, 2014 at 17:19
  • Do you mean those are individual fields in your class? Is there any reason they're not in a List<string> or something similar already? Commented Mar 13, 2014 at 17:19
  • I'm sorry, no, I don't want to change the String values, I only want to get them in the function. My bad, was a Brain Fart. I fixed the Question. Commented Mar 13, 2014 at 17:20
  • Note that you won't be able to use a List<string> as theStrings.String1... but you would be able to use theStrings[0] etc. Commented Mar 13, 2014 at 17:20
  • Ok, that would be an acceptable answer also than. Commented Mar 13, 2014 at 17:21

3 Answers 3

2

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

7 Comments

Ok, but can you explain on how to retrieve these values in the LoadData method?
I've added an example of getting the strings from within a method, assuming theStringsParam is the name of a List<string> that has been passed to than method.
Ok, thanks a million! This is a huge, didn't know you can create a List like that, thought it had to be done with .Add. Nice!!!
So, you have to put Param after the name of the List in order to get the index value?
Um... no, not exactly. Updated my code again. Maybe it was a bad choice for name, but it was in any case just meant to be the name of the parameter. Should be clear enough now! :)
|
2

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};

Comments

2

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.
}

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.