Re: usercontrol with readonly property that can be set at design t
- From: mmcd79@xxxxxxxxxxxxxxx
- Date: 16 Jul 2006 16:14:32 -0700
I probably said it wrong. I'll try to clarify what I meant. With the
code that I had, if the developer tried to set the code during design
time via the designer, the exception worked wonderfully.
If the developer tried to set the value elsewhere in code, the
exception wouldn't be thrown until runtime when the value would try to
be set.
What worries me in this scenario is say the developer set the value in
code, but in such a way that it wasn't set on load or any other
immediate time. Maybe it was a reaction to some other function that
never got called until well after the app was loaded. He may not get a
chance to "test" that functionality as the app loaded fine. The app
would be deployed and then someday as the app's being used, a horrible
exception gets thrown due to the developer's oversite.
Granted proper debugging of the entire app would help prevent this, but
most people don't do that, especially in my company. I'm trying to
prevent as much as possible since our normal "developers" aren't really
taking the time to do things right haha.
I hope that explains my worry and doubt for this method. I need a true
readonly property, but I need to be able to set it. Hopefully I'm not
just missing what you're saying.
Joris Zwaenepoel wrote:
I don't think this could cause much "interruption during runtime". The
property should never be set during runtime anyway (you wanted a read-only
property), so if you do try to set it, you get an exception the very first
time you test that code, and the exception message should be meaningful to
the developer, so that he understand what is wrong. Then the developer fixes
his code, and there are no more "interruptions at runtime".
Also, you could modify the property setter to not throw an exception, but
also not modifying the value of the property if it has been set before. That
would prevent the "interruption" but it would be very confusing for the
developer who tries to set the property, does not get an exception, but
notices that it does not work the way he expected. (Maybe a "debug.Assert"
could help, but only if the code is compiled in debug mode.).
Joris
"mmcd79@xxxxxxxxxxxxxxx" wrote:
Thanks Joris. I guess you could say that worked, although now I'm
thinking I really should do this a different way haha. The exception
works fine in the designer but it really causes some interruption
during runtime, obviously due to the exception I throw. I'd end up
having to do some exception handling in designmode vs realtime mode to
prevent total breakdown at runtime, and it's just getting too ugly.
I really appreciate the help and the insight into using
shortcircuiting. I never even thought of that. Nor did I even think
to try debugging :-x. I think it's cuz I knew the designmode if block
woulda been skipped during runtime. Denial I guess :).
Since I cannot think of an elegant solution to this I think I'm going
to just use a normal public property and let the developer know via
comment that setting this value more than once in code could wreak
havoc.
Another ugly thought, is there any way via reflection to possibly get
the calling procedure? Maybe I can limit the value to be set during
it's initialization phase so it can be set all it needs in the designer
(as it automatically generates that code), and then throw an exception
if it's tried to be set in any other procedure.
Thanks guys!
Joris Zwaenepoel wrote:
I suggest you step through the code in runtime, and you'll see what is wrong.
When not in "designmode", the value is not saved in the variable. Yo
ushould change you code as follows, to allow the property to be set in
runtime:
If Me.DesignMode orelse Not _PageIsSet Then
_WizardPageNumber = value
_PageIsSet = True
Else
Dim ex As New Exception("WizardPageNumber ....")
Throw ex
End If
Joris
"mmcd79@xxxxxxxxxxxxxxx" wrote:
I have set the following, but I'm still having an issue.
--------------------------------------------------------------------------------------------------
Public Property WizardPageNumber() As Integer
Get
Return _WizardPageNumber
End Get
Set(ByVal value As Integer)
If Me.DesignMode Then
Debug.WriteLine("we're in designmode")
'check to see if wizardpagenumber has already been set.
If so, throw exception alerting developer
'that this property can only be set once.
If Not _PageIsSet Then
_WizardPageNumber = value 'set page value
_PageIsSet = True 'set tracker variable to
prevent further setting of this value.
Else
Dim ex As New Exception("WizardPageNumber can only
be set once.")
Throw ex
End If
End If
End Set
End Property
--------------------------------------------------------------------------------------------------
If on a form I drop this control on the form, I can set the property
once via the designer. Any subsequent settings will raise an
exception. Super.
Now the problem I have is say I set the initial value to 5. Then as a
test set the property to a different value, say 3, during the form load
event. If I run a check for the value before and after setting it to
3, I get no exception, and both before and after the value is stuck at
0.
I'm assuming the inability to set the value is due to the "if
me.designmode" declaration. Any suggestions for this?
mmcd79@xxxxxxxxxxxxxxx wrote:
hahaha.. I'm such a retard. I'm researching all over to find out what
the "Joris Code" is as I sitting here thinking it's some well known
coding methodology. I couldn't find anything on it. Then I think,
"Self, he mentions something that the previous poster said as well.
Low and behold his name is Joris. *sigh*
I have seen several implementations of Joris' suggestions and some of
them have had an additional property, or private member so to speak
that tracks if the property has been set or not. Wasn't sure if that'd
be a good way to go. Guess I'll give it a shot.
Thanks again!
.
- References:
- usercontrol with readonly property that can be set at design time
- From: mmcd79
- Re: usercontrol with readonly property that can be set at design time
- From: mmcd79
- Re: usercontrol with readonly property that can be set at design time
- From: mmcd79
- Re: usercontrol with readonly property that can be set at design time
- From: mmcd79
- Re: usercontrol with readonly property that can be set at design t
- From: Joris Zwaenepoel
- Re: usercontrol with readonly property that can be set at design t
- From: mmcd79
- Re: usercontrol with readonly property that can be set at design t
- From: Joris Zwaenepoel
- usercontrol with readonly property that can be set at design time
- Prev by Date: Re: ClickOnce and login on as a different user
- Next by Date: Get Assembly version
- Previous by thread: Re: usercontrol with readonly property that can be set at design t
- Next by thread: control in user control to adjust with size
- Index(es):
Relevant Pages
|