Convert C# bitwise operators to VB - How?

  • Thread starter Thread starter James Radke
  • Start date Start date
J

James Radke

Hello,

I found some code that I could use in my application on the web, but it is
written in C#, and I would like to convert it to VB. And I am having
problems with one section. Is there anyone that could tell me how to
convert these two things?

1)

[Flags, Serializable]
public enum CardType
{
MasterCard = 0x0001,
VISA = 0x0002,
Amex = 0x0004,
DinersClub = 0x0008,
enRoute = 0x0010,
Discover = 0x0020,
JCB = 0x0040,
Unknown = 0x0080,
All = CardType.Amex | CardType.DinersClub | CardType.Discover |
CardType.Discover |
CardType.enRoute | CardType.JCB | CardType.MasterCard | CardType.VISA
}


I understand how to do the ENUM, but I don't understand what the values are?
They seem like some kind of bit settings and I don't know how to do that.

2.)

if (_cardTypes & CardType.Amex)!=0)

where _cardTypes I believe contains some bitwise combination of the
CardTypes from above.....

Thanks for any help!

Jim
 
Hi James,

1:

<Flags(), Serializable()> _
Public Enum CardType
MasterCard = &H1
VISA = &H2
Amex = &H4
DinersClub = &H8
enRoute = &H10
Discover = &H20
JCB = &H40
Unknown = &H80
All = MasterCard Or VISA Or Amex Or DinersClub Or enRoute Or Discover Or
JCB
End Enum

2:

If (_cardTypes And CardType.Amex) = CardType.Amex Then
' Amex is present
End If

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


: Hello,
:
: I found some code that I could use in my application on the web, but it is
: written in C#, and I would like to convert it to VB. And I am having
: problems with one section. Is there anyone that could tell me how to
: convert these two things?
:
: 1)
:
: [Flags, Serializable]
: public enum CardType
: {
: MasterCard = 0x0001,
: VISA = 0x0002,
: Amex = 0x0004,
: DinersClub = 0x0008,
: enRoute = 0x0010,
: Discover = 0x0020,
: JCB = 0x0040,
: Unknown = 0x0080,
: All = CardType.Amex | CardType.DinersClub | CardType.Discover |
: CardType.Discover |
: CardType.enRoute | CardType.JCB | CardType.MasterCard | CardType.VISA
: }
:
:
: I understand how to do the ENUM, but I don't understand what the values
are?
: They seem like some kind of bit settings and I don't know how to do that.
:
: 2.)
:
: if (_cardTypes & CardType.Amex)!=0)
:
: where _cardTypes I believe contains some bitwise combination of the
: CardTypes from above.....
:
: Thanks for any help!
:
: Jim
:
:
 
Hello,

James Radke said:
1)

[Flags, Serializable]
public enum CardType
{
MasterCard = 0x0001,
VISA = 0x0002,
Amex = 0x0004,
DinersClub = 0x0008,
enRoute = 0x0010,
Discover = 0x0020,
JCB = 0x0040,
Unknown = 0x0080,
All = CardType.Amex | CardType.DinersClub | CardType.Discover |
CardType.Discover |
CardType.enRoute | CardType.JCB | CardType.MasterCard | CardType.VISA
}

I understand how to do the ENUM, but I don't understand
what the values are?

They are in hexadezimal format. Use '&H1' for '0x0001', '&H2' for '0x0002'
and so on.
2.)

if (_cardTypes & CardType.Amex)!=0)

where _cardTypes I believe contains some bitwise combination
of the CardTypes from above.....

Use 'If (m_CardTypes And CardType.Amex) <> 0 Then' instead.
 
Tom,

Hmmm... So the All = in #1 below would use the 'Or' versus the 'Xor'? Does
a standard Or make ALL a unique ENUM? Or is that doing something slightly
different in VB, than in the C#?

I will try it! Thanks for the assist.......

Jim
 
Hi James,

VB's AND is a bitwise And and OR is a bitwise Or. They are the same as
their C# counterparts.

