Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Properties doesn't use nullable types in C#

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 36
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.

Reply With Quote
  #2  
Old 08-03-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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;
    }
}
Reply With Quote
  #3  
Old 08-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,388
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();
Reply With Quote
  #4  
Old 08-03-2010
Member
 
Join Date: May 2008
Posts: 2,380
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);
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 08-03-2010
Member
 
Join Date: Feb 2008
Posts: 1,844
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();
Reply With Quote
  #6  
Old 08-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,513
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"
Reply With Quote
  #7  
Old 01-09-2010
Member
 
Join Date: Sep 2010
Posts: 1
Re: Properties doesn't use nullable types in C#

For using System.Nullable<T> please refer
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:07 PM.