Porqqq
Porqqq

Reputation: 65

Way to set property depending on other property

So, I have this code

        Process[] processesManager = Process.GetProcesses();
        List<ProcessInfo> temporaryProcessesList = new List<ProcessInfo>();
        for (int i = 0; i < processesManager.Length; ++i)
        {
            temporaryProcessesList.Add(new ProcessInfo(processesManager[i].ProcessName, processesManager[i].Id, TimeSpan.Zero, "Group"));
        }

        processesList = temporaryProcessesList.GroupBy(d => new {d.Name}).Select(d => d.First()).ToList();

This code is used for getting current processes. Then I'm adding those procesess to temporaryProcessesList. And instead of simple string "Group" I want to set the property depending of the name of process. For example if process has name leagueoflegends.exe then I would like to set the group to "Games", if its devenv.exe I would like to set the group to "Software development".

And my question is, how to do it the simplest/best way? I was thinking about using Dictionary with string and enum. And comparing the ProcessName with string. But maybe there is better way to do it.

ProcessInfo is simple class with 4 properties and constructor.

public class ProcessInfo
{
    private string Name { get; set; }
    private int Id { get; set; }
    private TimeSpan Time { get; set; }
    private string Group { get; set; }

    public ProcessInfo(string name, int id, TimeSpan time, string group)
    {
        Name = name;
        Id = id;
        Time = time;
        Group = group;
    }
}

Upvotes: 3

Views: 82

Answers (2)

Josh K
Josh K

Reputation: 765

Maybe this is what you are looking for:

public class ProcessInfo
{
    private string _name;
    private string Name
    { 
      get { return _name; }
      set
      {
          _name = value;
          UpdateGroupName();
      }
    }
    private int Id { get; set; }
    private TimeSpan Time { get; set; }
    private string Group { get; set; }

    private void UpdateGroupName()
    {
        Group = ProcessNames::GetGroupFromProcessName(Name);
    }

    public ProcessInfo(string name, int id, TimeSpan time)
    {
        Name = name;
        Id = id;
        Time = time;
    }
}

internal static class ProcessNames
{
    private static Dictionary<string, string> _names;

    public static string GetGroupNameFromProcessName(string name)
    {
        // Make sure to add locking if there is a possibility of using
        // this from multiple threads.
        if(_names == null)
        {
            // Load dictionary from JSON file
        }

        // Return the value from the Dictionary here, if it exists.
    }
}

This design isn't perfect, but hopefully you see the idea. You could also move the update of the Group name to the constructor, but then it would not change the Group name if you set the property after construction.

Also, you could clean the interface up by using INotifyPropertyChanged and/or dependency injection. https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

Upvotes: 1

Ivan Yurchenko
Ivan Yurchenko

Reputation: 3871

Using dictionary is the best way to accomplish this:

var dictionary = new Dictionary<string, string>();
dictionary.Add("a.exe", "aGroup");
dictionary.Add("b.exe", "bGroup");

string val;
if (dictionary.TryGetValue(processName, out val))
    processInfo.Group = val;
else
    processInfo.Group = "Undefined Group";

Upvotes: 3

Related Questions