HighTechTalks DotNet Forums  

Moving to C#

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Moving to C# in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Dave
 
Posts: n/a

Default Moving to C# - 12-12-2005 , 08:08 AM






I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'



--
Thanks in advance,
Dave

Reply With Quote
  #2  
Old   
Vadym Stetsyak
 
Posts: n/a

Default Re: Moving to C# - 12-12-2005 , 08:42 AM






in C# if you type & it is bit AND operator, it returs int, that is the
reason why the compiler generated error.

In order to get boolean AND comparison you have to write &&

if(checkLogin && 1)
{ }

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Dave" <Dave (AT) discussions (DOT) microsoft.com> wrote

Quote:
I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number
exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'



--
Thanks in advance,
Dave



Reply With Quote
  #3  
Old   
Sundar.k
 
Posts: n/a

Default RE: Moving to C# - 12-12-2005 , 12:20 PM



use && instead of &

"Dave" wrote:

Quote:
I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'



--
Thanks in advance,
Dave

Reply With Quote
  #4  
Old   
ondrejbohaciak
 
Posts: n/a

Default RE: Moving to C# - 01-05-2006 , 08:42 PM



logical expression must be composed of boolean operands. int is not boolean.
you'll have to write it like this

int a;
(checkLogin && (a == 1))
{
...
}

by asking if a's value is some value you have made boolean expression out of
int . numerical value doesn't evaluate to true/false (c# being strongly
typed, unlike c++). also && is logical operator and & bitwise

"Dave" wrote:

Quote:
I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'



--
Thanks in advance,
Dave

Reply With Quote
  #5  
Old   
SloPoke
 
Posts: n/a

Default Re: Moving to C# - 01-11-2006 , 06:33 PM



Unless I'm mistaken, '&' is also a logical boolean operator if both
operands are boolean.

The correct answer to the problem, since he is using a bitmask to
check/set multiple options, is:

if ((checkLogin & 1) != 0)
lbError.Text = "Login already exists";

if (((checkLogin & 2) != 0) & (txtEmpNum.Text.Length > 0))
lbError.Text = "Employee Number already exists";

Could also use:

if (((checkLogin & 2) != 0) && (txtEmpNum.Text.Length > 0))
lbError.Text = "Employee Number already exists";

for the second statement. Since && is a conditional logical boolean
operator, C# will not evaluate the second test if the first is false
since the overall boolean value can not be true if the first is false.

"ondrejbohaciak" <ondrejbohaciak (AT) discussions (DOT) microsoft.com> wrote:

Quote:
logical expression must be composed of boolean operands. int is not boolean.
you'll have to write it like this

int a;
(checkLogin && (a == 1))
{
...
}

by asking if a's value is some value you have made boolean expression out of
int . numerical value doesn't evaluate to true/false (c# being strongly
typed, unlike c++). also && is logical operator and & bitwise

"Dave" wrote:

I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'



--
Thanks in advance,
Dave

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.