If you come across && in some C# code, it is the same as VB's AndThen.
Similarly C#'s || is the same as VB's OrElse.

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
Hello,

Fergus Cooney said:
If you come across && in some C# code, it is the same as
VB's AndThen.

'AndThen'? Typo or am I missing something?

;-)
 
Herfried,
Yes I too find these operators not nice Herfried,
In Dutch there is a proverbial: That is grist for his mill.
:-))
Cor
 
Hello,

Cor said:
Yes I too find these operators not nice Herfried,
In Dutch there is a proverbial: That is grist for his mill.

'AndAlso' and 'OrElse' -- IMO very nice. I would have liked 'And' and 'Or'
as logical operators and 'BitAnd' and 'BitOr' as bitwise operators, but they
changed that in the Beta.
 
Hi Herfried,
Of course, first of all, which VB programmer uses those really often.

We always agree that the natural language prefers, I seldom say "en ook" do
you say often "und auch" ?

That renaming bitwise operators had been a good sollution. And fits to the
theorie of natural language. Bitwise is not a part of the natural language
and for that you can create a new word.

Cor
 
Hello,

Cor said:
Of course, first of all, which VB programmer uses those really
often.

The bitwise operators or the logical operators?
We always agree that the natural language prefers, I
seldom say "en ook" do you say often "und auch" ?

In German, "und" has the meaning of "und auch".
That renaming bitwise operators had been a good sollution.
And fits to the theorie of natural language. Bitwise is not
a part of the natural language and for that you can create
a new word.

That's exactly what I think.
 
Everyone who replied,

Thanks for the assistance! After a little trial and error, everything works
great now!

Jim


Peter Huang said:
Hi James,
Hmmm... So the All = in #1 below would use the 'Or' versus the 'Xor'? Yes.
Does a standard Or make ALL a unique ENUM? Yes.
The value of ALL will be &H7F in #1
Or is that doing something slightly
different in VB, than in the C#?
I think they are same in the bitwise operation.

You may check the link belwo for more information.

Used to perform a logical disjunction on two Boolean expressions, or
bitwise disjunction on two numeric values..
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/
vaopror.asp

Binary | operators are predefined for the integral types and bool. For
integral types, | computes the bitwise OR of its operands. For bool
operands, | computes the logical OR of its operands; that is, the result is
false if and only if both its operands are false.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vclrfBarOperator.asp

Dis this answer the questions?

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
Reply-To: "James Radke" <[email protected]>
From: "James Radke" <[email protected]>
References: <[email protected]>
Subject: Re: Convert C# bitwise operators to VB - How?
Date: Thu, 18 Sep 2003 17:02:47 -0500
Lines: 98
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:139201
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Tom,

Hmmm... So the All = in #1 below would use the 'Or' versus the 'Xor'? Does
a standard Or make ALL a unique ENUM? Or is that doing something slightly
different in VB, than in the C#?

I will try it! Thanks for the assist.......

Jim


Tom Spink said:
Hi James,

1:

<Flags(), Serializable()> _
Public Enum CardType
MasterCard = &H1
VISA = &H2
Amex = &H4
DinersClub = &H8
enRoute = &H10
Discover = &H20
JCB = &H40
Unknown = &H80
All = MasterCard Or VISA Or Amex Or DinersClub Or enRoute Or
Discover
Or
JCB
End Enum

2:

If (_cardTypes And CardType.Amex) = CardType.Amex Then
' Amex is present
End If

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


