Home All Groups Group Topic Archive Search About

How to compare objects

Author
30 Sep 2005 12:21 PM
Andreiwid
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

Author
30 Sep 2005 1:04 PM
Kevin Spencer
if (someobject.GetType()) == typeof(SqlException) ...

--
HTH,

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

Show quoteHide quote
"Andreiwid" <Andrei***@discussions.microsoft.com> wrote in message
news:A466F507-45A2-4236-81AB-2B85562BC54F@microsoft.com...
> 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
Are all your drivers up to date? click for free checkup

Author
30 Sep 2005 1:11 PM
Jon Skeet [C# MVP]
Kevin Spencer <kevin@DIESPAMMERSDIEtakempis.com> wrote:
> 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)
....

--
Jon Skeet - <sk***@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
Author
30 Sep 2005 8:50 PM
Kevin Spencer
True on all counts, Jon.

--
HTH,

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

Show quoteHide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1da749f0b3529b0d98c821@msnews.microsoft.com...
> Kevin Spencer <kevin@DIESPAMMERSDIEtakempis.com> wrote:
>> 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)
> ...
>
> --
> Jon Skeet - <sk***@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
Author
30 Sep 2005 1:47 PM
Andreiwid
Thanks a lot Kevin!

Adding information: VB.NET version

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


Show quoteHide quote
"Kevin Spencer" wrote:

> if (someobject.GetType()) == typeof(SqlException) ...
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> Big things are made up of
> lots of little things.
>
> "Andreiwid" <Andrei***@discussions.microsoft.com> wrote in message
> news:A466F507-45A2-4236-81AB-2B85562BC54F@microsoft.com...
> > 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
>
>
>
Author
30 Sep 2005 2:23 PM
Jon Skeet [C# MVP]
Andreiwid <Andrei***@discussions.microsoft.com> wrote:
> Thanks a lot Kevin!
>
> Adding information: VB.NET version
>
> If TypeOf objSomeObject.GetType() Is System.Data.SqlClient.SqlException Then
>    ...
> ElseIf TypeOf...
>    ...
> Else ...
>    ...
> 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.

--
Jon Skeet - <sk***@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
Author
30 Sep 2005 1:09 PM
Jon Skeet [C# MVP]
Andreiwid <Andrei***@discussions.microsoft.com> wrote:
> 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?

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.

--
Jon Skeet - <sk***@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

Bookmark and Share