473,902 Members | 4,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10399
"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= Xns94E599A1CA78 fujicomxyz%4021 6.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("ap ple", "banana", "cucumber")

Dim B As New SetOfColor(Colo r.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(N ew 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*******@hotm ail.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,1 9} (Set , not an array) and check
if var's value belongs to the set. Something like : If var In
{12,13,17,18,1 9} . 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******** ******@TK2MSFTN GP14.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
4645
by: Mike Hale | last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or? MikeH
16
1803
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 viewpoint it seems I can pretty much to a find / replace on my existing code and pick up a performance boost, but something seems fishy that I really never see other people using these! Thanks for any insight! --Michael
11
6508
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 performing short-circuiting on an AndAlso? Purely for enlightenment. Thanks in advance. - Arthur Dent.
30
4609
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
19266
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 Module Main Function reflect(x as boolean) as boolean
8
1260
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!
0
11279
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.