RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

x == (y || z)

Dipset

Wanderer
x == (y || z)

if (x == (y || z))

Is there a way to get something like that besides

if (x == y || x == z)
 

Phantom

Knight
Whats wrong with?

Code:
if (x == y || x == z)

What you want to do, and what you asked, does exactly the samething so I perhaps don't understand why the method that does work is not something you want to use.
 

wieganka

Sorceror
Phantom said:
Whats wrong with?

Code:
if (x == y || x == z)

What you want to do, and what you asked, does exactly the samething so I perhaps don't understand why the method that does work is not something you want to use.

I believe that this is a question of being more "efficient" (or lazy, whichever you prefer). The poster wanted to know if there was a shorter way of writing:

Code:
if (x == y || x == z)

Which, of course, in C#, there isn't.
 

Phantom

Knight
wieganka said:
I believe that this is a question of being more "efficient" (or lazy, whichever you prefer). The poster wanted to know if there was a shorter way of writing:

Code:
if (x == y || x == z)

Which, of course, in C#, there isn't.

I don't understand whats wrong with it....:confused:
 

Phantom

Knight
Dipset said:
if (x == y || x == z) looks fucking ugly and is a waste of text.

If you say so, but considering there isn't another way to do an OR statement, its not a waste of text.
 

yarex

Wanderer
(x == y || x == z) is wrong.

x == (y || z) .... when x ==0 and y ==1 and z==0
0 == (1||0) .... = (0==1) = 0. so it gives false

but when its (x == y || x == z)

(0 == 1 || 0==0) ....... (0 || 1) = 1

So these two are not equal in case if x=0 and y=1 and z=0
or case
x=0 and y=0 and x=1
(other cases are ok ;) )
 

Sep102

Page
yarex said:
(x == y || x == z) is wrong.

x == (y || z) .... when x ==0 and y ==1 and z==0
0 == (1||0) .... = (0==1) = 0. so it gives false

but when its (x == y || x == z)

(0 == 1 || 0==0) ....... (0 || 1) = 1

So these two are not equal in case if x=0 and y=1 and z=0
or case
x=0 and y=0 and x=1
(other cases are ok ;) )

Yes, but in his case, the semantics of
Code:
x == y || x == z
is what he was looking for, just in a shorter way of doing it, so the semantics that you pointed out, while valid in certain situations, was not what he was looking for.


Now, back to the topic, you can doing something like
Code:
x == (y || z)
But not normally and not all too pretty (nor without abusing the privilege of overloading operators). I felt like screwing with C#'s overloading of operators and wanted to see if you could abuse it in somewhat the same way as in C++, and I'm happy to say that you can, to a degree.

Code:
class TestClass
{
	public TestClass ( int i1, int i2 )
	{
		x = i1;
		y = i2;
	}
		
	public static bool operator == ( int i, TestClass tc )
	{
		return (i == tc.x || i == tc.y);
	}

	public static bool operator != ( int i, TestClass tc )
	{
		return !(i == tc);
	}

	int x
	        , y;
}

class TestClass2
{
	public TestClass2 ( int i )
	{
		x = i;
	}

	public static TestClass operator | ( TestClass2 tc1, TestClass2 tc2 )
	{
		return new TestClass( tc1.x, tc2.x );
	}
	
	public int x;
}
Using these two classes together, it's possible for me to write something of the form
Code:
int i = 20;
TestClass2 tc1 = new TestClass2(10)
        , tc2 = new TestClass2(20);

i == (tc1 | tc2);
and have it be semantically equivalent to
Code:
i == tc1.x || i == tc2.x
Please note that I do not advocate doing this, as it is indeed a bit of a misuse of the powerful and easily abusable operator overloading concept.

However, do note that I love that you can do this.
 

Phantom

Knight
yarex said:
(x == y || x == z) is wrong.

x == (y || z) .... when x ==0 and y ==1 and z==0
0 == (1||0) .... = (0==1) = 0. so it gives false

but when its (x == y || x == z)

(0 == 1 || 0==0) ....... (0 || 1) = 1

So these two are not equal in case if x=0 and y=1 and z=0
or case
x=0 and y=0 and x=1
(other cases are ok ;) )

Your example its actually wrong, because 0 == 0 is true, and thus the if statement is true.

What he wants is if x is equal to y or z, then the statement would be true, which is what has been posted.
 

Dipset

Wanderer
David said:
As opposed to what, this thread maybe?
Yes, I asked a simple question and everyone goes on and on about random shit.

This shouldn't have gone past daat99's reply.
 

Phantom

Knight
Dipset said:
Yes, I asked a simple question and everyone goes on and on about random shit.

This shouldn't have gone past daat99's reply.

I just wanted to know why he didn't want to use it.

Now that I know, I wish I didn't because its a lame reason :D
 

yarex

Wanderer
Phantom said:
Your example its actually wrong, because 0 == 0 is true, and thus the if statement is true.

What he wants is if x is equal to y or z, then the statement would be true, which is what has been posted.

If you mean this:
(0 == 1 || 0==0) ....... (0 || 1) = 1

then think again, becouse you are wrong.

0==1 gives 0
0==0 gives 1
therefore i've written that its equal to (0 || 1) = 1 = true

Result is the same as you say, but looks like you think something different.
 
Top