Results 1 to 7 of 7

Thread: Properties doesn't use nullable types in C#

  1. #1
    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.

  2. #2
    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;
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,394

    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();

  4. #4
    Join Date
    May 2008
    Posts
    2,393

    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);

  5. #5
    Join Date
    Feb 2008
    Posts
    1,856

    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. #6
    Join Date
    Jan 2008
    Posts
    1,521

    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"

  7. #7
    Join Date
    Sep 2010
    Posts
    1

    Re: Properties doesn't use nullable types in C#

    For using System.Nullable<T> please refer

Similar Threads

  1. Trinergy motherboad doesn't boot, doesn't display devices
    By TO-Phir in forum Motherboard Processor & RAM
    Replies: 5
    Last Post: 09-06-2011, 07:41 PM
  2. Replies: 1
    Last Post: 16-12-2009, 12:11 PM
  3. Explanation for Nullable Types in detail - C#
    By Ivann in forum Software Development
    Replies: 3
    Last Post: 10-11-2009, 08:51 PM
  4. Different types Types CCFL's
    By Zipp in forum Overclocking & Computer Modification
    Replies: 3
    Last Post: 29-10-2009, 09:11 AM
  5. system properties doesn't show hardware configuration! HELP!
    By This is so annoying..... in forum Vista Help
    Replies: 3
    Last Post: 10-10-2008, 12:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •