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;

        }
}
有帮助吗?

解决方案

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;
    }
 }

其他提示

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
        }

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

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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top