I created a C# code for logging error codes.
I hardcoded the error codes into a class RecordId as static ints.
public class RecordId
{
public static int UnknownCommand = 100;
public static int SoftwareVersion = 101;
public static int WarningError = 110;
public static int AbortError = 111;
// etc...
}
Having static int means that I can do RecordId.SoftwareVersion anywhere in my code, I don't actually need to instantiate the class RecordId, which is very convenient, since I want to be able to log things from different parts of the code by calling a Log class that also doesn't need instantiation (it just appends a message to a file)
The logging function is also static, being something like
public class Logger
{
public static void LogExperiment(int key, string value)
{
// Append key and value to a hardcoded filename
}
}
Then from anywhere in my code I can do
Logger.LogExperiment(RecordId.SoftwareVersion, "1.0");
This will just append 101 1.0 in a log file
I don't need instances of the classes, so I can log anywhere from my code.
Now, as the code grows, I don't want to modify the code every time I add a new RecordId, so I want to have a JSON file where I load the values into the class.
I modified the RecordId class to look like:
public class RecordIdNew
{
public String UnknownCommand { get; set; }
public String SoftwareVersion { get; set; }
public String WarningError { get; set; }
public String AbortError { get; set; }
}
The problem I see now, is that in order to populate this values from the JSON file I have to instantiate the class RecordId, whereas before I was using the values as static ints, and therefore I could call RecordId.SoftwareVersion
The question (which might be a bit open) is: Is there a way I can keep RecordId not instantiated, but access values that come from a JSON file.
Or if not possible, is there another structure that would allow me to do that?
You are looking for static constructor, i.e.
// Let's have class being static if you don't want to create instances
public static class RecordId
{
// To be on the safer side of the road, let's have readonly fields:
// once set in the static constructor they can't be changed
public static readonly int UnknownCommand;
public static readonly int SoftwareVersion;
public static readonly int WarningError;
public static readonly int AbortError;
// Static constructor, it will be called before the first read of any field
static RecordId() {
//TODO: put your logic here: read the file and assign values to the fields
}
}
Edit:
Please, have a look at the your current design, maybe you are looking for {Key, Value} pairs? E.g. Key == 100, Value == "UnknownCommand" etc.
If it's your case, try using Dictionary:
public static class RecordId {
private static readonly Dictionary<int, string> s_Names = new();
public IReadOnlyDictionary<int, string> Names => s_Names;
static RecordId() {
//TODO: Your logic here (fill in s_Names)
}
}
usage:
int code = 100;
if (RecordId.Names.TryGetValue(code, out var name))
Console.WriteLine($"{code} is {name}");
else
Console.WriteLine("Unknown code");
Assuming you can perfectly match up the static C# properties or fields to the values in the JSON, you can use ModuleInitializerAttribute to set the static properties.
public static class RecordId
{
public static int UnknownCommand { get; private set; }
public static int SoftwareVersion { get; private set; }
public static int WarningError { get; private set; }
public static int AbortError { get; private set; }
// etc...
[ModuleInitializer]
public static void Init()
{
// code to read JSON
// loop over JSON fields, matching them to
// above fields, setting their values...
}
}
This gives you a way to set the values at runtime, once, when the module loads (modules are groups of logical code in an assembly (reference)).
Module initializers are guaranteed to be run before any other access to the module; so if you reference, say, UnknownCommand anywhere, you will get the value that was read from the JSON. In fact, as Dmitry notes in the comments, the module init code is guaranteed to run period, even if no other code in the module is accessed at all. This could be a drawback if the code is slow or buggy, but useful in cases such as yours.
This does not give you a way to dynamically create the properties; that would require either code generation prior to compilation or access to the values at runtime via some sort of "Get" method coupled with a static dictionary.
Here's an article on the subject, and here's the original proposal on GitHub.
Related
I have a Configuration class that stores certain variables which serve as settings. And I'm struggling to find easiest and most optimal way to save it to file - would be weird if user had to configure it every time.
Example class:
public static Configuration
{
public static bool A = false;
public static bool B = false;
public static int C = 100;
}
Serializing collection is not an issue, but i cannot really make collection out of these variables, since they have not matching data types.
I'm certain solution is simple, but for some reason I'm stuck here.
Elaborating on my comment, you're better off converting your static class into an instance class for minimizing manual coding to store/read the property values in the future. This refactoring can be done in minutes. So do that as a first step, it shouldn't take too long to do, and a simple "Find/Replace" can fix all of your declarations everywhere in your code where you previously used "Configuration".
Keep your implementation static, but change to a single instance that you are accessing.
public class Configuration
{
private static Configuration instance;
public static Configuration Instance
{
get
{
if (instance == null)
{
instance = new Configuration();
}
return instance;
}
set
{
instance = value;
}
}
public bool A { get; set; }
public bool B { get; set; }
public int C { get; set; }
}
Do a Find/Replace where ever you declared your static class and replace "Configuration." with "Configuration.Instance.". Also, where you previously declared static properties like public static bool A; public static bool B; ... just select all of the text, do a Find/Replace and replace "static " with "".
Save/Read your data
// To Save
File.WriteAllText(#"c:\temp\myconfig.json", Newtonsoft.Json.JsonConvert.SerializeObject(Configuration.Instance));
// To Read
using (var file = File.OpenText(#"c:\temp\myconfig.json"))
{
JsonSerializer serializer = new JsonSerializer();
Configuration.Instance = (Configuration)serializer.Deserialize(file, typeof(Configuration));
}
I am a bit confused with the get set property in C#.
I have the simple code below:
using System;
class Example
{
int _number;
public int Number
{
get
{
return this._number;
}
set
{
this._number = value;
}
}
}
class Program
{
static void Main()
{
Example example = new Example();
example.Number = 5; // set { }
Console.WriteLine(example.Number); // get { }
}
}
The code above using get set properties. However, if I delete the get set code like below code, the results stay the same.
using System;
class Example
{
int _number;
public int Number;
{
}
}
class Program
{
static void Main()
{
Example example = new Example();
example.Number = 5; // set { }
Console.WriteLine(example.Number); // get { }
}
}
My query is, what is the get set code used for? In the above program, the results are same. Can you give me some simple code which show the get set usage?
In your code, Number is simply a public field, as evidenced by the semicolon (;) at the end.
public int Number;
It is not a property, you just have an empty set of brackets right underneath which led to your confusion. If you were to remove the ; then you would actually have a property that is missing it's get, and would not compile at all.
All properties need to have a getter (setters are optional). If you want to avoid writing them, you can use auto properties, which take care of the backing field without you having to get involved:
public int Number { get; set; } // No field required
Note: A common usage pattern you'll see involving auto properties is the following:
public int Number { get; private set; }
This allows for properties that can be read from anywhere, but can only be modified from within the class they belong to.
EDIT: To answer your question, the main difference between fields and properties is in encapsulation. You can read more about the general differences between fields and properties here.
However, the example you have given has one additional difference, the private set. A normal field can be written from and to throughout the program. A property with a private setter however can only be modified from inside the class it belongs to.
Example:
public class Foo
{
public int Id { get; private set; }
public string Name;
public Foo()
{
this.Id = 1; // This works!
}
}
Here, Name is a field and Id is a property with a private setter. Notice that we modify Id in the constructor and that works, because it is within the class Id belongs to. Moving outside the class however:
var foo = new Foo();
// Field (no get and set):
foo.Name = "test" // Works
string bar = foo.Name; // Works
// Property (get and *private* set)
int i = foo.Id; // Works, because get is public
foo.Id = 2; // Doesn't work, because set is private
I am sure I am not going to use the correct words to explain but I hope you understand what I am trying to accomplish.
I have a class that gets passed in dictionary collection. Is it mandatory that this passed in dictionary collection has certain keys that this class needs. I would like to be able to expose publicly those keys needed by the class, but not just as strings, but actual items. An enumeration would work but since this class is a base class I dont think that is a good solution. I need something similar to the Resources class that is generated everytime you add items to the Resources.resx file. So you that you can just go to Resources.Resources.MyLabel1, Resources.Resources.MyLabel2, etc.
Is this possible?
Thanks,
Mike
Create properties that expose your values
public class MyClass
private Dictionary<string,string> _dict;
public MyClass (Dictionary<string,string> dict)
{
_dict = dict;
}
public string FirstName { get { return _dict["FirstName"]; } }
public string LastName { get { return _dict["LastName"]; } }
...
}
Alternatively you can aslo create read/write properties
public string FirstName {
get { return _dict["FirstName"]; }
set { _dict["FirstName"] = value; }
}
You could just offer a static or const member on your class:
class PickyClass {
public static readonly string[] RequiredKeys = new[] {"length", "width"};
/// <summary>
/// Please note that you must include at least RequiredKeys in values
/// </summary>
public void Setup(Dictionary<string,string> values)
{
...
}
}
(edit) or is it important that they are addressable by name? In which case, how about:
class PickyClass {
public class RequiredKeys
{
public const string Length = "length";
public const string Width = "width";
}
/// <summary>
/// Please note that you must include every const in RequiredKeys in values
/// </summary>
public void Setup(Dictionary<string,string> values)
{
...
}
}
This sounds like a detail you should hide
public void Setup(Dictionary<string,string> values)
If values must contain certain keys, then maybe you could encode that information in your own dictionary type.
public class FussyDictionary : Dictionary<string,string> {
// You must provide these values
public FussyDictionary(string valueA, string valueB) {
Add("magicKey1", valueA);
Add("magicKey2", valueB);
}
// Override remove to stop you removing these keys?
}
In File1 I created a class with 3 strings. I created another class with a public arraylist. I want this arraylist to be dynamic and the object it contains are the class with the 3 strings.
I can access the members of the class in the file but not in a separate file.
file1
public class SensorCollection
{
public string ipAddress;
public string portNumber;
public string physicalLocation;
public DetectorCollection(string ipAddr, string portNum, string loc)
{
this.ipAddress = ipAddr;
this.portNumber = portNum;
this.physicalLocation = loc;
}
}
public class SensorCollectionArray
{
public System.Collections.ArrayList SensorArrayList;
}
...
System.Collections.ArrayList DetectorArrayList = new System.Collections.ArrayList();
...
DetectorArrayList.Add(new DetectorCollection(ipAddress, portNum, str));
So I can fill the array of classes but can't access it in a separate file.
File 2
AdvancedSettingsForm.SensorCollectionArray mainDetectorCollectionArray;
System.Collections.ArrayList arrList;
If you create a SensorCollectionArray like this:
SensorCollectionArray mySCA = new SensorCollectionArray();
Then you can access it's ArrayList like this (for example, to add an item):
mySCA.SensorArrayList.Add(mySensorCollection);
Note however, that in the code you've posted, you didn't include a constructor for the SensorCollectionArray, so the SensorArrayList will be null after instantiation. So you can either set it to a separately instantiated ArrayList, or you can create the ArrayList within your SensorCollectionArray class.
Final note: You might want to look into the generic List(of T) class if you want to create a strongly typed collection
Not entirely sure what're attempting to do, but I assume it's something like the below. Presumably, you're creating a collection of your sensors because you want to apply some rules of some kind before storing it to the collection.
"Is this a good sensor? It is? Add it to the collection!"
Otherwise, you could just use a
List<Sensor> mySensors;
and not really use a class that'll essentially doing the same things. Aside from that, like it's been mentioned there's not really a reason to use ArrayList. As Marc points out here, the most compelling reason to use ArrayList is if you're using .NET 1.1; otherwise, you should use the generic List collection and all the great things it does for you.
//Sensor.cs
public class Sensor
{
public string Ip{ get; set; }
public string Port{ get; set; }
public string PhysicalLocation{ get; set }
public Sensor(string ipAddr, string portNum, string loc)
{
Ip= ipAddr;
Port= portNum;
PhysicalLocation= loc;
}
}
//SensorCollection.cs
public class SensorCollection
{
private List<Sensor> sensors;
public Sensor this[int i]
{
get { return this.sensors[i]; }
set { this.sensors[i] = value; }
}
public IEnumerable<Sensor> Sensors
{
get{ return this.sensors; }
}
public SensorCollection()
{
sensors = new List<Sensor>();
}
public SensorCollection(string ip, string port, string location) : this()
{
this.sensors.Add(new Sensor(ip, port, location));
}
public SensorCollection(Sensor sensor) : this()
{
this.sensors.Add(sensor);
}
public void AddSensor(Sensor sensor)
{
//Determine whether or not to add it
this.sensors.Add(sensor);
}
public void RemoveSensor(Sensor sensor)
{
if (sensors.Contains(sensor))
sensors.Remove(sensor);
}
}
Edit
How do I access the ipaddress of each sensor in my dynamically created
list of classes?
var mySensors = new SensorCollection();
mySensors.AddSensor(new Sensor("1.1.1.1", "123", "Home"));
mySensors.AddSensor(new Sensor("9.9.9.9", "123", "Work"));
foreach(Sensor s in mySensors.Sensors)
Console.WriteLine(s.Ip);
I can not seem to access the members of the class in another file
Make sure they're in the same namespace, or that you include a "using" statement that includes the namespace of your classes you create.
I'm working on this project and I have a large number of "services" to execute. I need to have this service codes so I can verifiy and make decisions according to selected services by it's code.
I'm tring to define this enum and get its values from the web.config so I can change them easily.
public enum ServiceCodes
{
Transfer= Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"]),
ChangePlate= Convert.ToInt32(ConfigurationManager.AppSettings["servChangePlate"])
}
But I get this error: The expression must be constant. (because of the Conversion)
What can I do? (Or you can suggest me totally different approaches.)
Just go ahead and define a static class like this:
public static class ServiceCodes
{
readonly static int Transfer = Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"])
//...
}
The documentation states that enum values are constant. An alternative approach is to declare a class with static readonly members.
If you still need the type safety provided by an enum, you could use a slightly complex approach:
public class ServiceCodes {
public static readonly ServiceCodes Transfer = new ServiceCodes(Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"]));
public static readonly ServiceCodes ChangePlate = new ServiceCodes(Convert.ToInt32(ConfigurationManager.AppSettings["servChangePlate"]));
internal int Code {get; private set;}
private ServiceCodes(int code) {
Code = code;
}
}
Then, a method like:
public void SomeAction(ServiceCodes serviceCode) {
//....
}
could be called like this:
SomeAction(ServiceCodes.Transfer);
But, given the complexity (compared with the gain), I would go with the first approach.