Dregalia
Dregalia

Reputation: 61

Getting and setting values in simple collections and lists - C#

I'm working on a desktop application, after being out of the app dev world for a while, and I'm working on my C# instead of using VB. It's time to modernize, so excuse my lack of conceptional knowledge in answers. Been surfing SOF for the last day, and discovered that you can't use global variable arrays like we used to in VB6.. and it specifies to use a list. The goal is to make a class list, and add/update values.

I did some searching around, and found some great examples, and have gotten far, however I started following the yellow lightbulb way too much. Now I'm quasi stuck. Not saying I don't get it, but I need to know how to reference specific values in a list.

For instance:

public class WSettings
{
    public string SettingName { get; set; }
    public string SettingVal { get; set; }

    public static void Main()
    {
       List<WSettings> wSettings = new List<WSettings>();
       wSettings.Add(new WSettings { SettingName = "Port", SettingVal = "8080" });
       wSettings.Add(new WSettings { SettingName = "UpdateInterval", SettingVal = "60" });
       wSettings.Add(new WSettings { SettingName = "Baud", SettingVal = "9600" });
       wSettings.Add(new WSettings { SettingName = "bits", SettingVal = "8" });
       wSettings.Add(new WSettings { SettingName = "stopbits", SettingVal = "1" });
       wSettings.Add(new WSettings { SettingName = "parity", SettingVal = "no" });
        }

 }

I started out here. But say I wanted to update "Port" to "8181", I'd need to create a quick function to do it, so I could call it from a different part of the application.

public string GetSettingValByName()
{
    return wSetting.SettingName("Port").SettingValue;
}

public void SetValueByName(string settingvalue, string SettingName)
{
    wSetting.Settingvalue("Port") = settingvalue;
}

The question is how would I reference Settingval by SettingName in these examples? The code I posted I know does not work, but I'm guessing this is close to how it should be done. I am looking for the specific code on how to query using the string "Port", eg.. GetValueByName("Port") returns the SettingVal of "8080" or set the value for "Port".

Next question, I was reading something about if a class is specifically going to be a list, then make it a list.. or a collection, eg..

public class AppName1: List<Wsettings>

If that is the case, does anyone have a very specific example that I could follow along with to build what I'm looking for?

Thanks all

Upvotes: 0

Views: 131

Answers (3)

iamJP
iamJP

Reputation: 406

You could use a Dictionary to look up values by a key (SettingName). However, I don't think this is the best approach. You probably don't want to reference all of the settings by magic strings. Furthermore, you would benefit by storing integer values as integers instead of strings. Boolean values should be stored as booleans etc.

I would make a new class in a separate file from where Main is.

CommunicationSettings.cs

namespace MyNameSpace
{
    class CommunicationSettings
    {
      public int Port;
      public int UpdateInterval;      
      public int BaudRate;      
      public int Bits;      
      public int StopBits;      
      public bool Parity;
    }
}

In ProgramName.cs

namespace MyNameSpace
{
    class Program
    {
       static void Main()
       {
         CommunicationSettings communicationSettings = new CommunicationSettings();
         communicationSettings.Port = 8081;
         //repeat for all settings
       }
    }
}

You could also Encapsulate the values as follows:

class CommunicationSettings
{
  private int port;
  private int updateInterval;
  private int baudRate;
  private int bits;
  private int stopBits;
  private bool parity;

  public int Port { get { return port; } set { port = value; } }
  public int UpdateInterval { get { return updateInterval; } set { updateInterval = value; } }
  public int BaudRate { get { return baudRate; } set { baudRate = value; } }
  public int Bits { get { return bits; } set { bits = value; } }
  public int StopBits { get { return stopBits; } set { stopBits = value; } }
  public bool Parity { get { return parity; } set { parity = value; } }
}

To use this in other parts of the application, you will need to pass an instance of the class to the other classes that use it.

If there is only 1 copy of these settings in your application, consider using a Singleton Pattern as follows

class CommunicationSettings
{
  private int port;
  private int updateInterval;
  private int baudRate;
  private int bits;
  private int stopBits;
  private bool parity;

  public int Port { get { return port; } set { port = value; } }
  public int UpdateInterval { get { return updateInterval; } set { updateInterval = value; } }
  public int BaudRate { get { return baudRate; } set { baudRate = value; } }
  public int Bits { get { return bits; } set { bits = value; } }
  public int StopBits { get { return stopBits; } set { stopBits = value; } }
  public bool Parity { get { return parity; } set { parity = value; } }


  static CommunicationSettings _TheSettings = null;

  public static CommunicationSettings TheSettings
  {
    get
    {
      if (_TheSettings == null)
      {
        _TheSettings = new CommunicationSettings();
      }
      return _TheSettings;
    }
  }
}

Now you can use it like this in other parts of the code.

CommunicationSettings settings = CommunicationSettings.TheSettings;
settings.Port = 8081;

Upvotes: 2

Shubham Garg
Shubham Garg

Reputation: 71

SetPort will look like something below(Assuming there will be only one setting with name "Port" :

public void SetPort(string value)
        {
            wSettings.Single(x=>x.SettingName=="Port").SettingVal = value;
        }

Also if you are dealing with key value pair where key is unique consider using Dictionary It is faster as compared to list.

Upvotes: 1

Ryan S
Ryan S

Reputation: 531

It looks like you are dealing with key/value pairs. For that type of data, consider using a Dictionary, since it already has a lot of the functionality you are looking for.

class Program
{
    static Dictionary<string, string> settings = new Dictionary<string, string>();

    static void Main()
    {
        settings["Port"] = "8080";
        settings["UpdateInterval"] = "60";
        settings["Baud"] = "9600";
        settings["bits"] = "8";
        settings["stopbits"] = "1";
        settings["parity"] = "no";

        // Update a setting
        settings["Port"] = "8081";
    }
}

Upvotes: 1

Related Questions