And vs. AndAlso , Or vs. OrElse and Set data type

  • Thread starter Herfried K. Wagner [MVP]
  • Start date
H

Herfried K. Wagner [MVP]

Lior said:
I know that the AndAlso and OrElse statements are short-circuiting And and
Or statements ,
respectively .

Should I always use (I don't like the word "always" ...) AndAlso instead
of And
and OrElse instead of Or ?

Why are And and Or statements remained in VB.NET ? is this for the
people who are migrating from VB6 ?

No! Use 'AndAlso' and 'OrElse' only if calls to both operands are not
mandatory. In addition to that, 'And' and 'Or' are still used to perform
binary operations like setting bits or combinbing bit masks:

\\\
<Flags()> _
Public Enum Styles
Filled = 1
Border = 2
FilledAndBorder = Filled Or Border
End Enum
..
..
..
Dim s As Styles = ...
If (s And Styles.Border) = Styles.Border Then
MsgBox("Border is set.")
End If
///
Plus , I have a question about Set data type . As I know , there's no Set
data type (as in Pascal) in VB.NET
(maybe in Visual Basic 2005?) . I'll give an example why do I need the
Set data type for :

I have a program , where there's a special treatment with the numbers
12,13,17,18,19 . So , Instead of write something like : If var=12
Or/OrElse var=13 Or/OrElse var=17
Or/OrElse var=18 Or/OrElse var=19 Then... (Or/OrElse means either Or or
OrElse) .
As you can see here , I deal with 5 numbers - 12,13,17,18,19 - but what
if I had 20 numbers or more to
deal with ?

VB.NET doesn't provide intrinsic support for set operations. However, it's
easy to implement basic set operations using VB.NET:

<URL:http://www.google.to/[email protected]>
 
L

Larry Lard

Lior said:
Hello .

I know that the AndAlso and OrElse statements are short-circuiting And and
Or statements ,
respectively .

Should I always use (I don't like the word "always" ...) AndAlso instead of
And
and OrElse instead of Or ?

You should use whichever one has the semantics you desire, which sounds
like a cop-out, and in fact is :) Being aware of the difference, as you
are, is most of the battle.
Why are And and Or statements remained in VB.NET ? is this for the people
who are migrating from VB6 ?

Broadly, yes. A VB6 programmer is used to And and Or being
non-short-circuiting, and language designers try and avoid surprising
their users. But it's a nice thing to offer short-circuiting operators
for those situations where they make code more concise, so AndAlso and
OrElse were provided (This isn't historically accurate, by the way, but
that doesn't matter).
Plus , I have a question about Set data type . As I know , there's no Set
data type (as in Pascal) in VB.NET
(maybe in Visual Basic 2005?) . I'll give an example why do I need the Set
data type for :
[snip]

There is currently no built-in support for set-like collections.
However, it is relatively straightforward to build your own collection
class that supports a .Contains method and enforces single membership.
Is there a way to shorten the Or/OrElse long statement , so it will be short
and readable -
in VB.NET or Visual Basic 2005 ?

And in VB2005, Generics will make it even easier to create your own
collection classes and have them be strongly-typed. For example, once
you (or more likely someone else) has done the necessary plumbing work
to define the generic class SetOf<Type>, you will be able to do things
like

Dim S As New SetOfInteger(11, 13, 15, 17)
Debug.Print S.Contains(7)
Debug.Print S.Contains(11)

Dim A As New SetOfString("apple", "banana", "cucumber")

Dim B As New SetOfColor(Color.Red, Color.Green, Color.Blue)

Personally I'm really looking forward to Generics.
 
C

Cor Ligthert [MVP]

Lior,

I don't understand as well not why with the upgrading from VB6 to VBNet,
that despite in somecases almost the whole program is upgraded, the old And
and Or from VB6 could not be have translated to other operators as well.

However choosen is that what means in other languages logical And is in
VBNet AndAlso and what in other programmes means Or is OrElse.

The And and Or are stayed for boolean operations and as you said non
short-circuited And and Or.

In other words. Where you use logical And you have to use in VBNet AndAlso
for the best performance and to overcome problems.

I hope this helps.

Cor
 
D

Dragon

Hello Lior,

First,
Why are And and Or statements remained in VB.NET ? is this for the people
who are migrating from VB6 ?

And/Or operators are useful when you have some function as second argument
and you want it to be executed no matter what second argument returns, e.g:

~
a>b And Func1() 'Func1 always executes
a>b AndAlso Func1() 'if a<=b Func1 is omitted
~

Second,
Is there a way to shorten the Or/OrElse long statement , so it will be short
and readable -
in VB.NET or Visual Basic 2005 ?

Well, you can use

~
Array.IndexOf(New Integer() {12, 13, 17, 18, 19}, var)<>-1

~

Note that array type should be equal to var type, or you'll get the
permanent false.



Hope this helps
 
L

Larry Lard

Cor said:
Lior,

I don't understand as well not why with the upgrading from VB6 to VBNet,
that despite in somecases almost the whole program is upgraded, the old And
and Or from VB6 could not be have translated to other operators as well.

It's bad practice to change the meaning of an existing language
element. I am guessing, for example, that this is why VB.NET uses
'Shared' where C# uses 'static' - because 'Static' already means
something (something quite different) in VB.

However choosen is that what means in other languages logical And is in
VBNet AndAlso and what in other programmes means Or is OrElse.

VB is not in the C family of languages; there is no a priori reason why
it should have these similarities.
The And and Or are stayed for boolean operations and as you said non
short-circuited And and Or.

In other words. Where you use logical And you have to use in VBNet AndAlso
for the best performance and to overcome problems.

No. Where you want a short-circuited and, use AndAlso. Where you want a
non-short-circuited and, use And. Different semantics, different
operators. There are no 'problems' if you actually know the language
you are coding in.
 
C

Cor Ligthert [MVP]

Larry,

My opinion in this is (because I find VBNet such a great programming
language) that it would have been better for the future when it was changed
to the acting as more general used statements. Your statements would only be
valid for me when every keyword in VB6 did still exist in VBNet AndAlso you
know, it is not. In my opinion is trying to go in two directions never
leading to the shortest and/or most efficient route.

This sentence of you is not true by the way
VB is not in the C family of languages; there is no a priori reason why
it should have these similarities.
It is not only the C family that acts like that, better in the C family it
is not used at all.
(Although translating the && you get the And).

However most is my opinion.

Cor
 
H

Herfried K. Wagner [MVP]

Larry Lard said:
It's bad practice to change the meaning of an existing language
element.

The meaning of 'And' and 'Or' didn't change. Additional operators were
introduced. In early versions of VB.NET 'BitAnd' and 'BitOr' operators were
planned, and 'And' and 'Or' should become short-circuiting operators.
However, this would have broken a lot of code without giving an easily way
to find where the code was broken.
 
J

J Kel

Hello .

I know that the AndAlso and OrElse statements are short-circuiting And and
Or statements ,
respectively .

Should I always use (I don't like the word "always" ...) AndAlso instead of
And
and OrElse instead of Or ?

Why are And and Or statements remained in VB.NET ? is this for the people
who are migrating from VB6 ?

Plus , I have a question about Set data type . As I know , there's no Set
data type (as in Pascal) in VB.NET
(maybe in Visual Basic 2005?) . I'll give an example why do I need the Set
data type for :

I have a program , where there's a special treatment with the numbers
12,13,17,18,19 . So , Instead of write something like : If var=12
Or/OrElse var=13 Or/OrElse var=17
Or/OrElse var=18 Or/OrElse var=19 Then... (Or/OrElse means either Or or
OrElse) .
As you can see here , I deal with 5 numbers - 12,13,17,18,19 - but what if
I had 20 numbers or more to
deal with ?

It would be nice if I could simply define a set of numbers :
{12,13,17,18,19} (Set , not an array) and check
if var's value belongs to the set. Something like : If var In
{12,13,17,18,19} . That would be less
complicated than a long statement including Or/OrElse as previously
mentioned .

Is there a way to shorten the Or/OrElse long statement , so it will be short
and readable -
in VB.NET or Visual Basic 2005 ?

Thanks in advance ,
Lior.

I am dumbfounded as to why the case statement has not been suggested
to you for this.

Select Case My Var
Case 12,13,17,18,19
'.... code to run
Case Else
'.... code to run if its not one of those numbers
End Select
 
L

Lior

Hello .

I know that the AndAlso and OrElse statements are short-circuiting And and
Or statements ,
respectively .

Should I always use (I don't like the word "always" ...) AndAlso instead of
And
and OrElse instead of Or ?

Why are And and Or statements remained in VB.NET ? is this for the people
who are migrating from VB6 ?

Plus , I have a question about Set data type . As I know , there's no Set
data type (as in Pascal) in VB.NET
(maybe in Visual Basic 2005?) . I'll give an example why do I need the Set
data type for :

I have a program , where there's a special treatment with the numbers
12,13,17,18,19 . So , Instead of write something like : If var=12
Or/OrElse var=13 Or/OrElse var=17
Or/OrElse var=18 Or/OrElse var=19 Then... (Or/OrElse means either Or or
OrElse) .
As you can see here , I deal with 5 numbers - 12,13,17,18,19 - but what if
I had 20 numbers or more to
deal with ?

It would be nice if I could simply define a set of numbers :
{12,13,17,18,19} (Set , not an array) and check
if var's value belongs to the set. Something like : If var In
{12,13,17,18,19} . That would be less
complicated than a long statement including Or/OrElse as previously
mentioned .

Is there a way to shorten the Or/OrElse long statement , so it will be short
and readable -
in VB.NET or Visual Basic 2005 ?

Thanks in advance ,
Lior.
 
J

Jay B. Harlow [MVP - Outlook]

Lior,
In addition to the other comments, my response in this thread covers a
number of other options:

http://groups-beta.google.com/group...55cc5/1f53e96e4f34b30d?hl=en#1f53e96e4f34b30d

Hope this helps
Jay

| Hello .
|
| I know that the AndAlso and OrElse statements are short-circuiting And and
| Or statements ,
| respectively .
|
| Should I always use (I don't like the word "always" ...) AndAlso instead
of
| And
| and OrElse instead of Or ?
|
| Why are And and Or statements remained in VB.NET ? is this for the
people
| who are migrating from VB6 ?
|
| Plus , I have a question about Set data type . As I know , there's no Set
| data type (as in Pascal) in VB.NET
| (maybe in Visual Basic 2005?) . I'll give an example why do I need the
Set
| data type for :
|
| I have a program , where there's a special treatment with the numbers
| 12,13,17,18,19 . So , Instead of write something like : If var=12
| Or/OrElse var=13 Or/OrElse var=17
| Or/OrElse var=18 Or/OrElse var=19 Then... (Or/OrElse means either Or or
| OrElse) .
| As you can see here , I deal with 5 numbers - 12,13,17,18,19 - but what
if
| I had 20 numbers or more to
| deal with ?
|
| It would be nice if I could simply define a set of numbers :
| {12,13,17,18,19} (Set , not an array) and check
| if var's value belongs to the set. Something like : If var In
| {12,13,17,18,19} . That would be less
| complicated than a long statement including Or/OrElse as previously
| mentioned .
|
| Is there a way to shorten the Or/OrElse long statement , so it will be
short
| and readable -
| in VB.NET or Visual Basic 2005 ?
|
| Thanks in advance ,
| Lior.
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top