1

I want to assign multiple variables at once in c#. I know I can assign each variable to the index of an array but am wondering if there is another alternative. The sample code I am working on is as follows:

I am using the following code to find the line with variables and return that line:

static public string linqalternative(string textfind1)
{

    var nextLine = File.ReadLines(FILENAME)
        .SkipWhile(line => !line.Contains(textfind1))
        .Skip(1)
        .First();
    string linqalternative = nextLine;
    return linqalternative;
}

Then I am calling the above function in the main subroutine as follows: The line has multiple values and it is stored in an array. Then I want new variables to be assigned the values of that array.

// Read Card C4
string stringC4 = linqalternative("C4   ISLTMT ISSSMMT");
string[] C4values = 
       stringC4.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

int ISLTMT = int.Parse(C4values[0]);
int ISSSMMT = int.Parse(C4values[1]);
int ISLTMTS = int.Parse(C4values[2]);
int ISIA = int.Parse(C4values[3]);
float RPIA = float.Parse(C4values[4]);
float RSQMIA = float.Parse(C4values[5]);
int ITRMIA = int.Parse(C4values[6]);
int ISAVEC = int.Parse(C4values[7]);

Can I assign all the variables shown above in a line or two or that is the only alternative ?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Jd Baba
  • 5,948
  • 18
  • 62
  • 96
  • I don't think you can assign it in a line or two. – Arun Ghosh Oct 07 '14 at 04:24
  • 6
    dont be stingy with the lines, its free and better for clarity ;) – meda Oct 07 '14 at 04:32
  • Just was wondering if there was any alternative. Long lines are not a problem. – Jd Baba Oct 07 '14 at 04:40
  • 1
    Wrap it in a method, pass references to it and assign them in that method. This way you **feel** you did it in one line. – Mehraban Oct 07 '14 at 04:57
  • 2
    If you have some flexibility with the variables, you could consider a Dictionary with the current variable names as keys. In that case, you could use a read-only array to map positions to names and then call ToDictionary to create the hash, though the values would have to be objects or floats. This technique, while clunky, can be useful in SOA situations to preserve method signatures. – neontapir Oct 07 '14 at 05:56
  • How about grouping variables of the same type and using commas? e.g int x = 5, y = 7, z =10; – Szymon Knop Oct 07 '14 at 07:32
  • Separate question, but why are you creating a separate variable to hold the value of `nextLine`, just to return it? – JLRishe Oct 07 '14 at 09:05

2 Answers2

0

I don't see the point in making it a one-liner. You could create a class as opposed to separate variables, and let that class contain the parsing logic:

public class C4
{
    public int ISLTMT { get; set; }
    public int ISSSMMT { get; set; }
    public int ISLTMTS { get; set; }
    // ...

    public static C4 ParseFromLine(string line)
    {
        string[] values = line.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

        return new C4
        {
            ISLTMT = int.Parse(C4values[0]),
            ISSSMMT = int.Parse(C4values[1]),
            ISLTMTS = int.Parse(C4values[2]),
            // ...
        };
    }   
}

Then using this code will be as easy as:

string stringC4 = linqalternative("C4   ISLTMT ISSSMMT");
C4 parsed = C4.ParseFromLine(stringC4);

And access the values like parsed.ISLTMT.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

To answer your question, yes it is possible to do so for variables of the same type, using basic variable declaration syntax:

int ISLTMT = int.Parse(C4values[0]), ISSSMMT = int.Parse(C4values[1]); // etc., etc. 
float RPIA = float.Parse(C4values[4]), RSQMIA = float.Parse(C4values[5]);

However, this is not very readable and it is not a recommended practice, especially for as many variables as you are trying to assign.

As @meda said in the comments, "dont be stingy with the lines, its free and better for clarity".

JLRishe
  • 99,490
  • 19
  • 131
  • 169