Define a variable with a subvariable (or with a property) - c#

The target is to have a variable with a Unit property, which could be accessed by e.g.:
Params MyParams;
MyParams.Diae = 100.0;
MyParams.Diae.Unit = "kg";
Ideally, the variable Diae is a member of Class Params and should Inherit the Unit property from a class IParameter:
internal class IParameter
{
string unit;
internal string Unit { get; set; }
internal IParameter()
{
unit = "";
}
}
How to declare Diae variable? Here is a snippet of a quasi-code of the declaration expected:
namespace SomeCode
{
internal class Params
{
double diae;
internal class Diae:IParameter
{
get { return diae; }
set { diae = value; }
}
}
}

You can't do exactly what you want -- you can't add extra properties to an existing type like double, so you'll have to make Diae return a separate class. Something like this:
internal class ParamWithUnit
{
public string Unit { get; set; }
public double Value { get; set; }
}
internal class Params
{
private ParamWithUnit diae = new ParamWithUnit();
public ParamWithUnit Diae
{
get { return this.diae; }
}
}
Then you can do:
Params MyParams = new Params();
MyParams.Diae.Value = 100.0;
MyParams.Diae.Unit = "kg";

You won't [want to] be able to assign a double to Diae while still allowing it to have a property Unit. (You could hack around this through implicit conversions or something like that, but I would discourage you from doing so.)
The best option would be to create a class such as Measurement that will have both a Value and a Unit property:
public class Measurement
{
public double Value {get;set;}
public string Unit {get;set;}
}
Then Diae can be of that type:
internal class Params
{
public Measurement Diae {get;private set}
public Params()
{
Diae = new Measurement();
}
}
Now you can do:
Params MyParams = new Params();
MyParams.Diae.Value = 100.0;
MyParams.Diae.Unit = "kg";

Related

Using generic types within a foreach loop

I really cannot figure out how to use generic types with IEnumerable so that I can iterate through values contained by a given generic value.
Consider the following class (note that the classes here are only for example purposes):
public class Parameter<T> : IParameter<T> where T : IEnumerable<T>
{
public List<UInt64> output = new List<UInt64>();
private T _value;
public T Value
{
get => ...;
set
{
// I want to be able to apply special treat to the value
// Value can be of any type: int, int[], bool, bool[]
foreach (var v in value)
{
output.Add(Convert.UInt64(v) + 5);
}
...
}
}
}
public interface IParameter<T> where T : IEnumerable<T>
{
T Value { get; set; }
}
I then have a test module that instantiate some parameters as per, but I cannot even compile here. I have even tried to replace bool[] to IEnumerable here below, but the compiler does not like it either.
public class TestModule : ModuleBase, ITestModule
{
public IParameter<bool[]> Test1 { get; set; } = new Parameter<bool[]>();
public IParameter<uint[]> Test2 { get; set; } = new Parameter<uint[]>();
...
public IParameter<int> Test3 { get; set; } = new Parameter<int>();
}
I did consider using overload for the Parameter() class, but I thought it to be overkill to create a class per supported type (considering it is only for the Value property).
Your issue is that your generic parameter is specified incorrectly.
public class Parameter<T> : IParameter<T> where T : IEnumerable<T>
implies that whatever comes in of type T is an enumerable of the same type, meaning for instance a T of type bool[] should be an IEnumerable<bool[]> which is clearly incorrect.
One way to get it to compile is this:
public class Parameter<TEnumerable, TType> : IParameter<TEnumerable, TType> where TEnumerable : IEnumerable<TType>
{
public List<ulong> output = new List<ulong>();
private TEnumerable _value;
public TEnumerable Value
{
get => { return null; }
set
{
// I want to be able to apply special treat to the value
// Value can be of any type: int, int[], bool, bool[]
foreach (Q v in value)
{
output.Add(Convert.ToUInt64(v) + 5);
}
}
}
}
public interface IParameter<TEnumerable, TType> where TEnumerable : IEnumerable<TType>
{
TEnumerable Value { get; set; }
}
public class TestModule
{
public IParameter<bool[], bool> Test1 { get; set; } = new Parameter<bool[], bool>();
public IParameter<uint[], uint> Test2 { get; set; } = new Parameter<uint[], uint>();
public IParameter<int[], int> Test3 { get; set; } = new Parameter<int[], int>();
}
As for your additional comment, no, there's no way you can avoid having to specify the two types since IEnumerable is not a T in the form you've formulated your code. You have 2 separate parameters here and as such, you will have to use 2 generic parameters if you must do it the way you've done it.
A much simpler solution to your problem would be something like this which serves the same purpose more or less, although I don't really know your requirements so this may or may not suffice (interface omitted for clarity):
public class Parameter<TType>
{
public List<ulong> output = new List<ulong>();
private IEnumerable<TType> _value;
public IEnumerable<TType> Value
{
get => { return null; }
set
{
// I want to be able to apply special treat to the value
// Value can be of any type: int, int[], bool, bool[]
foreach (TType v in value)
{
output.Add(Convert.ToUInt64(v) + 5);
}
}
}
}
public class TestModule
{
public Parameter<bool> Test1 { get; set; } = new Parameter<bool>();
public Parameter<uint> Test2 { get; set; } = new Parameter<uint>();
public Parameter<int> Test3 { get; set; } = new Parameter<int>();
}

