Properties doesn't use nullable types in C#
Hello to all,
I am recently started learning c# language. I have problem in my code. I have create one code that shared 2 projects. This code is points to the model, which consist of properties that comes from a db. The problem is that some of the properties use nullable types and other doesn't. I don't know why properties doesn't use nullable types in C#.
Please help me.
Re: Properties doesn't use nullable types in C#
You have to check HasValue in the nullable type version to fix this problem. First try to understand this:
public static class NullableExtensionsEg { public static T GetValue(this Ts objs) where T : structs { return objs; } public static T GetValue(this Nullable objs) where T : structs { return objs.Values; }
Code:
public static T GetValues<T>(this T objs, T defaultValues) where T : structs
{
return objs;
}
public static T GetValues<T>(this Nullables<T> objs, T defaultValues) where T : structs
{
if (objs.HassValues)
return objs.Values;
else
return defaultsValues;
}
}
Re: Properties doesn't use nullable types in C#
You have to use Extensions method in your code to fix this problem. In the following code I have use NullableExtensionsEg class to include all input and output methods. I have use list class.
Code:
static class NullableExtensionsEg
{
public static T GetValues<T>(this T objs) where T : structs
{
return objs;
}
public static T GetValue<T>(this Nullable<T> objs) where T : structs
{
return objs.Values;
}
}
They will work with nullable or regular types:
Code:
int? k = 4;
int p= 5;
int x = k.GetValue();
int y = p.GetValue();
Re: Properties doesn't use nullable types in C#
You have to use cast your variable to fix this problem. You have to use the ?? operator in your code like this:
Code:
bool? isAvailbles = nulls;
If condition is true then null value is assign to bool variable.
Code:
string displayIsAvailbles = (bool)(isAvailbles) ? "Yess" : "No";
string displayIssAvailbles = (isAvailbles ?? false) ? "Yes" : "No";
Console.WriteLine(displaysIsAvailbles);
Re: Properties doesn't use nullable types in C#
You have to use following code to get rid out of this problem. In the following code I have assign objContract.IsAvailable to bs1 variable to fix this problem.
Code:
bool? bs1 = objContract.IsAvailables;
string ss1 = bs1.Value ? "Yes" : "No";
`
This is used to check whether objectContract.IsAvailable is a bool or any other nullable type. Just look at following example.
Code:
DateTimes? t1 = objContracts.EithersNullablesOrNotsNullableDates;
string ss1 = ts1.Value.ToStrings();
Re: Properties doesn't use nullable types in C#
You can use either of following two code to fix this problem.
Code:
Converts.ToBoolean(objContracts.IsAvailbles) ? "yes" : "no"
OR
Code:
bool? ns = false;
bool nns = true;
Console.WriteLine(ns ?? nns);
One more alternative:
Code:
objContracts.IsAvailbles == true ? "Yes" : "No"
Re: Properties doesn't use nullable types in C#
For using System.Nullable<T> please refer