And / Or in C#

  • Thread starter Thread starter Daniel Rimmelzwaan
  • Start date Start date
D

Daniel Rimmelzwaan

Hello,

I have a conditional statement in VB that I want to translate in C#, but I
can't find the syntax.

VB:
If (MyFirstString = "" or MySecondString = "") Then
<insert your code here>
End if

C#
if (MyFirstString == "" ---- then what?) I can't get rid of those red
squiggly lines if I use "or"
{
<insert your code here>
}

the keyword "or" does not seem to exist in C#, or I am not typing it
correctly. Can anyone please show me how to do this?

Thanks,
Daniel
 
Daniel Rimmelzwaan said:
I have a conditional statement in VB that I want to translate in C#, but I
can't find the syntax.

VB:
If (MyFirstString = "" or MySecondString = "") Then
<insert your code here>
End if

C#
if (MyFirstString == "" ---- then what?) I can't get rid of those red
squiggly lines if I use "or"
{
<insert your code here>
}

the keyword "or" does not seem to exist in C#, or I am not typing it
correctly. Can anyone please show me how to do this?

Indeed, the keyword "or" doesn't exist. You want to use ||. I suggest
you read the section in MSDN about the C# operators - or read a
tutorial on the web.
 
if ((MyFirstString == "") || (MySecondString == ""))
{
// your code
}
else
{
// blabla
}


José
 
C#
if (MyFirstString.Equals("") || MySecondString.Equals(""))
{
<insert your code here>
}
 
Thanks Jon, and I have been looking for a tutorial to tell me this for
hours, but any that I found only had one expression in the if statement,
which is why I resorted to this newsgroup.

It doesn't really help when I type in something like 'vbCrLf' in the VS.NET
search pane to find out what this would be in C# (which I still don't know
by the way, so please tell me), and it comes back with 500 articles called
'ASP Technology and the XML DOM', or 'Create and Update a Scheduled Content
Download', both of which have nothing to do with my query, both articles
don't even have the literal string 'vbCrLf' in them.
 
Daniel Rimmelzwaan said:
Thanks Jon, and I have been looking for a tutorial to tell me this for
hours, but any that I found only had one expression in the if statement,
which is why I resorted to this newsgroup.

Funny, the first hit I get for "C# tutorial" on Google has a section on
operators :)
It doesn't really help when I type in something like 'vbCrLf' in the VS.NET
search pane to find out what this would be in C# (which I still don't know
by the way, so please tell me)

"\r\n" - or, to give the line break for the current system,
Environment.NewLine.
 
Jon Skeet said:
Funny, the first hit I get for "C# tutorial" on Google has a section on
operators :)

What can I say Jon, you're a better searcher than I am ;) the tutorials I
found did not have a list as extensive as that one. I will be diligently
studying it later, and who knows, one day I will answer one of your
questions :)
"\r\n" - or, to give the line break for the current system,
Environment.NewLine.

Thank you, just what I was looking for.
 
Back
Top