How do I implement extension helper methods as an abstract class with variable parameters?

To cut down on reused code throughout my repository which gets values from another library, I wanted to create extension methods for "parsing"(for lack of a better word) one class to another. How do I implement abstract methods with different parameters.
I can't find anything that answers my question, and I'm not sure it can even be done.
Instead of having something like this in multiple places.
var list = _library.GetList();
var model = list.Select(o => new ClassA()
{
ID = o.ID,
Name = o.Name
}).ToList<ClassA>();
I want extension methods so I can call something like
var list = _library.GetList();
var model = ExtensionClass.ParseMany(list);
But, I want to base this off an abstract class so it can be reused by mutliple different classes, so I have
public abstract class Parser<U, T> where T : class where U : class
{
public abstract T ParseOne(U parser);
public abstract IEnumerable<T> ParseMany(IEnumerable<U> parser);
}
public class ParseA<ClassA, ClassADTO>
{
public override ClassA ParseOne(ClassADTO parser){ // }
}
But it doesn't seem that my parameter that is passed in is the actual object, it says it's a KeyValuePair and now I'm lost.
I expect to able to return a new instance based on my parameter, basically what I already do in my code multiple times.
I guess you can have a generic parser using Func. I just wrote a sample and hope it helps you.
public class ClassA
{
public int SomeNumber { get; set; }
public string SomeString { get; set; }
}
public class ClassB
{
public int OtherNumber { get; set; }
public string OtherString { get; set; }
}
public static class ExecuteParsingFunction
{
public static TDestiny Parse<TOrigin, TDestiny>(TOrigin origin,
Func<TOrigin, TDestiny> parserFunction)
{
return parserFunction(origin);
}
}
public static class ParsingFunctions
{
public static ClassB ParseAToB(ClassA a)
{
return new ClassB { OtherNumber = a.SomeNumber, OtherString = a.SomeString };
}
public static IEnumerable<ClassB> ParseManyAToB(IEnumerable<ClassA> aCollection)
{
foreach(var a in aCollection)
yield return ParseAToB(a);
}
}
public void Sample()
{
var a = new ClassA { SomeNumber = 1, SomeString = "Test" };
var manyAs = new List<ClassA> { a };
var b = ExecuteParsingFunction.Parse(a, ParserFunctions.ParseAToB);
var manyBs = ExecuteParsingFunction.Parse(manyAs, ParserFunctions.ParseManyAToB);
}

inheriting a class with parameteraized constractor to reuse base properties

i'm trying to minimize use of constractors in derived classes, and have a base class to hold common properties
so i tried as follows
public class sharedData
{
public string GlobMeta;
public int GlobValue;
public sharedData(string meta, int value)
{
GlobMeta = meta;
GlobValue= value;
}
}
public class derivedData: sharedData
{
public string test;
test = string.Format("Shared meta = {0}, Shared Value = {1}",GlobMeta, GlobValue);
}
then use
var shared = new sharedData("desc", 1);
var derived = new derivedData();
var testData = derived.test;
so when i will create instance of derived it will use base (shared) values
wich is common to all derived classes.
how can i implement this, as my idea was to use less memory with large collections of data. and also clean code and ease of use.
You have declared a based class, sharedData, without parameterless constructor. Therefore, any class that inherit that class must define a base constructor. You can't rely on the the compiler do job the job for you anymore.
public class derivedData : sharedData
{
public string test;
public derivedData(string meta, int value) : base(meta, value)
{
// you can't do assignment in the class scope, unless it can be done statically, it has to be inside a method block
test = string.Format("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
}
// or, if you prefer to have a parameterless ctor
public derivedData() : base("a default value for meta", default(int))
{
test = string.Format("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
}
}
EDIT: It seems that you are using this for global settings, in that case, you need to mark GlobMeta and GlobValue as static. Otherwise they won't be "shared" at all between different instance of sharedData/derivedData.
You should consider rewriting it as this :
public static class SharedData
{
public static string Meta { get; private set; }
public static int Value { get; private set; }
public static void SetData(string meta, int value)
{
Meta = meta;
Value = value;
}
}
public class DerivedData
{
public string Test
{
get { return string.Format("Shared meta = {0}, Shared Value = {1}", SharedData.Meta, SharedData.Value); }
}
}
EDIT 2 : If you need to have various instances of these, use the following :
public class SharedData
{
public string Meta { get; set; }
public int Value { get; set; }
public SharedData(string meta, int value)
{
Meta = meta;
Value = value;
}
}
public class DerivedData : SharedData
{
public string Test
{
get { return string.Format("Shared meta = {0}, Shared Value = {1}", Meta, Value); }
}
public DerivedData(string meta, int value) : base(meta, value)
{
}
// note: this is a copy ctor, changing data after this has been created, will not affect this.
public DerivedData(SharedData data) : base(data.Meta, data.Value)
{
}
}
I was thinking along the same lines as Xiaoy312, and wrote this while he was posting his second example:
public class SharedData
{
internal static string GlobMeta;
internal static int GlobValue;
public SharedData(string meta, int value)
{
GlobMeta = meta;
GlobValue = value;
}
public SharedData(){}
}
public class DerivedData: SharedData
{
public DerivedData() : base()
{
Console.WriteLine("Shared meta = {0}, Shared Value = {1}", GlobMeta, GlobValue);
}
}
Declaring the static variables as 'internal means they will only be accessible in the scope of 'SharedData and any classes derived from it.

Access properties of objects in an object

I have 3 classes MetaA, MetaB and MetaC. Each have a number of properties.
There are certain situations where it would be nice to just have one class that contains the properties of all three Meta classes, called for example, MetaComposite. In the composite class, I have tried creating and instance of each MetaA, B and C in the hope that I could access the properties like so:
Meta Composite mc = new MetaComposite();
mc.MetaA.Property1 = "Hello";
Since C# does not allow multiple inheritance, what is the best way of making a class that is a composite of other classes? I could put fields and write getters and setters in the composite class to pass on the property values, but this would be a lot of duplicated code.
What is the correct approach here?
How about creating interfaces for all three Meta classes and have the MetaComposite class implement all three of these interfaces. The MetaComposite class can instantiated the correct Meta class and call it to execute the desired property.
Here is an example:
public interface IMeta1
{
int Metaproperty1 {get; set;}
}
public interface IMeta2
{
int Metaproperty2 {get; set;}
}
public interface IMeta3
{
int Metaproperty3 {get; set;}
}
public class MetaComposite : IMeta1, IMeta2, IMeta3
{
private readonly Meta1 _meta1;
private readonly Meta2 _meta2;
private readonly Meta3 _meta3;
public MetaComposite()
{
_meta1 = new Meta1();
_meta2 = new Meta2();
_meta3 = new Meta3();
}
public int Property1
{
get { return _meta1.Property1; }
set { _meta1.Property1 = value; }
}
public int Property2
{
get { return _meta2.Property2; }
set { _meta2.Property2 = value; }
}
public int Property3
{
get { return _meta3.Property3; }
set { _meta3.Property3 = value; }
}
}
What is the reason to have all properties on class level?
Why not simply use exactly what you wrote?
class MetaComposite
{
public MetaAClass MetaA { get; private set; }
public MetaBClass MetaB { get; private set; }
public MetaCClass MetaC { get; private set; }
public MetaComposite()
{
MetaA = new MetaAClass();
MetaB = new MetaBClass();
MetaC = new MetaCClass();
}
}
public void Main()
{
var composite = new MetaComposite();
composite.MetaA.Field1 = 1;
composite.MetaB.Field2 = '2';
composite.MetaC.Field3 = new MetaDClass();
}

Instantiating a class given a generic abstract type

Using this model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
#region Abstracts definitions
abstract class AnAbstract
{
public string Name { get { return this.GetType().Name; } }
public bool IsNumeric { get { return this is ANumericAbstract; } }
public /*abstract*/ string Description = default(string);
}
abstract class ANumericAbstract : AnAbstract
{
public /*abstract*/ double Min = double.MinValue;
public /*abstract*/ double Max = double.MaxValue;
}
abstract class ANonNumericAbstract : AnAbstract
{
public List<Object> objects = new List<Object>();
}
#endregion Abstracts definitions
#region Concrete definitions
class NumericImpl : ANumericAbstract
{
new public const string Description = "A numeric implementation";
new public const double Min = 0;
new public const double Max = 1000;
public NumericImpl()
{
}
}
abstract class AnotherImpl : ANonNumericAbstract
{
public AnotherImpl()
{
objects.Add("one");
objects.Add("two");
objects.Add("three");
}
}
class SideA : AnotherImpl
{
new public const string Description = "Disc side A";
}
class SideB : AnotherImpl
{
new public const string Description = "Disc side B";
}
#endregion Concrete definitions
partial class Parameter
{
public string Name { get; set; }
public string Description { get; set; }
public bool IsNumeric { get; private set; }
public double Min { get; private set; }
public double Max { get; private set; }
public List<Object> Values { get; private set; }
private Parameter()
{
Values = new List<Object>();
}
}
}
With this, I pretend to define a hierarchy of classes for which I can have some abstract properties (Name,Description,IsNumeric) and at the end of the hierarchy there should be some classes which mandatorily define those properties; in the case of ANumericAbstract they should have additional specific properties, e.g. Min and Max.
Now here's the problem.
I'm attemtping to be able to create instances of Parameter which take a generic AnAbstract and read from it some values to fill in the Parameter properties, à la
Parameter<ANumericAbstract> ParamNum = new Parameter<NumericImpl>();
where a Parameter constructor would take in the passed type and "fill in the blanks". In other words, I'm trying something like:
using System;
namespace ConsoleApplication1 {
partial class Parameter
{
public static Parameter NewParameter<T>() where T : AnAbstract
{
Parameter Parameter = new Parameter();
// THESE DON'T WORK:
this.Name = T.Name;
this.Description = T.Description;
this.IsNumeric = T.IsNumeric;
if (this.IsNumeric)
{
this.Min = (T as ANumericAbstract).Min;
this.Max = (T as ANumericAbstract).Max;
}
else
{
foreach(Object val in (T as ANonNumericAbstract).Values)
{
this.Values.Add(val);
}
}
return Parameter;
}
}
class Program
{
private AnAbstract Number = new NumericImpl();
static void Main(string[] args)
{
}
// THESE DON'T WORK:
private static Parameter<ANumericAbstract> ParameterNum =
Parameter.NewParameter<NumericImpl>();
private static Parameter<ANonNumericAbstract> ParameterA =
Parameter.NewParameter<SideA>();
private static Parameter<ANonNumericAbstract> ParameterB =
Parameter.NewParameter<SideB>();
}
}
Obviously the syntax is invalid, but I'm not sure if I'm going in the right direction. Is there some Generics syntax that I'm not using properly? Should I just be done with it and use Getters and Setters à la Java? :-) At this point, just doing the
Parameter par = new Parameter { Name = NumericImpl.Name, /* ... */ };
might seem more sensible...
Firstly you should not use New keyword on your properties.
Consider virtual keyword:
abstract class AnAbstract
{
public virtual string Name { get { return this.GetType().Name; } }
public virtual string Description { get { return String.Empty; } }
}
abstract class ANumericAbstract : AnAbstract
{
public virtual double Min = double.MinValue;
}
class NumericImpl : ANumericAbstract
{
public override string Description { get { return "A numeric implementation"; } }
public override double Min { get { return 0; } }
}
1) You can place an instance of you type in Parameter constructor and have Parameter instance.
partial class Parameter
{
public Parameter(AnAbstract inputObject)
{
this.Name = inputObject.Name;
// etc
}
}
private static Parameter ParameterNum = new Parameter(new NumericImpl());
2) The second way is to use reflection to create an instance of object with initial parameters.
partial class Parameter<T> where T : AnAbstract
{
public static Parameter<T> NewParameter<T>() where T : AnAbstract
{
Parameter<T> parameter = new Parameter<T>();
AnAbstract instance = (AnAbstract)Activator.CreateInstance(typeof(T));
parameter.Name = instance.Name;
// etc
return parameter;
}
}
private static Parameter<NumericImpl> ParameterNum =
Parameter<NumericImpl>.NewParameter();
3) Make Parameter class static and create in via static constructor.
static partial class Parameter<T> where T : AnAbstract
{
public static string Name { get; set; }
//etc
}
static partial class Parameter<T> where T : AnAbstract
{
static Parameter ()
{
AnAbstract instance = (AnAbstract)Activator.CreateInstance(typeof(T));
Parameter<T>.Name = instance.Name;
//etc
}
}
In the last example you can use this class like this:
String someName = Parameter<NumericImpl>.Name;

Categories