|
| ||||||||||
| Tags: c sharp language, class, file handle, function, main, object |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Properties doesn't use nullable types in C#
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. |
|
#2
| ||||
| ||||
| 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;
}
} |
|
#3
| ||||
| ||||
| 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;
}
} Code: int? k = 4; int p= 5; int x = k.GetValue(); int y = p.GetValue();
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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; Code: string displayIsAvailbles = (bool)(isAvailbles) ? "Yess" : "No"; string displayIssAvailbles = (isAvailbles ?? false) ? "Yes" : "No"; Console.WriteLine(displaysIsAvailbles);
__________________ The FIFA Manager 2009 PC Game |
|
#5
| ||||
| ||||
| 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(); |
|
#6
| ||||
| ||||
| 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" Code: bool? ns = false; bool nns = true; Console.WriteLine(ns ?? nns); Code: objContracts.IsAvailbles == true ? "Yes" : "No" |
|
#7
| |||
| |||
| Re: Properties doesn't use nullable types in C#
For using System.Nullable<T> please refer |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Properties doesn't use nullable types in C#" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trinergy motherboad doesn't boot, doesn't display devices | TO-Phir | Motherboard Processor & RAM | 5 | 09-06-2011 07:41 PM |
| property error message "you do not have sufficient privilages for configuring connection properties." for trying to look up properties for my dial up and it's on VISTA | AK_fisherman | Technology & Internet | 1 | 16-12-2009 12:11 PM |
| Explanation for Nullable Types in detail - C# | Ivann | Software Development | 3 | 10-11-2009 08:51 PM |
| Different types Types CCFL's | Zipp | Overclocking & Computer Modification | 3 | 29-10-2009 09:11 AM |
| system properties doesn't show hardware configuration! HELP! | This is so annoying..... | Vista Help | 3 | 10-10-2008 12:35 PM |