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 ?

7
  • I don't think you can assign it in a line or two. Commented Oct 7, 2014 at 4:24
  • 6
    dont be stingy with the lines, its free and better for clarity ;) Commented Oct 7, 2014 at 4:32
  • Just was wondering if there was any alternative. Long lines are not a problem. Commented Oct 7, 2014 at 4: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. Commented Oct 7, 2014 at 4: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. Commented Oct 7, 2014 at 5:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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.