|
Let me break your statement into three parts:
bool a = (string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR"))
&& (string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"));
bool b = (string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))
&& (string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR"));
bool c = string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper());
Combined it would look like this:
if (a || b && c) {
MessageBox.Show("ERROR");
}
What the compiler makes out of this statement is this:
if (a || (b && c)) {
MessageBox.Show("ERROR");
}
Thats because the logical AND has a higher priority than the logical OR. Its like this simple numeric example:
3 + 2 * 4 = 3 + (2 * 4)
In conclusion the statement will be true if a is true no matter what the other subparts are.
|
|
|
|
|
Thank you for your answer,
but I think I did:
if ( ( a || b) && c ) {
MessagBox.Show("ERROR")
}
Please look at the (many) brackets - therefor I placed the brackets around the or-expression to force the compiler to check the or-term fist and this result with the &&-expression !
And that's what's going wrong
Frank
|
|
|
|
|
Yes, you thought that you did, but you failed.
Instead of ((a || b) && c) you created ((a) || (b) && c).
string a = "MR";<br />
string b = "MRS";<br />
string c = "BAKER";<br />
string d = "Meyers"; <br />
<br />
a = a.Trim().ToUpper();<br />
b = b.Trim().ToUpper();<br />
c = c.Trim().ToUpper();<br />
d = d.Trim().ToUpper();<br />
<br />
if (((a == "MR" && b == "MRS") || (a == "MRS" && b == "MR")) && c == d) {<br />
MessageBox.Show("ERROR");<br />
}
---
b { font-weight: normal; }
|
|
|
|
|
no you did not. It will result in
if
(
(1) || ( 1 ) && (0)
)
{
MessageBox.Show("ERROR");
}
Must be this:
if
(
(((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))) ||
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR")))) &&
(string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper()))
)
{
MessageBox.Show("ERROR");
}
will result in
if
(
((1) || ( 1 )) && (0)
)
{
MessageBox.Show("ERROR");
} and will not display message box.
But still, it's a mess.
David
|
|
|
|
|
May I suggest you little simplification?
a.TrimEnd().TrimStart().ToString().ToUpper() -> a.Trim().ToUpper()
David
|
|
|
|
|
Thank's but that doesn't help in my problem !
regards
Frank
|
|
|
|
|
fracalifa wrote:
that doesn't help in my problem !
Pardon, but it does. Low readability (daily-wtf calibre!) of your code is what caused problem here.
ok, it is prevence but not solution, that's why I say it is OT (off topic).
David
|
|
|
|
|
|
Hi all,
coming from "C" I'm a little bit confused on the result of the following boolean term.
I think this can never become TRUE but it does - why ?
if( ((("A"=="A") && ("B"=="B")) || (("A"=="B") && ("B"=="A"))) && (true==false) )
{
MessageBox.Show("Error"); // this code is not reachable ?????
}
A and B is really a comparison of strings
reduced to my understanding is that the results shoud be
if( (((TRUE)&&(TRUE)) ||((FALSE && FALSE))) && (FALSE) ) // should ever be FALSE
........\---------/........\----------/.........|
........... TRUE......||.......FALSE.......&& FALSE
.............\------------------/...............|
.....................TRUE..................&& FALSE = FALSE //!!
the second problem is that the code in the brackets is not reachable ????
the count of backets is correct !
that's leave my understanding of C#
where is my error in thinking ?
Thanks for your help
Frank
|
|
|
|
|
In c# the == operator has been redefined for strings, so when you compare two strings written like "abc" the compiler is able to compare them. Using variables, you have to use the string.Equals(...) or the string.CompareTo(...) methods to have reliable results. In VB.NET you can use the = operator also for string variables.
|
|
|
|
|
Have you actually tried this code? The fact that the compiler tells you thats not reachable definitely means that it will never get executed. Ive tried the code and the messagebox is not showing.
|
|
|
|
|
That's one of the two problems,
I reduced two brackets - that it runs
but I am trying to implement the first suggestion and than .... ?
|
|
|
|
|
The first term (("A"=="A") && ("B"=="B")) is true, thus the whole OR-expression || will become true.
Because the first term is always true, there is no need for the compiler to evaluate the second term.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
... yes but ..
I put the OR-expression into brackets followed by an AND-expression so the compiler has to check for
( 1 || 0 ) && ( 0 ) so there is a need to do this - because the last term is not true !
regards
|
|
|
|
|
Oh sorry, I misread the brackets.
Well, because the last term is always false, the compiler detects that the whole clause never can become true. The or-clause becomes always true, bug then the and-clause will become false anyway.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi Greeeg,
please look to following code I have implemented:
Please try this code:
string a = "MR";
string b = "MRS";
string c = "BAKER";
string d = "Meyers";
if
(
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS"))) ||
((string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS")) &&
(string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR"))) &&
(string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(),
d.TrimEnd().TrimStart().ToString().ToUpper()))
)
{
MessageBox.Show("ERROR");
}
The result of the first 4 lines of the if-expression becomes TRUE -- o.k.
but the comparison of c.Trim and d.Trim is FALSE so the whole expression (due to the &&) has to become false but the code is running into the MessageBox bracket
why
thank you for your help
Frank
|
|
|
|
|
this is strange indeed, looks like the && operator is evaluated BEFORE the || operator - I thought they are handled equally.
If you want the || clause to be evaluated before, better put brackets around the first two expressions.
This is a test I made:
string a = "MR";
string b = "MRS";
string c = "BAKER";
string d = "Meyers";
bool b1,b2,b3,b4,b5;
b1 = string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MR");
b2 = string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MRS");
b3 = string.Equals(a.TrimEnd().TrimStart().ToString().ToUpper(),"MRS");
b4 = string.Equals(b.TrimEnd().TrimStart().ToString().ToUpper(),"MR");
b5 = string.Equals(c.TrimEnd().TrimStart().ToString().ToUpper(), d.TrimEnd().TrimStart().ToString().ToUpper());
bool b6, b7;
b6 = (((b1 && b2) || (b3 && b4)) && b5);
b7 = ((b1 && b2) || ((b3 && b4) && b5));
if(b6)
{
MessageBox.Show("ERROR");
}
regards
/edit:
This supports my theory:
Operator precedence and associativity
modified 12-Sep-18 21:01pm.
|
|
|
|
|
They have different precendence.
A lot of things have in C#...
The PROgrammer Niklas Ulvinge aka IDK
|
|
|
|
|
Hi,
I need to find the relation between the windows user(username with which he logged ) ,his session id and the process that were started by him.All these informations are available in the task manager but how do i get them through the code.
Does c# directly have any procedures to find this or if there are any other API's.Please let me know.
Thanks
|
|
|
|
|
System.Environment.UserName returns the Windows username of who started the thread that is calling that static property.
For the rest try to see MSDN for System.Diagnostics.Process and its members.
You can also access system infos using System.Diagnostics.PerformanceCounter that allows you to read information about cpu, ram, hdd usage, running processes, threads, modules and so on... That is a little complicated to use...
|
|
|
|
|
can ne1 plz tell me hw m goin to do this..
i hav a function in which i execute the datareader
ie--dataReader = SqlHelper.ExecuteReader(SQLConn,CommandType.StoredProcedure,"USP_GET_DATA" );
i declare getobj as array of objects..
after it gets all the data from the database in getobj ,
i want to fill the datagrid with the data...m not using dataset at all...m getting the values of each row in getobj
in the form_load
private void frmIATA_CODES_Load(object sender, System.EventArgs e)
{
NFDataObjects.IATACodeRequest [] getobj;
getobj=getIATA();
//here i want to write code to fill the datagrid with getobj which is array of objects
dataGrid1.//how to populate the datagrid???
}
ne help is appreciated....
|
|
|
|
|
Hi there friends,
I am working on a program that its language is not english(so mine!).
every time user wants to change the language he must press ALT+SHIFT
and this is annoying.How can I change language programatically?
Thank You!
|
|
|
|
|
Question: I would like to know if is there a method to send a mouse click to an application, more precisely to the apllication that stays behind my window.
Essentially I need to build a click-through window.
thanks to all.
|
|
|
|
|
Hello
I made a small software in C#. I want to add help feature in it. I dont know how to to do it Can any body give me an idea or help me in this regrad.
Regards
Asmyan
|
|
|
|
|
Install the "HTML Help Workshop" utility which comes with the Visual Studio. See the detailed help in this utility for creating HTML help (.chm file) for your program. Basic steps are as under:
1. Create a new Help project using the File-New command.
2. Add HTML files for the topics to be included.
3. Add / create Index and Contents file which will be used for the Contents / Index tabs in your help system.
4. Add graphics files etc. inside your html topic files.
5. Change properties of the help project file as per requirements.
6. Compile the help project to give you the .chm file.
Details on these individual topics can be seen in the HTML Help Workshop utility's help file which gives step-by-step details.
|
|
|
|