Initialization with null-coalescing operator? - c#

I may not have a good grasp of the ?? operator yet and ran into a design flaw I couldn't explain.
Compare the following two properties, the only difference being how there are initialized: the first explicitly initialized, while the second with the ?? operator (or am I doing it wrong here?).
If I run data init with both properties, the collection based on the first property comes up populated as expected, while the second one with the ?? operator never gets populated and comes up with 0 elements in the collection.
Surely something is wrong here in my assumption; what is the flaw here?
P.S. Please ignore the Set method which is to implement INotifyPropertyChanged in the base class and has no bearing on this issue (which is confined to the type of initialization).
// property version 1
private ObservableCollection<UserName> _userNameColl = new ObservableCollection<UserName>();
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl; }
set { Set(ref _userNameColl, value); }
}
// property version 2
private ObservableCollection<UserName> _userNameColl;
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl ?? new ObservableCollection<UserName>(); }
set { Set(ref _userNameColl, value); }
}
// a simple class for creating object collection
public class UserName
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
// a simple test populating the collection
for (int i = 0; i < 4; i++)
{
// silly data init just for test
UserNameColl.Add(new UserName()
{
Name = $"UserName No {i}",
Age = 20 + i,
Email = $"email{i}#local.lan"
});
}

The second one never initializes your field but always returns a new collection. Try this one instead:
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl ?? (_userNameColl = new ObservableCollection<UserName>()); }
set { Set(ref _userNameColl, value); }
}

Related

C# - Is it possible to tell whether a property has been initialized with its default value or not initialized at all?

