473,383 Members | 1,922 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,383 software developers and data experts.

option strict on

I have a routine that does a standard comparison that I pass two objects to

Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolea

' Compares two values to determine if they are equal. Also compares DBNULL.Value

If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both are DBNull.Value
If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only one is DBNull.Value
Return A = B ' Value type standard compariso
'Return A Is

End Functio

If option strict is off and A = "AAA" and B = "AAA"

Return A = B ' Value type standard compariso
returns Tru

with option strict o
Return A Is
returns false

Why doesn't Return A Is B return true if the values are the same

I would like to leave option strict on......

Nov 22 '05 #1
3 1453

"droope" <an*******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
I have a routine that does a standard comparison that I pass two objects to.
Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean '
' Compares two values to determine if they are equal. Also compares DBNULL.Value. '
If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both are DBNull.Value. If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only one is DBNull.Value. Return A = B ' Value type standard comparison 'Return A Is B

End Function

If option strict is off and A = "AAA" and B = "AAA"

Return A = B ' Value type standard comparison
returns True

with option strict on
Return A Is B
returns false.

Why doesn't Return A Is B return true if the values are the same?

I would like to leave option strict on.......

As your A and B are object variables, they are reference types that store
the memory address of the actual object. Your example of assigning the same
string literal to your object variable results in both variables holding the
same address. Thus, with Option Strict off, evaluation of the expression A =
B returns true. As I'm sure you've noticed, if you turn Option Strict on,
use of the = operator with object variables is disallowed.
When you execute your function procedure with Option Strict on, you are
passing two different objects, correct, not two references to the same
object? Using the Is operator to compare two object variables returns true
only if both hold the same address (refer to the same object).
With two object variables assigned to the same string constant, I am unable
to replicate the behavior you cite (with Option Strict on, A Is B returns
false), but if I pass object variables that reference two different objects
to your function procedure, false is always returned.

--
Peter [MVP Academic]
Nov 22 '05 #2
droope,
In addition to Peter's comments, have you tried using Object.Equals instead?

Return A.Equals(B)
Return A = B ' Value type standard comparison 'Return A Is B Why doesn't Return A Is B return true if the values are the same? As Peter stated, when you pass values as Object you are receiving references
to objects on the heap, although they may have the "same value" they have
different "references" as they are distinct objects. The Is operator always
compares references!

Hope this helps
Jay

"droope" <an*******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com... I have a routine that does a standard comparison that I pass two objects to.
Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean '
' Compares two values to determine if they are equal. Also compares DBNULL.Value. '
If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both are DBNull.Value. If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only one is DBNull.Value. Return A = B ' Value type standard comparison 'Return A Is B

End Function

If option strict is off and A = "AAA" and B = "AAA"

Return A = B ' Value type standard comparison
returns True

with option strict on
Return A Is B
returns false.

Why doesn't Return A Is B return true if the values are the same?

I would like to leave option strict on.......

Nov 22 '05 #3
Cor
Hi Droope,

In addition to the others but written in another way

A IS B the have different names but they are the same (reference to the
same adres)

A = B the are different but have the same value (and are from the same type
to do this check)

Cor
Nov 22 '05 #4

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

Similar topics

9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
11
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
8
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
13
by: C. Moya | last post by:
I fully expected the lack of a way to set Option Strict globally to be fixed in SP1. I can't seem to figure out if it has been fixed or not. It still seems we have to add the declaration at the top...
1
by: Jerad Rose | last post by:
I believe this issue is specific to ASP.NET. Why does VB.NET (2.0) ignore the project-level setting for Option Strict? I have the setting turned on in web.config: <compilation debug="true"...
18
by: Poldie | last post by:
How do I turn it on? I'm using vb 2005 in visual studio 2005 sp1. In my web.config I have: <compilation debug="true" strict="true" /> In my Tools/Options/Projects and solutions/vb defaults...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
8
by: =?Utf-8?B?R3JlZw==?= | last post by:
We have an application in our office that has the Option Strict option set to off right now. I do understand it should be set to ON, but right now, I'm just going to continue with it this way since...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.