0

I am reading txt file, and I would like to separate it into some parts. This example of my TXT file:

"Part error(1) devic[3].data_type(2)"
"Escape error(3) device[10].data_type(12)"

I want to achieve such a situation that, when I have first word "Part" I would like to have enum type for it, and in switch I would like to call some function that will work with whole line, and on the other hand, when I will have first word "Escape", there will another case in switch that will call other functions. How can I do it? This is my code so far:

class Row
{
    public enum Category { Part, Escape }
    public string Error{ get; set; }
    public string Data_Type { get; set; }
    public string Device{ get; set; }
}

public object HandleRegex(string items)
{
        Row sk = new Row();
        Regex r = new Regex(@"[.]");
        var newStr = r.Replace(items, @" ");

        switch(this.category)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;

        }
}
1
  • Isn't the bracket a regex language element? I think you should escape these caracters Commented Aug 5, 2013 at 6:16

4 Answers 4

2
public object HandleRegex(string items)
{        
    Regex r = new Regex(@"[.]");
    var newStr = r.Replace(items, @" ");
    try {
     category = (Category) new EnumConverter(typeof(Category)).ConvertFromString(items.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries)[0]);
        }
    catch {
       throw new ArgumentException("items doesn't contain valid prefix");
    }
    switch(category)
    {
        case Category.Part:
            HandlePart(newStr);
            break;
        case Category.Escape:
            HandleEscape(newStr);
            break;
    }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

But it has many branches. I just uploaded 2 as an example. It can even have few thousands.
@user2592968 if so, you should add more entries to Category, at least 3 is enough.
There is a mistake in items.Split(" ", StringSplitOptions.RemoveEmptyEntries). It has some invalid argument. What can it be?
1

You could use TryParse :

Category outCategory;
Enum.TryParse(this.category, out outCategory)

switch(outCategory)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;
            default:
                // Needs to be handled
        }

3 Comments

Ok, but how I should make differentiation of my two case? What I mean is, how program will know that when it will read "Part" word it will have to call this specific part of code? I am normally reading each line of my txt file.
You just need to read each line and execut the above code. Or do you need code to get the first word?
Ok, I will try this way.
1

You can create Dictionary<Category, Action<string>> and then use it to call code according to category:

static void Main(string[] args)
{
    var input = @"Part error(1) devic[3].data_type(2)
Escape error(3) device[10].data_type(12)";

    var functions = new Dictionary<Category, Action<string>>()
    {
        { Category.Part, HandlePart},
        { Category.Escape, HandleEscape }
    };

    foreach (var line in input.Split(new [] {Environment.NewLine }, StringSplitOptions.None))
    {
        Category category;
        if(Enum.TryParse<Category>(line.Substring(0, line.IndexOf(' ')), out category) && functions.ContainsKey(category))
            functions[category](line);
    }
}

static void HandlePart(string line)
{
    Console.WriteLine("Part handler call");
}

static void HandleEscape(string line)
{
    Console.WriteLine("Escape handler call");
}

Output of program above:

Part handler call
Escape handler call

Comments

0

if you read the file line by line then you can do

string str = file.ReadLine();
string firstWord = str.substring(0, str.IndexOf(' ')).Trim().ToLower();

now you have your first word you can do

switch(firstWord){
  case "escape":// your code
  case "Part":// your code
}

1 Comment

Where is the comparison to an enum?

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.