Conditional ?:
-
Yes so long as the return types of Method1/2 are bool also.
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 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
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.
-
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)
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
-
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
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
-
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)
yes
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
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
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;
}
} -
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
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
-
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
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.
-
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
-
Hi, I see ... the problem was the methods are void return types. Can this be used with void? Cheers,
Jammer My Blog | Article(s)
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