1

Environment: Asp.net 4.5, Webforms

I'm creating a composite control. I've exposed multiple public properties, but running into a slight problem.

Let's say I have two properties:

Public Property Path() As String
   Get
       Return ViewState("Path")
   End Get
   Set(ByVal Value As String)
       If UseAbsolute = True Then
           ' do something
       Else
           ' it always lands heere...
       End If
   End If
   ViewState("Path") = Value
   End Set
End Property
Private _Path As String = String.Empty

Public Property UseAbsolute() As Boolean
    ....
End Property
Private _UseAbsolute As Boolean = False

My controls are being assigned values on PreRender. The problem is, when I call "Path" it's getting the default/private value for UseAbsolute. So even if I set the property to True in the control/html, it grabs the false first.

I can work around this many ways, but I feel I'm missing a proper method or understanding.

UPDATE

I forgot to mention. I am:

EnsureChildControls()

in the PreRender...

I also tried adding this to the properties themselves.

user1447679
  • 3,076
  • 7
  • 32
  • 69
  • I'd like to know if there's a solution also. One of your likely workarounds is what I'm doing -- My properties are just getters and setters. I'm doing all the logic inside of `CreateChildControls()` – InbetweenWeekends Jun 23 '15 at 20:32
  • @onskee throw me an upvote? Maybe a genius will come along and make us feel silly. – user1447679 Jun 23 '15 at 20:48
  • I would think that if one of the properties are available, all of them would be. – user1447679 Jun 23 '15 at 20:51

1 Answers1

0

Shouldn't Your code be like this instead of how you have it?

Private _Path As String = String.Empty
Private _UseAbsolute As Boolean = False
'
Public Property Path() As String
  Get
    Return _Path
  End Get
  Set(ByVal Value As String)
    If UseAbsolute = True Then
      ' do something
      _Path = some_absolute_path
    Else
      ' do something else
      _Path = some_relative_path
    End If
  End Set
End Property
'
Public Property UseAbsolute () As Boolean
  Get
    Return _UseAbsolute
  End Get
  Set(ByVal Value As Boolean)
    _UseAbsolute = Value
  End Set
End Property
Zeddy
  • 2,079
  • 1
  • 15
  • 23
  • I've seen this both ways, separate from the public and many times beneath each public properties. Is this not more of an organizational preference? Technically my code is just like yours, so that wasn't the issue... but does it have any relevance? – user1447679 Jun 24 '15 at 15:58
  • There is a slight difference, the variables used to STORE the values start with underscore, the property methods do NOT. In your code the property method and variable both have the same names! I would think it is better to declare the variables FIRST and then the property methods after. Did you even TRY my solution? – Zeddy Jun 24 '15 at 17:28
  • Actually from what I understand, I'm writing things in the long method, as the private with the underscore is inferred, not that there's anything wrong with writing it completely out. In regards to "Did you even TRY my solution?", I stated in my first comment that "Technically my code is just like yours, so that wasn't the issue". I'm not convinced though, and would love to be proven wrong. I don't think the placement has anything to do with it ( for the most part/common scenarios ) – user1447679 Jun 24 '15 at 21:40
  • I would also think that if one property is available, the others would be. I'm searching to see if I've made a mistake somewhere else. Thanks for your help. – user1447679 Jun 24 '15 at 21:41
  • Aha. I did find a typo in my example, missing the underscore on _Path... is that one of the issue you were referring to? This is correct in my real code, just missed it when typing an example here. – user1447679 Jun 24 '15 at 21:43