Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
108 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.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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