Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. sharing values at runtime...

sharing values at runtime...

Scheduled Pinned Locked Moved Visual Basic
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manni_n
    wrote on last edited by
    #1

    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 ...

    D 1 Reply Last reply
    0
    • M manni_n

      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 ...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 Sub

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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 Sub

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        M Offline
        M Offline
        manni_n
        wrote on last edited by
        #3

        thanks dave, now it makes sense to me. as forms are classes so encapsulation concept will be voilated... i'll try this way and come back to you.. thanks..

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups