Problem accessing property value in User Control

D

David Hearn

I have a property in a user control that I am setting:

Private strPageName as String
Public Property PageName() as String
Get
Return strPageName
End Get
Set(byVal Value as String)
strPageName = Value
End Set
End Property

So why can't I access strPageName in any of my functions? I am setting this
property from the parent page that the user control is in. It is always
Nothing by the time I get to my functions even though I can see it come in
initially.

Thanks in advance!
 
M

Marina Levit [MVP]

You need to show us when and how you are calling these functions. Also,
exactly when you are setting the property.
 
D

David Hearn

Setting the property from the Page_Load event of the parent page.

Parent Page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim objHeader As New Header
objHeader.PageName = "Stock Check"
objHeader = Nothing
End If
End Sub

Public property is listed below in original message. Then I am am calling
the function in the user controls Page_PreRender event per a suggestion from
someone else. SetHeaderInfo is the name of the function and trying to pass
strPageName as the variable that is set in the property:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
SetHeaderInfo(strPageName)
End Sub


Thanks!
 
M

Marina Levit [MVP]

When would you be calling this method?

You are creating the object, setting a property, then setting the object
pointer to nothing - effectively meaning there are no more references to
this object, so nothing ever could call any methods on it. The object will
be garbage collected when the GC runs.

So there is no way you are calling any methods on the object. The object
never gets added to the page. You create it, set a property, then that's it.
Nothing else ever happens to it.
 
D

David Hearn

Marina,

I took the line out that sets objHeader to nothing to try it but my variable
is still coming out as nothing by the time the function gets called.

Thanks again!
 
M

Marina Levit [MVP]

Setting it to nothing doesn't really do anything, since it is a local
variable. As soon as the if block is done, the variable is out of scope,
and nothing anywhere can ever reference it again.

There is no possible way you are calling any methods on this object, because
as soon as the IF block is done, it can no longer be reference in any way.
 
D

David Hearn

Marina,

I got it working per some of Patrice's advice in the post above. Thanks for
your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top