Solved

asp.net c# read lines of text file and put each row in mutliple variables variables not updated

Posted on 2016-09-30
3
79 Views
Last Modified: 2016-09-30
About this code you helped me here:

List<string> contents = File.ReadAllLines(@"C:\Working Directory\test.txt").ToList();
Dictionary<string, string> vars = new Dictionary<string, string>();
foreach( string line in contents ) {
    string[] parts = line.Split(new char[] {'='}, StringSplitOptions.RemoveEmptyEntries);
    string key = parts[0].Substring(4).Trim();
    string value = parts[1].Replace("'", "").Trim();
    vars.Add ( key, value );
}

Open in new window


To read each line and gives the var the value in a string variable of similar items like:

var p1='Question 1'
var a1a='Answer1 1'
var a1b='Answer1 2'
var a1c='Answer1 3'
var a1d='Answer1 4'
var e1a='Incorrect'
var e1b='Incorrect'
var e1c='Incorrect'
var e1d='Correct'

Open in new window


I tested and after your "vars.Add(key, value);" I added for example one supposed created variable respe1.Text = e1d; and e11d does not have any value but if I check "key" and "value" both have values. Then how could you update the key and value to obtain the key the value like a variable? In other words like example make something like e1d="Correct" if entered manually.  Maybe after vars.Add requires an update or something?
0
Comment
Question by:Alex E.
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41823943
Hi Alex;

As stated in the last thread you can not add variables on the fly they need to be known at compile time. I said that a Dictionary can be use but it can not be used as an standard variable. A Dictionary needs to have a unique key just like a variable must be unique in the scope it is defined in. Therefore to modify a value you need to check to see if the key exist if it does then you can modify it if it does not exist then you would need to add the new key and value. The following code snippet will show how to do this.
// First check to see if the key exist or not
if( vars.ContainsKey( "e10d" ) ) {
    // The key exist and so we can change its value
    vars["e10d"] = "New Value";
} else {
    // The key does not exist so we need to add the key and value
    vars.Add ( "e10d", "The New Value" );
}

Open in new window

0
 

Author Closing Comment

by:Alex E.
ID: 41824242
Worked. Thank you
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41824256
Not a problem Alex, glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question