Objectmix
Tags Register Mark Forums Read

Moving to C# : DOTNET

This is a discussion on Moving to C# within the DOTNET forums in Framework and Interface Programming category; 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 ...


Object Mix > Framework and Interface Programming > DOTNET > Moving to C#

DOTNET Discussion forums related to Microsoft Dot net technologies, CSharp and other related items

Reply

 

LinkBack Thread Tools
  #1  
Old 12-12-2005, 08:08 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Moving to C#

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
  #2  
Old 12-12-2005, 08:42 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Moving to C#

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@discussions.microsoft.com> wrote in message
news:7E91BEC8-DA13-4472-912A-9792B5275189@microsoft.com...
>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



  #3  
Old 12-12-2005, 12:20 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default RE: Moving to C#

use && instead of &

"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

  #4  
Old 01-05-2006, 08:42 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default RE: Moving to C#

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

  #5  
Old 01-11-2006, 06:33 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Moving to C#

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@discussions.microsoft.com> wrote:

>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

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Moving from FTP 6 to FTP 7 usenet Inetserver 7 11-28-2007 05:23 PM
Moving to vb.net from vba usenet DOTNET 4 10-11-2007 02:26 AM
moving companies moving loans moving budget usenet Microsoft Money 0 07-16-2007 08:28 PM
Re: Moving from 6.5 to Pro usenet Adobe Premiere 0 05-31-2005 08:36 AM
Re: Moving from 6.5 to Pro usenet Adobe Premiere 0 05-30-2005 07:35 PM


All times are GMT -5. The time now is 04:01 PM.

Managed by Infnx Pvt Ltd.