Usercontrol - property value gets lost

G

Gary Kahrau

I need some help passing data to and from a usercontrol.
In the following Property DisplayValue,
If I hard code a return value (Return "Test Data"), then the data gets
returned.
If I work with any kind of assigned value, the return value is blank!
Any idea on what I am doing wrong?




Public Class UserControl1
Public saveVal As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
DisplayValue = TextBox1.Text
End Sub


Public Property DisplayValue() As String
Get
If saveVal <> "" Then
Return saveVal ' "Test Data" ' TextBox1.Text
Else
Return "Lost Value"
End If

End Get
Set(ByVal Value As String)
saveVal = Value
End Set
End Property


End Class
 
C

Chris Dunaway

Where are you assigning a value to you property? BTW: Your property
variable (saveVal) should probably be private.
 
R

Robert S. Liles

This works just fine on my machine.
_____________________________________

Public Class Form1

Inherits System.Windows.Forms.Form



'Windows Form Designer generated code



Dim MyControl As New UserControl1()



Private Sub SetVariable_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles SetVariable.Click



MyControl.DisplayValue = TextBox1.Text



End Sub



Private Sub GetVariable_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles GetVariable.Click



TextBox1.Text = MyControl.DisplayValue



End Sub

End Class



Public Class UserControl1



Private saveVal As String



Public Property DisplayValue() As String

Get

If saveVal <> "" Then

Return saveVal

Else

Return "Lost Value"

End If



End Get

Set(ByVal Value As String)

saveVal = Value

End Set

End Property

End Class
 

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

Top