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

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

13 Experts available now in Live!

Get 1:1 Help Now