Say we work with this class:
public class UsefulClass
{
public string A { get; set; }
public string B { get; set; }
public int? C { get; set; }
public int? D { get; set; }
public decimal E { get; set; }
public decimal F { get; set; }
}
Let's consider the following instance:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
At this point, z_objUsefulInstance.A and C are null, E is 0, B, D and F have not been initialized.
Is there a way to tell, automatically, which properties of z_objUsefulInstance haven't been initialized and which ones have been initialized with null or 0?
EDIT: by popular demand, why I need this: to emulate a system of database access akin to EntityFramework. Right now all properties are a specific generic type, so it's rather easy to know which is null and which is Generic<T>.HasNullValue == true. But that generic type causes various issues and now we'd like to get rid of it, particularly as we have grown more conversant with Expressions.
Is there a way to tell, automatically, which properties of z_objUsefulInstance haven't been initialized and which ones have been initialized with null or 0?
You can't really know in ways that you can easily inspect at runtime what properties have been set unless you intercept the property setter and set some sort of flag. from a first-principals perspective that would resemble something like this:
public class UsefulClass
{
public string A { get => _a; set { _a = value; A_Set = true; } }
private string _a;
private bool A_Set = false;
public string B { get => _b; set { _b = value; B_Set = true; } }
private string _b;
private bool B_Set = false;
public int? C { get => _c; set { _c = value; C_Set = true; } }
private string _c;
private bool C_Set = false;
public int? D { get => _d; set { _d = value; D_Set = true; } }
private string _d;
private bool D_Set = false;
public decimal E { get => _e; set { _e = value; E_Set = true; } }
private string _e;
private bool E_Set = false;
public decimal F { get => _f; set { _f = value; F_Set = true; } }
private string _f;
private bool F_Set = false;
}
It is pretty verbose, but you can see here how we are not comparing the value at all, we can determine definitively if each property has been set, thought not specifically during the initialization of the instance, this simple code only tracks if each property was set at all.
So after your init, we can inspect these new flags:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
Console.WriteLine(z.C_Set); // True
Console.WriteLine(z.D_Set); // False
We can simplify this with a dictionary for the backing store and helper methods to get and set the property values, we can even encapsulate that logic in a base class to make this easier to consume:
public class UsefulClass : PropertyTracker
{
public string A { get => GetProperty<string>(); set => SetProperty(value); }
public string B { get => GetProperty<string>(); set => SetProperty(value); }
public int? C { get => GetProperty<int?>(); set => SetProperty(value); }
public int? D { get => GetProperty<int?>(); set => SetProperty(value); }
public decimal E { get => GetProperty<decimal>(); set => SetProperty(value); }
public decimal F { get => GetProperty<decimal>(); set => SetProperty(value); }
}
public abstract class PropertyTracker
{
private Dictionary<string, object> _values = new Dictionary<string, object>();
protected void SetProperty<T>(T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
_values[propertyName] = value;
}
protected T GetProperty<T>([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
if (!_values.ContainsKey(propertyName))
return default;
return (T)_values[propertyName];
}
public bool IsSet(string propertyName)
{
return _values.ContainsKey(propertyName);
}
}
See we still have the concept of a backing store, it's just not a field anymore. The inspection code is a bit different too:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
Console.WriteLine(z.IsSet(nameof(UsefulClass.C)); // True
Console.WriteLine(z.IsSet(nameof(UsefulClass.D)); // False
There are all sorts of techniques you can use to scaffold this or similar code out across your classes, this is just an example implementation. You could even write a generic wrapper that uses reflection to do the same thing. In my solutions I tend to use T4 templates to generate what are effectively View Model classes. My main argument was that I could generate some verbose code and take a hit at compile-time instead of a performance hit at runtime with a reflection based implementation.
If your ViewModel classes inherit from your model class, then you can get close to an apparently automatic implementation that is more compatible with the rest of your runtime, but that would require your properties be declared as virtual to enable the inheriting class to override the implementation.
If you end up going down this route, consider adding value to your classes by implementing INotifyPropertyChanged, or perhaps IChangeTracking or IRevertibleChangeTracking while you're there.
UsefulClass z_objUsefulInstance = new UsefulClass() {
A = null
C = null,
E = 0
};
At this point, z_objUsefulInstance.A and C are null, E is 0,
B, D and F have not been initialized.
No that's not quite right.
From "14.11.4 Constructor Execution" in the C#7 language spec
Variable initializers are transformed into assignment statements, and these assignment statements are executed before the invocation of the base class instance constructor.
So before your instance constructor in the above example is started executing, the properties are assigned
A = default(string); // null
B = default(string); // null
C = default(int?); // null
D = default(int?); // null
E = default(decimal); // 0.0m
F = default(decimal); // 0.0m
(Not quite accurate, but close enough for this answer)
Then your instance constructor is run (in this example, the default provided by the compiler), then your property assignments are made
A = null,
C = null,
E = 0
. There's no difference between E = 0 and E = default(decimal), nor is there a difference between null and null (default(string)).
If you need to tell whether a property was set or not you will have to provide a backing field, or otherwise control access to the property.
If you want to read more about constructor details, a friendlier summary than the language spec can be found at https://jonskeet.uk/csharp/constructors.html .

Pass properties from array to a new class

i am working with a .net application where i have a web service that returns values in array form and now this array values i want to pass to a class and also as a reference to a private object. But since i am fresh new in programming i do not know how where an with what logic to start.
This is the private obj i created and i want to pass those references where CT is the array type and clsIn is the info that comes from another class but i have no idea how to pass neither of them.
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
return objFromCD;
}
And here is the new class i have created that where i want to pass all the values from clsIn and CT:
public class TotInfo
{
// Object properties
private string LAST_OFFER;
private string LAST_OFFER_DATE;
private string CLOSING_REASON;
private string _NO;
private string _STATUS;
#region "GET/SET Property"
public string NO
{
get { return _NO; }
set { _NO = value; }
}
public string LAST_OFFER
{
get { return _LAST_OFFER; }
set { _LAST_OFFER = value; }
}
public string LAST_OFFER_DATE
{
get { return _LAST_OFFER_DATE; }
set { _LAST_OFFER_DATE = value; }
}
public string CLOSING_REASON
{
get { return _CLOSING_REASON; }
set { _CLOSING_REASON = value; }
}
public string STATUS
{
get { return _STATUS; }
set { _STATUS = value; }
}
#endregion
#region "Costruttori"
public CardsTotInfo() { }
public CardsTotInfo(string No, string lastOffer, string lastOfferDate, string closingReason, string status)
{
this.NO = No;
this.LAST_OFFER = lastOffer.ToUpper();
this.LAST_OFFER_DATE = lastOfferDate.ToUpper();
this.CLOSING_REASON = closingReason.ToUpper();
this.STATUS = status.ToUpper();
}
}
I have passed, or better say i think i have passed in the correct way the values of clsIn but i do not know how to pass the properties of the array type CT[].
I really need help.
Thank you in advance.
If CT is an object array and the data you get from the web service always comes in the same order, for instance using an arbitrary example:
object[] CT = { 1, DateTime.Now, "foo", true }
If you know that each property data inside the array will always be at the same index (you will always have a int in index 0 representing an Id, and a DateTime on index 1 representing the last offer day and so on)
I would say you need to set each property "manually":
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
//get data from DB
//set the data from the array into the class properties
objFromCD.Id = (int)ct[0];
objFromCD.LastOfferDate = (DateTime)ct[1];
objFromCD.ClosingReason = (string)ct[2];
objFromCD.Available = (bool)ct[3];
return objFromCD;
}

Implementing an observable settings class

Using Rx, I have a settings panel which controls whether operations are enabled and at what rate they should run.
These are stored in a LibrarySettings class, when there is a change to the property via a front end slider/checkbox the observable property picks up on the change.
How should I write the LibrarySettings class such that the it does not set the setting.Value (whole LibrarySettings instance) to null.
IDisposable reader = setting.Value.Subscribe(options =>
{
OperationOneEnabled = options.OperationOneEnabled;
OperationTwoEnabled = options.OperationTwoEnabled;
OperationOneRate = options.OperationOneRate;
OperationTwoRate = options.OperationTwoRate;
});
IDisposable writer = this.WhenAnyPropertyChanged()
.Subscribe(vm =>
{
settings.Write(new LibrarySettings(OperationOneEnabled, OperationOneRate,
OperationTwoEnabled, OperationTwoRate));
});
OperationOneRateProperty = this.WhenValueChanged(vm => vm.ScheduleRate)
.DistinctUntilChanged()
.Select(value => $"{value} seconds")
.ForBinding();
_CleanUp = new CompositeDisposable(reader, writer, OperationOneRateProperty);
So within the LibrarySettings class I need to be able to create the property
public IObservable<LibrarySettings> Value
{
get { return _Value; }
set { _Value = value; }
}
So I try the following
Value = Observable.Create<LibrarySettings>(() =>
{
new LibrarySettings(false, OperationOneEnable,OperationOneRate,
OperationTwoEnabled, OperationTwoRate);
});
and get a
delegate func<IObserver<LibrarySettings>> does not take 0 arguments
Firstly, this is not valid code (wont compile)
Value = Observable.Create<LibrarySettings>(() =>
{
new LibrarySettings(false, OperationOneEnable,OperationOneRate,
OperationTwoEnabled, OperationTwoRate);
});
Observable.Create generally takes a Func<IObserver<T>, IDisposable> as a parameter, so should be corrected to be
Value = Observable.Create<LibrarySettings>(observer =>
{
observer.OnNext(new LibrarySettings(/*args*/));
//What to do here?
return Disposable.Empty; //Yuck.
});
Probably better and more simple is to just use Observable.Return, but then what is observable about this. Seems like it is using Rx just to satisfy a signature, because this isn't in the spirit of Rx.
Instead I imagine what you really want is a Settings property that pushes notifications when it changes. To this end I think there are two reasonable approaches
You have a readonly property of LibrarySettings where the the type LibrarySettings is mutable and observable.
You have a mutable and observable property of LibrarySettings, but the type LibrarySettings is immuatable.
i.e. either the readonly property
this.Setting.WhenAnyPropertyChanged()....
this.Setting.OperationOneRate = 25;
this.Setting.IsOperationOneEnabled= true;
where the type is mutable
public class LibrarySettings : INotifyPropertyChanged
{
public LibrarySettings()
{
IsOperationOneEnabled = false;;
OperationOneRate = 0;
IsOperationTwoEnabled = false;
OperationTwoRate = 0;
}
public bool IsOperationOneEnabled { get;set; }
public double OperationOneRate { get; set; }
public bool IsOperationTwoEnabled { get;set; }
public double OperationTwoRate { get; set;}
#region INPC Impl
#region
}
Or the immutable type, and you mutate the property (with a new instance each time). You would obviously want to create it with a default value.
this.WhenValueChanges(t=>t.Setting)....
this.Setting = new LibrarySettings(OperationOneEnable, OperationOneRate,
OperationTwoEnabled, OperationTwoRate);
And the type like...
public class LibrarySettings
{
public LibrarySettings(bool isOperationOneEnabled, double operationOneRate,
bool isOperationTwoEnabled, double operationTwoRate)
{
IsOperationOneEnabled = isOperationOneEnabled;
OperationOneRate = operationOneRate;
IsOperationTwoEnabled = isOperationTwoEnabled;
OperationTwoRate = operationTwoRate;
}
public bool IsOperationOneEnabled { get; }
public double OperationOneRate { get; }
public bool IsOperationTwoEnabled { get; }
public double OperationTwoRate { get;}
}
I just found the code that you linked to (you linked to the root of the repo not the actual classes in question)
* https://github.com/markiemarkus/Amadeus/blob/master/Amadeus/NovoApp/Models/LibrarySettings.cs
https://github.com/markiemarkus/Amadeus/blob/master/AmadeusNovoApp/Models/View/LibraryOptionsViewModel.cs
The main issue is these lines here
Value = Observable.Create<LibrarySettings>(observer =>
{
observer.OnNext(new LibrarySettings(false, OperationOneEnabled, OperationOneRate, OperationTwoEnabled, OperationTwoRate));
return Disposable.Empty;
});
}
public IObservable<LibrarySettings> Value
{
get { return _Value; }
set { _Value = value; }
}
public void Write(LibrarySettings item)
{
Value = Observable.Create<LibrarySettings>(observer =>
{
observer.OnNext(new LibrarySettings(false, OperationOneEnabled,
OperationOneRate, OperationTwoEnabled, OperationTwoRate));
return Disposable.Empty;
});
}
You create an observable sequence that has a single value (so isn't really observable). You then expose it via a property with a public setter (what does a settable IObservable property mean?!). And lastly you write over that instance in your write method, meaning anyone that has actually subscribed to the original value of the property is left holding a subscription to an orphaned Observable Sequence.
If you're just looking to get past the compile error, you would win with this:
Value = Observable.Return(new LibrarySettings(/*args*/));
or this:
Value = Observable.Create<LibrarySettings>(observer =>
{
observer.OnNext(new LibrarySettings(/*args*/));
return Disposable.Empty;
});
It sounds like you have a bigger design problem that you haven't laid out though.

How to UnitTest if all Properties are Set. There is no Duplication

Hello I'm trying to test a class that represents layout theme of GUI. It has color and size properties and a method that sets the default values.
public class LayoutTheme : ILayoutTheme
{
public LayoutTheme()
{
SetTheme();
}
public void SetTheme()
{
WorkspaceGap = 4;
SplitBarWidth = 4;
ApplicationBack = ColorTranslator.FromHtml("#EFEFF2");
SplitBarBack = ColorTranslator.FromHtml("#CCCEDB");
PanelBack = ColorTranslator.FromHtml("#FFFFFF ");
PanelFore = ColorTranslator.FromHtml("#1E1E1E ");
// ...
}
public int WorkspaceGap { get; set; }
public int SplitBarWidth{ get; set; }
public Color ApplicationBack { get; set; }
public Color SplitBarBack { get; set; }
public Color PanelBack { get; set; }
public Color PanelFore { get; set; }
// ...
}
I need to test:
1. If all of the properties are set by the SetTheme method.
2. If there is no duplication in setting a property.
For the first test, I first cycle through all properties and set an unusual value. After that I call SetTheme method and cycle again to check if all properties are changed.
[Test]
public void LayoutTheme_IfPropertiesSet()
{
var theme = new LayoutTheme();
Type typeTheme = theme.GetType();
PropertyInfo[] propInfoList = typeTheme.GetProperties();
int intValue = int.MinValue;
Color colorValue = Color.Pink;
// Set unusual value
foreach (PropertyInfo propInfo in propInfoList)
{
if (propInfo.PropertyType == typeof(int))
propInfo.SetValue(theme, intValue, null);
else if (propInfo.PropertyType == typeof(Color))
propInfo.SetValue(theme, colorValue, null);
else
Assert.Fail("Property '{0}' of type '{1}' is not tested!", propInfo.Name, propInfo.PropertyType);
}
theme.SetTheme();
// Check if value changed
foreach (PropertyInfo propInfo in propInfoList)
{
if (propInfo.PropertyType == typeof(int))
Assert.AreNotEqual(propInfo.GetValue(theme, null), intValue, string.Format("Property '{0}' is not set!", propInfo.Name));
else if (propInfo.PropertyType == typeof(Color))
Assert.AreNotEqual(propInfo.GetValue(theme, null), colorValue, string.Format("Property '{0}' is not set!", propInfo.Name));
}
}
Actually the test works well and I even found two missed settings, but I don't think it is written well.
Probably it can be don with Moq of the interface and check if all properties are set.
About the second test, don't have idea how to do it. Probably mocking and checking the number of calls can do it. Any help?
Thank you!
For testing if all properties are set to certain values, I would implement Equals() for this class and create a second object with known values and check for equality. This also comes in handy when testing for state changes etc.
I would certainly not test if a property gets set multiply times if there is no explicit reason to do it.

C# StackOverflowException

Problem: I am trying to update a List. If a certain item's ID already exists in the List, I want to add onto that item's quantity. If not, then I want to add another item to the list.
cart = (List<OrderItem>)Session["cart"];
for(int counter = cart.Count-1; counter >= 0; counter--)
{
if (cart[counter].productId == item.productId)
{
cart[counter].productQuantity += item.productQuantity;
}
else if (counter == 0)
{
cart.Add(item);
}
}
cart[counter] and item represent an instance(s) of a custom object of mine. Currently when I finally find a matching ID, everything APPEARS as though it should work, but I get a StackOverflowException thrown in my custom object class.
public int productQuantity
{
get
{
return _productQuantity;
}
set
{
productQuantity = value;
}
}
It gets thrown right at the open-bracket of the "set". Could somebody please tell me what the heck is wrong because I've been going at this for the past 2+ hours to no avail. Thank you in advance.
the problem is in your setter of the productQuantity
it should read:
set
{
_productQuantity= value;
}
edit (naming convention):
public class Vertex3d
{
//fields are all declared private, which is a good practice in general
private int _x;
//The properties are declared public, but could also be private, protected, or protected internal, as desired.
public int X
{
get { return _x; }
set { _x = value; }
}
}
Replace productQuantity = value; with _productQuantity = value; (you're recurring infinitely by calling the setter over and over)
Why not just use this instead?
public int productQuantity { get; set; }
But the flaw was in the _
public int productQuantity {
get {
return _productQuantity;
}
set {
_productQuantity = value;
}
}
cart = (List<OrderItem>)Session["cart"];
int index = cart.Find(OrderItem => OrderItem.productId == item.productId);
if(index == -1) {
cart.Add(item);
} else {
cart[index].productQuantity += item.productQuantity;
}
public int productQuantity
{
get
{
return _productQuantity;
}
set
{
_productQuantity = value; //this should be an assignment to a member variable.
}
}

Categories

Resources