"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables
"Best" method for passing variables "Best" method for passing variables
"Best" method for passing variables
Go Back  Xtreme Visual Basic Talk > > > "Best" method for passing variables


Reply
 
Thread Tools Display Modes
  #1  
Old 04-19-2006, 10:52 AM
tek_man tek_man is offline
Freshman
 
Join Date: Mar 2006
Location: Georgia
Posts: 33
Default "Best" method for passing variables


Before I start...

I have read (and reread) the tutorial about passing variables between forms. I have also been searching / lurking the forums soaking up everything that I can. I have noticed, like most things in life, there are multiple ways of accomplishing the same task.

I have made 2 small projects that accomplish the same task using 2 different methods. Each project consist of 2 forms with a textbox and 2 buttons each. One button will set the text in the remote text box, and the other button will get the text from the remote text box.

My question is.....
Which method is "better", or are there situations where one is preferred over the other.

Method 1:
Create a new instance of Subform from MainForm
Pass (me) to subform by overriding Sub Main in SubForm

Method 2:
Create a new instance of Subform from MainForm
Use Event Handlers in MainForm to handle the button clicks in Subform

The Code for Method 1:
Code:
Public Class MainForm Inherits System.Windows.Forms.Form Private mySubForm As SubForm Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load mySubForm = New SubForm(Me) mySubForm.Show() End Sub Private Sub GetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles GetButton.Click MainTextBox.Text = mySubForm.SubTextBox.Text End Sub Private Sub SetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles SetButton.Click mySubForm.SubTextBox.Text = "Placed Here from Main Form" End Sub End Class Public Class SubForm Inherits System.Windows.Forms.Form Private myMainForm As MainForm Public Sub New(ByVal newMainForm As MainForm) MyBase.New() InitializeComponent() myMainForm = newMainForm End Sub Private Sub GetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles GetButton.Click SubTextBox.Text = myMainForm.MainTextBox.Text End Sub Private Sub SetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles SetButton.Click myMainForm.MainTextBox.Text = "Placed Here from Sub Form" End Sub


The Code for Method 2:
Code:
Public Class MainForm Inherits System.Windows.Forms.Form Dim mySubForm As SubForm Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load mySubForm = New SubForm mySubForm.Show() AddHandler mySubForm.ClickSet, AddressOf PlaceText AddHandler mySubForm.ClickGet, AddressOf GetText End Sub Private Sub PlaceText(ByVal NewString As String) MainTextBox.Text = NewString End Sub Private Sub GetText(ByRef myText As String) myText = MainTextBox.Text End Sub Private Sub SetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles SetButton.Click mySubForm.SubTextBox.Text = "Placed Here by Main Form" End Sub Private Sub GetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles GetButton.Click MainTextBox.Text = mySubForm.SubTextBox.Text End Sub End Class Public Class SubForm Inherits System.Windows.Forms.Form Private Sub GetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles GetButton.Click Dim myNewText As String RaiseEvent ClickGet(myNewText) SubTextBox.Text = myNewText End Sub Private Sub SetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles SetButton.Click RaiseEvent ClickSet("Placed Here by Sub Form") End Sub Public Shared Event ClickSet(ByVal NewText As String) Public Shared Event ClickGet(ByRef NewText As String) End Class

Thanks for any help or advice!

Lee
Reply With Quote
  #2  
Old 04-19-2006, 02:40 PM
shaul_ahuva"Best" method for passing variables shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Of the two, I would opt for the events (preferably sending the event data as parameters in the handler instead of allowing direct access to controls) - the main reason is to reduce the amount of coupling.

Using the first method has a high amount of coupling between forms - the second form can't survive without the main form and the update logic is interwoven between forms.

Using the second method lets the main form (and anyone else who's interested) deal with the events as it sees fit. The second form only cares if anyone's listening, it doesn't care what listeners do with the events.
Reply With Quote
  #3  
Old 04-19-2006, 06:28 PM
tek_man tek_man is offline
Freshman
 
Join Date: Mar 2006
Location: Georgia
Posts: 33
Default

Thanks,
That is what I was thinking, but I was not sure. Is there a scenario where the first example would be more appropriate? Or is there a better method than either of these 2 methods?

I appreciate the comments,

Lee
Reply With Quote
  #4  
Old 04-19-2006, 08:27 PM
shaul_ahuva"Best" method for passing variables shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

If you're working on a relatively small application and there's no pressing need to "stick to the rules" (i.e. doing everything the absolute "best" way may be overkill or it's a very temporary solution), the first method would probably be more appropriate. For instance, I created a very simple, temporary app to run basic queries against Oracle (just can't get used to SQL Plus ). However, in the main project I'm working on we are following normal OOP rules for a long list of reasons.

I can't think of a better way to accomplish the task - you need communication between two forms and the only way to do so is via the above methods (or a variation thereof).
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables "Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables "Best" method for passing variables
"Best" method for passing variables
"Best" method for passing variables
 
"Best" method for passing variables
"Best" method for passing variables
 
-->