0

Not sure how to do this. Simplified things for readability. Actual code does more.

Read column from file. Values are aaa, bbb, ccc.

string[] lines = System.IO.File.ReadAllLines(@"C:\mice\file.csv");

Declare/define string variables.

string aaa = "123"
string bbb = "456"
string ccc = "789"
int counter = 0;

I want to write a loop that reads each column value and prints out its string value.

While (...)
{
   Console.WriteLine(lines[counter] + " |" + lines[counter] + "|");
}

But the print out is: aaa |aaa|

Whereas, I want the print out to be: aaa |123

QUESTION: How do I adjust the 2nd part of the WriteLine to print the value of 123 instead of aaa?

Thanks.

2
  • 1
    ReadAllLines gives you rows, not columns. Commented Jul 21, 2014 at 20:07
  • @crashmstr - edited the Q. Changed columns to lines. Just symantics but yeah, using "columns" could cause some confusion. Commented Jul 21, 2014 at 20:13

3 Answers 3

2

It sounds like you're trying to dynamically get the contents of a variable by it's name, which is not possible in C#. If it were a class member you could use reflection, but an even better option is a dictionary:

Dictionary<string, string> map = new Dictionary<string, string>();
map["aaa"] = "123";
map["bbb"] = "456";
map["ccc"] = "789";

while (...)
{
   Console.WriteLine(lines[counter] + " |" + map[lines[counter]] + "|");
}
Sign up to request clarification or add additional context in comments.

1 Comment

bravo, this works, thank you! just need a ; at the end of the first line ... <string, string>();
1

Alright I think I get what you're trying to do. You have 3 values in a csv file on a line. First you need to get the lines you've done that. Then for each line you need to split it into it's comma separated columns using

string[] columns = lines[counter].Split(',');

Now you should be able to print each column of the lines with

Console.WriteLine("{0} | {1} | {2}", columns[0], columns[1], columns[2]);

1 Comment

@RAguilar - csv has 3 lines. Each line has one value. First line is "aaa". Second line is "bbb". Third line is "ccc". I also have variables with the same names with a value attached. I want to print a line with the line value and value of the variable with the same name. So I want final result as: aaa |123 and not aaa |aaa ... I think DStanley understands what I'm trying to do. I'm trying to implement his dictionary idea ... thanks.
0

You could use Reflection to get the values of your fields based on the names read from the CSV file.

Assuming your strings aaa, bbb etc are private member variables in the same class as you are reading the CSV file in you should be able to do something like this:

while (...)
{
    FieldInfo fieldInfo = this.GetType().GetField(lines[counter], BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo != null)
    {
        Console.WriteLine(string.Format("{0}|{1}|", lines[counter], fieldInfo.GetValue(this).ToString()));
    }
}

If the fields are in a different class you'll need to replace this with the correct instance to retrieve the values from. If the "fields" are properties you'll need to replace GetField with a call to GetProperty and finally if the fields are not private or instance variables you'll need to change your BindingFlags to match your requirements.

More information on reflection can be found on MSDN.

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.