Print Page | Close Window

Testing for enums

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=358
Printed Date: 12-Jun-2026 at 1:26am


Topic: Testing for enums
Posted By: brianlj
Subject: Testing for enums
Date Posted: 08-Aug-2007 at 8:46am
Hi -
I have some problems when I try to test the value of an enum in code.  For example, I use:
 
If testentity.status Is testentitystatus.Ready then
.....
end if
 
This seems to work MOST of the time, but inexplicably fails to return TRUE at times (when I know the status is READY).
 
Any thoughts?  Should I be testing differently?
Thanks!



Replies:
Posted By: Linguinut
Date Posted: 08-Aug-2007 at 12:55pm
Hi Brian,
 
Have you put a break on that line to see what the actual values are for each of the test subjects?  testentity.status must be returning something other than testentitystatus.Ready for it to return false.  I state the obvious.  Perhaps you can trace back from the breakpoint why the status property has not changed by that point in the code.
 
Bill


Posted By: wdevine
Date Posted: 08-Aug-2007 at 3:22pm
My VB.NET isn't as polished as it should be, but I think I'd use something like this:
 
If testentity.status = testentitystatus.Ready then
 
I'm not 100% sure, but I think the 'Is' operator in the context that it is being used is and object compare.
 
Bill 
 
 
 


Posted By: brianlj
Date Posted: 09-Aug-2007 at 10:21am
Yes, you are correct that 'Is' does a literal object compare.  But since DevForce uses cached objects, my thought is they should be one in the same object.  And this seems to be true MOST of the time, but I have had bugs that I have traced back to this compare failing.  And unfortunately, I don't believe there is an operator overload for '='; I could probably write my own, but was trying to avoid doing this for every enum.
 
I have of course debugged and made sure the status is what I expect. 


Posted By: jsmark
Date Posted: 09-Aug-2007 at 1:29pm
Try doing something like testentity.Status.ID  = testentitystatus.Ready.ID
(ID being the identity field in the database)


Posted By: brianlj
Date Posted: 09-Aug-2007 at 6:29pm
testentity.Status.ID  = testentitystatus.Ready.ID is what I ended up doing and I believe should work just fine.  However, it would be nice if you could just compare the enum entities directly and not have to know the specific fields when trying to compare. I know, I know..... in a perfect world......just seems like DevForce should have all the info it needs to support this functionality.......
 
Thanks to all that provided some input here......



Print Page | Close Window