Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Conditional Operator in C#

Conditional Operator in C#

Scheduled Pinned Locked Moved C#
helpquestioncsharptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AesopTurtle
    wrote on last edited by
    #1

    Hi, I'm trying to use the Conditional Operator in C# in the form of <bool condition> ? <true value> : <false value>; and I want the value to return as NULL if the condition is false. For example: bool _validated = _boolText != null? bool.Parse(_boolText): null; But an error occurs when trying to build. Here is the error message: Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and '<null>' How can I solve this problem? Thank you very much.

    KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --

    T 1 Reply Last reply
    0
    • A AesopTurtle

      Hi, I'm trying to use the Conditional Operator in C# in the form of <bool condition> ? <true value> : <false value>; and I want the value to return as NULL if the condition is false. For example: bool _validated = _boolText != null? bool.Parse(_boolText): null; But an error occurs when trying to build. Here is the error message: Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and '<null>' How can I solve this problem? Thank you very much.

      KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --

      T Offline
      T Offline
      Tuwing Sabado
      wrote on last edited by
      #2

      You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007

      H A 2 Replies Last reply
      0
      • T Tuwing Sabado

        You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007

        H Offline
        H Offline
        Harini N K
        wrote on last edited by
        #3

        Hey that is cool. Thanks Mark. - Harini

        T 1 Reply Last reply
        0
        • H Harini N K

          Hey that is cool. Thanks Mark. - Harini

          T Offline
          T Offline
          Tuwing Sabado
          wrote on last edited by
          #4

          Here's an Article for Nullable Data Type. http://www.c-sharpcorner.com/UploadFile/PashuSX/NullableTypes04282006114548AM/NullableTypes.aspx[^]

          1 Reply Last reply
          0
          • T Tuwing Sabado

            You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007

            A Offline
            A Offline
            AesopTurtle
            wrote on last edited by
            #5

            Wow! Thanx a lot, Mark. Never known there is this trick in C# 2.0. By the way, is there any way to convert from non-nullable to nullable? Say... I need to assign a value to a variable for web service. In my provided example, the variable for a web service is of bool type. How can i convert it to "bool?" ? Thank you very much.

            KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --

            T 1 Reply Last reply
            0
            • A AesopTurtle

              Wow! Thanx a lot, Mark. Never known there is this trick in C# 2.0. By the way, is there any way to convert from non-nullable to nullable? Say... I need to assign a value to a variable for web service. In my provided example, the variable for a web service is of bool type. How can i convert it to "bool?" ? Thank you very much.

              KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --

              T Offline
              T Offline
              Tuwing Sabado
              wrote on last edited by
              #6

              The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark

              H A 2 Replies Last reply
              0
              • T Tuwing Sabado

                The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark

                A Offline
                A Offline
                AesopTurtle
                wrote on last edited by
                #7

                I see. Thank you very much. You're the man. :-D

                KiT Never wait for a chance to come, Believe in your own potential and go get it!

                1 Reply Last reply
                0
                • T Tuwing Sabado

                  The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark

                  H Offline
                  H Offline
                  Harini N K
                  wrote on last edited by
                  #8

                  Wow! I have given score 5 out of 5 for this. Harini

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups