0

I am working in C# converting MS Batch files to WindowsForms.

I have need to read in a list of key=value lines from text files and convert the key to a variable.

Example text
MailClass=Std
Selectivity=MailNoErrors
MailForeigns=No
ImbType=FS
Postnet=No

I would like to set these as variables to be passed between methods and forms as needed.

string MailClass = "Std";
string Selectivity = "MailNoErrors";
string MailForeign s= "No";
string ImbType = "FS";
string Postnet = "No";

I am thinking of loading them into a List[] array.

Where I am getting is how do I make them actual variables?

3
  • 4
    Maybe a Dictionary<string, string> is what you are looking for. Commented Mar 24, 2017 at 20:14
  • 1
    Why don't you tell us what is your end goal, since making it into a variable sounds like a means to achieve something more and we can help you out with that Commented Mar 24, 2017 at 20:17
  • The original batch files have calls to various functions based on the info that is read in from the text files and used as temporary environment variable. Decisions are made in the batch files by these key/value pairs. Such as If %Selectivity% = "" ? do this : or that. So, I need to do the same thing. These key/value pairs are created as the output of an Access database (eventual conversion to SQL). Commented Mar 24, 2017 at 20:24

3 Answers 3

2

using a Dictionary would be more appropriate to the task at hand.

An example would be something like this:

Dictionary<string, string> myDictionary = new Dictionary<string, string>(); // make this global
var file = File.ReadAllLines(path);
List<string> TempList = new List<string>(file);
TempList.ForEach((line) =>
{
     string[] TempArray = line.Split('=');
     myDictionary.Add(TempArray[0], TempArray[1]);
});

After you've done that then you can just access you key/value pairs from the Dictionary.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I will look this over and give it a try.
@lsieting sure thing.
0

I would take it one step forward and create a class:

public class MyVars {
    public string MailClass {get;set;}
    public string Selectivity {get;set;}
    public string MailForeigns {get;set;}
    public string ImbType {get;set;}
    public string Postnet {get;set;}
}

And initiailize it from the Dictionary in the other answer like this:

var myVars = new MyVars {
    MailClass = dict["MailClass"],
    Selectivity = dict["Selectivity"],
    MailForeign = dict["MailForeigns"],
    ImbType = dict["ImbType"],
    Postnet = dict["Postnet"]
}

Then, you are dealing with strong type entity that can easily be moved around.

You can then have different types as well, for example you might decide that Postnet is a bool and maybe another int var will come along next week.

public class MyVars {
    public string MailClass {get;set;}
    public string Selectivity {get;set;}
    public string MailForeigns {get;set;}
    public string ImbType {get;set;}

    public bool Postnet {get;set;}
    public int SomeNewVar {get;set;}
}

var myVars = new MyVars {
    MailClass = dict["MailClass"],
    Selectivity = dict["Selectivity"],
    MailForeign = dict["MailForeigns"],
    ImbType = dict["ImbType"],

    Postnet = dict["Postnet"]=="YES",
    SomeNewVar = int.Parse(dict["SomeNewVar"])
}

1 Comment

This looks like a really good way to handle what I need. I will try this as well.
-1

If you want key-value pair in your text file convert to c# property then I think you must use T4 template.

I use T4 Template for database tables to C# class creator, like as entity framework.

https://msdn.microsoft.com/en-us/library/bb126478.aspx

2 Comments

I will take a look at this T4 Template information.
Please show how you do this rather than linking to an external site.

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.