|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to compare objectsHi 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 if (someobject.GetType()) == typeof(SqlException) ...
-- Show quoteHide quoteHTH, 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 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 True on all counts, Jon.
-- Show quoteHide quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer Big things are made up of lots of little things. "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 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 > > > Andreiwid <Andrei***@discussions.microsoft.com> wrote:
> Thanks a lot Kevin! No, that will never work - because you're getting the type of the > > Adding information: VB.NET version > > If TypeOf objSomeObject.GetType() Is System.Data.SqlClient.SqlException Then > ... > ElseIf TypeOf... > ... > Else ... > ... > End If 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 Andreiwid <Andrei***@discussions.microsoft.com> wrote:
> I've a function that returns a System.Data.SqlClient.SqlException or I wouldn't *return* the exception if an error occurs - I'd *throw* the > 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? 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
Other interesting topics
.NET Framework Ignores Regional Settings on WinXP
why Assembly.LoadFrom() function does'nt load VC++ Project exe file HOW to solve ... System.Net.WebException: Connection closed Not all images are loaded correctly Manifest Resources: format programatically how to set the steps involved in enabling the ASP. How does FileSystemWatcher work? Documents on .NET framework reduce thumbnal size Printer settings |
|||||||||||||||||||||||