Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

How to compare objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andreiwid - 30 Sep 2005 13:21 GMT
Hi guys!

I've a function that returns a System.Data.SqlClient.SqlException or
System.Int32 depending on the function's result. If occurs an error it
returns SQLException; if executes with no problem, it returns the @@IDENTITY
of the row which is an Int32 type.

Now I need to compare the type and give to the users the feedback. I ask
you: how can I compare objects. For example, if the function's result is a
SQLException or an Int32 type?

Is that technique right?

Thanks in advance
Kevin Spencer - 30 Sep 2005 14:04 GMT
if (someobject.GetType()) == typeof(SqlException) ...

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Big things are made up of
lots of little things.

> Hi guys!
>
[quoted text clipped - 11 lines]
>
> Thanks in advance
Jon Skeet [C# MVP] - 30 Sep 2005 14:11 GMT
> if (someobject.GetType()) == typeof(SqlException) ...

Note that that would only return true if the object is *exactly* a
SqlException - not if it's *derived* from SqlException.

It's usually better to use:

if (someObject is SqlException)
...

or

SqlException e = someObject as SqlException
if (e != null)
...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Kevin Spencer - 30 Sep 2005 21:50 GMT
True on all counts, Jon.

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Big things are made up of
lots of little things.

>> if (someobject.GetType()) == typeof(SqlException) ...
>
[quoted text clipped - 11 lines]
> if (e != null)
> ...
Andreiwid - 30 Sep 2005 14:47 GMT
Thanks a lot Kevin!

Adding information: VB.NET version

If TypeOf objSomeObject.GetType() Is System.Data.SqlClient.SqlException Then
  ...
ElseIf TypeOf...
  ...
Else ...
  ...
End If

> if (someobject.GetType()) == typeof(SqlException) ...
>
[quoted text clipped - 13 lines]
> >
> > Thanks in advance
Jon Skeet [C# MVP] - 30 Sep 2005 15:23 GMT
> Thanks a lot Kevin!
>
[quoted text clipped - 7 lines]
>    ...
> End If

No, that will never work - because you're getting the type of the
return value of GetType(), which is always going to be System.Type,
never System.Data.SqlClient.SqlException

Just use

If TypeOf objSomeObject Is System.Data.SqlClient.SqlException

Or, as I said, just throw the exception.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jon Skeet [C# MVP] - 30 Sep 2005 14:09 GMT
> I've a function that returns a System.Data.SqlClient.SqlException or
> System.Int32 depending on the function's result. If occurs an error it
[quoted text clipped - 6 lines]
>
> Is that technique right?

I wouldn't *return* the exception if an error occurs - I'd *throw* the
exception. You can then catch it in the error condition (preferrably
several stack frames up) or just look at the return value (as an int)
if there are no problems.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.