: Hello,
:
: I found some code that I could use in my application on the web, but
it
is
: written in C#, and I would like to convert it to VB. And I am having
: problems with one section. Is there anyone that could tell me how to
: convert these two things?
:
: 1)
:
: [Flags, Serializable]
: public enum CardType
: {
: MasterCard = 0x0001,
: VISA = 0x0002,
: Amex = 0x0004,
: DinersClub = 0x0008,
: enRoute = 0x0010,
: Discover = 0x0020,
: JCB = 0x0040,
: Unknown = 0x0080,
: All = CardType.Amex | CardType.DinersClub | CardType.Discover |
: CardType.Discover |
: CardType.enRoute | CardType.JCB | CardType.MasterCard | CardType.VISA
: }
:
:
: I understand how to do the ENUM, but I don't understand what the values
are?
: They seem like some kind of bit settings and I don't know how to do that.
:
: 2.)
:
: if (_cardTypes & CardType.Amex)!=0)
:
: where _cardTypes I believe contains some bitwise combination of the
: CardTypes from above.....
:
: Thanks for any help!
:
: Jim
:
:
 
Hi Herfried, Cor,

'AndThen' is how I think of 'AndAlso', which is somewhat awkward English
as far as my usage of it goes. Like "und auch", the also doesn't add anything
unless you're emphasising - "... and another thing...."

I would prefer BitAnd and BitOr to emphasis the bitwise nature of the
operation (and unsigned values to use it on) but changing the semantics of And
and Or would break a lot of code until the programmers writing/upgrading it
had a firm realisation of this radical change.

Ah, well,

Regards,
Fergus
 
Hello,

Fergus Cooney said:
I would prefer BitAnd and BitOr to emphasis the bitwise
nature of the operation (and unsigned values to use it on)
but changing the semantics of And and Or would break a
lot of code until the programmers writing/upgrading it
had a firm realisation of this radical change.

I think that was the main reason why they changed it back. They didn't want
to confuse the VB developers.
 
Hi Fergus,
Some weeks ago I was arguing about that in the newsgroup and told that in
Dutch (translated)
"en ook" is unusual and only used to give an extra dimension to "and". But
it is in normal Dutch awkward.

Some regulars from England and the US said that it was normal used English
but I did doubt it, because in using key words, Dutch is in many ways quiet
similar to English. But they said: "no it is well used English!"

Herfried said too that it was normal in the German language, that I can
absolute not oversee because in the German language are a lot of words quiet
similar with Dutch but it is grammatical totally different. The grammatical
seems to be from the bible from Luther, what was in the language from the
people from the mountain like Herfried.

And now you come with this sentence.
'AndThen' is how I think of 'AndAlso', which is somewhat awkward English
as far as my usage of it goes. Like "und auch", the also doesn't add anything
unless you're emphasising - "... and another thing...."

My first characters I did type were "Fergus I love you".
:-))
I would prefer BitAnd and BitOr to emphasis the bitwise nature of the
operation (and unsigned values to use it on) but changing the semantics of And
and Or would break a lot of code until the programmers writing/upgrading it
had a firm realisation of this radical change.
Make sense but I think that the most VB programmers avoid using bitwise
programming. I think 95% does not know they exist and 98% not how it works.
(Only my opinion).

Gives me a good feeling that I was not arguing* with only stupid arguments.


Cor
* do I nice use this word now or again wrong?
 
An example of 'AndThen' in everyday english would be:

If you subscribe to this newsgroup and then you phrase a question politely
then someone might answer it.

An example of 'AndAlso' in everyfday english would be:

If you have a PC and (you) also have Visual Studio .NET then you tou might
like to aprticipate in this newsgroup.
 
Stephany, (checked twice now)
An example of 'AndThen' in everyday English would be:
If you subscribe to this newsgroup and then you phrase a question politely
then someone might answer it.
An example of 'AndAlso' in everyday English would be:
If you have a PC and (you) also have Visual Studio .NET then you tou might
like to participate in this newsgroup.That is the same as in Dutch (with other words for "and" and "also" and
"then", but phonetical are "and" and "then" almost the same as in English).

When you look at a thread I tried to start about a serious discussion about
AndAlso the answer were totaly oposite.

Thanks,

(Did you make those typing errors express to tickle me or was it an accident
:-)) ?)

Cor
 
Hello,

Cor said:
Make sense but I think that the most VB programmers avoid
using bitwise programming.

Remember that in VB6 only bitwise 'And', 'Or', etc. operators existed.
 
Back
Top