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 ?:

Conditional ?:

Scheduled Pinned Locked Moved C#
comtoolsquestion
13 Posts 7 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.
  • J Offline
    J Offline
    Jammer 0
    wrote on last edited by
    #1

    Hi, Is there any way to get the following to work?

    bool s = _myProperty == true ? Method1() : Method2();

    Or is this just silly talk? :)

    Jammer My Blog | Article(s)

    I S A L 4 Replies Last reply
    0
    • J Jammer 0

      Hi, Is there any way to get the following to work?

      bool s = _myProperty == true ? Method1() : Method2();

      Or is this just silly talk? :)

      Jammer My Blog | Article(s)

      I Offline
      I Offline
      Ian McCaul
      wrote on last edited by
      #2

      Yes so long as the return types of Method1/2 are bool also.

      M 1 Reply Last reply
      0
      • J Jammer 0

        Hi, Is there any way to get the following to work?

        bool s = _myProperty == true ? Method1() : Method2();

        Or is this just silly talk? :)

        Jammer My Blog | Article(s)

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Jammer wrote:

        bool s = _myProperty == true ? Method1() : Method2();

        Why doesn't it work? It should, as long as Method1() and Method2() return bool, or return types convertible to each other and to bool.

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        V J 2 Replies Last reply
        0
        • I Ian McCaul

          Yes so long as the return types of Method1/2 are bool also.

          M Offline
          M Offline
          Mel Feik
          wrote on last edited by
          #4

          I believe they (method1/2) would also have to be static (?)

          --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

          I 1 Reply Last reply
          0
          • M Mel Feik

            I believe they (method1/2) would also have to be static (?)

            --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

            I Offline
            I Offline
            Ian McCaul
            wrote on last edited by
            #5

            Only unless the method that is using this code is also static... otherwise they dont need to be. From the looks he is testing an internal variable so chances are it wont be static. Edit: But it could be static even if it wasnt static... could go either way.

            1 Reply Last reply
            0
            • J Jammer 0

              Hi, Is there any way to get the following to work?

              bool s = _myProperty == true ? Method1() : Method2();

              Or is this just silly talk? :)

              Jammer My Blog | Article(s)

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              Jammer wrote:

              Is there any way to get the following to work? bool s = _myProperty == true ? Method1() : Method2();

              It should work, if both Method1 and Method2 are static and retrun a Bool value.

              cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

              S 1 Reply Last reply
              0
              • A Abhijit Jana

                Jammer wrote:

                Is there any way to get the following to work? bool s = _myProperty == true ? Method1() : Method2();

                It should work, if both Method1 and Method2 are static and retrun a Bool value.

                cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                I don't get it, why should the methods be static?

                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                I M 2 Replies Last reply
                0
                • J Jammer 0

                  Hi, Is there any way to get the following to work?

                  bool s = _myProperty == true ? Method1() : Method2();

                  Or is this just silly talk? :)

                  Jammer My Blog | Article(s)

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  yes

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    I don't get it, why should the methods be static?

                    Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                    I Offline
                    I Offline
                    Ian McCaul
                    wrote on last edited by
                    #9

                    They dont need to be as I said earlier... i.e.

                    class Conditional
                    {
                    private bool _myProperty;

                    public bool MyProperty
                    {
                    get { return _myProperty; }
                    set { _myProperty = value; }
                    }

                    public bool DoSomething()
                    {
                    bool s = _myProperty == true ? Method1() : Method2();
                    return s;
                    }

                    public bool Method1()
                    {
                    return true;
                    }

                    public bool Method2()
                    {
                    return false;
                    }
                    }

                    1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      I don't get it, why should the methods be static?

                      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                      M Offline
                      M Offline
                      Mel Feik
                      wrote on last edited by
                      #10

                      sorry on that - I had another issue on my mind when I read the question and posted that. I just thought of how I would stick that in a console app - where method1 & 2 were helpers to Main (ie. methods in the Program class) - in which case static would be required. But on considering.. should have just posted my own question quietly.

                      --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

                      1 Reply Last reply
                      0
                      • S S Senthil Kumar

                        Jammer wrote:

                        bool s = _myProperty == true ? Method1() : Method2();

                        Why doesn't it work? It should, as long as Method1() and Method2() return bool, or return types convertible to each other and to bool.

                        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                        V Offline
                        V Offline
                        Vikram A Punathambekar
                        wrote on last edited by
                        #11

                        S. Senthil Kumar wrote:

                        or return types convertible to each other and to bool

                        Good lord, I still remember that post on your blog. I asked a few people, and nobody knew it! I wonder why they made that restriction. :~

                        Cheers, Vikram.

                        Current activities: Films: Sense and Sensibility TV series: Friends, season 2 Books: Longitude, by Dava Sobel.


                        Carpe Diem.

                        1 Reply Last reply
                        0
                        • S S Senthil Kumar

                          Jammer wrote:

                          bool s = _myProperty == true ? Method1() : Method2();

                          Why doesn't it work? It should, as long as Method1() and Method2() return bool, or return types convertible to each other and to bool.

                          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                          J Offline
                          J Offline
                          Jammer 0
                          wrote on last edited by
                          #12

                          Hi, I see ... the problem was the methods are void return types. Can this be used with void? Cheers,

                          Jammer My Blog | Article(s)

                          S 1 Reply Last reply
                          0
                          • J Jammer 0

                            Hi, I see ... the problem was the methods are void return types. Can this be used with void? Cheers,

                            Jammer My Blog | Article(s)

                            S Offline
                            S Offline
                            S Senthil Kumar
                            wrote on last edited by
                            #13

                            No, the expression containing the ternary operator must be of some type. If both the methods take no parameters, this will work

                            Action result = p ? new Action(M1) : new Action(M2);
                            result();

                            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                            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