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
54 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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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.
Comment Utility
Worked. Thank you
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Alex, glad to help.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now