sharing values at runtime...
-
i have a very basic problem and i dont know the reason for that.. suppose you have 3 forms if in form2 u code : form1.textbox1.text=textbox1.text it works as at runtime value of textbox of form2 transfers into textbox of form1.. even in form 3 u code form1.textbox1.text=textbox1.text it works.. but if in form3 u code form2.textbox1.text=textbox1.text it doesnt works.. can anybody tell the reason.. and alternate method of doing the same.. thanks ...
-
i have a very basic problem and i dont know the reason for that.. suppose you have 3 forms if in form2 u code : form1.textbox1.text=textbox1.text it works as at runtime value of textbox of form2 transfers into textbox of form1.. even in form 3 u code form1.textbox1.text=textbox1.text it works.. but if in form3 u code form2.textbox1.text=textbox1.text it doesnt works.. can anybody tell the reason.. and alternate method of doing the same.. thanks ...
You're breaking OOP encapsulation. The code on a form should be concerned with ONLY those controls on THAT form. It should not care about the controls of another form at all. If a TextBox on a form needs to be modified, it can either subscribe to an event supplied by the object that might need to make changes, or it can pass a delegate to that object that the object can use to call a method on your form. Having a form pass a reference of itself to another object so that object can modify the form's contents breaks encapsultation and permanently ties the object to that form. So much for reusability. For instance, if I have a class that does some work and needs to report status information, it can expose an event that does this:
Public Class WorkerClass
Public Event ProgressReport(ByVal SomeValue As Integer, ByVal SomeMessage As String)Public Sub DoSomeWork(_blah, blah_) ... RaiseEvent ProcessReport(somenumber, "Some message...") ... End Sub
End Class
In your Form1 code, you can create an instance of the class and wire up the events like this:
Dim myWorker As New WorkerClass AddHandler myWorker.ProgressReport, AddressOf MyProgressHandler
.
.
.
Private Sub MyProgressHandler(ByVal SomeValue As Integer, ByVal SomeMessage As String)
' This is where you update your forms controls
End SubA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
You're breaking OOP encapsulation. The code on a form should be concerned with ONLY those controls on THAT form. It should not care about the controls of another form at all. If a TextBox on a form needs to be modified, it can either subscribe to an event supplied by the object that might need to make changes, or it can pass a delegate to that object that the object can use to call a method on your form. Having a form pass a reference of itself to another object so that object can modify the form's contents breaks encapsultation and permanently ties the object to that form. So much for reusability. For instance, if I have a class that does some work and needs to report status information, it can expose an event that does this:
Public Class WorkerClass
Public Event ProgressReport(ByVal SomeValue As Integer, ByVal SomeMessage As String)Public Sub DoSomeWork(_blah, blah_) ... RaiseEvent ProcessReport(somenumber, "Some message...") ... End Sub
End Class
In your Form1 code, you can create an instance of the class and wire up the events like this:
Dim myWorker As New WorkerClass AddHandler myWorker.ProgressReport, AddressOf MyProgressHandler
.
.
.
Private Sub MyProgressHandler(ByVal SomeValue As Integer, ByVal SomeMessage As String)
' This is where you update your forms controls
End SubA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007