473,413 Members | 1,713 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,413 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 1455

"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...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.