473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.
Nov 21 '05 #1
9 10349
"Lior" <aa*@aaa.com> schrieb:
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/groups?threadm=Xns94E599A1CA78fujicomxyz%40216.196 .105.130>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

Lior wrote:
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.

--
Larry Lard
Replies to group please

Nov 21 '05 #3
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
Nov 21 '05 #4


Cor Ligthert [MVP] wrote:
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.

--
Larry Lard
Replies to group please

Nov 21 '05 #5
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

Nov 21 '05 #6
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
Nov 21 '05 #7
"Larry Lard" <la*******@hotmail.com> schrieb:
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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
On Tue, 26 Jul 2005 12:41:57 -0100, "Lior" <aa*@aaa.com> wrote:
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
Nov 21 '05 #9
Lior,
In addition to the other comments, my response in this thread covers a
number of other options:

http://groups-beta.google.com/group/...53e96e4f34b30d

Hope this helps
Jay

"Lior" <aa*@aaa.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
| 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.
|
|
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mike Hale | last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or? MikeH
16
by: Raterus | last post by:
I kinda just stumbled across these operators, they seem great, as you can forget the second expression depending on the result of the first, but are there any cons to using these? From my...
11
by: A Traveler | last post by:
I was just curious if anyone knows how the combinations of And/AndAlso and Or/OrElse compare in terms of performance. Which takes more work for the system, performing two evaluations on an And or...
30
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, If x = y And m = n The .... End If If x = y AndAlso m = n Then .... End If
3
by: Siegfried Heintze | last post by:
Are there operators in C# that are the counterparts of "OrElse" and "AndAlso" that I can use when translating the following program from VB.NET to C#? Thanks, Siegfried Namespace vbtest...
8
by: Euvin | last post by:
I am kind of confuse as to how these operators work: AndAlso and OrElse Anyone care to explain. Some examples would be great!
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.