|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Read private variables between formswho gives one more shining solution to me? My application load form "A". The form "A" by means of a push-button load form "B" that in its turn by means of a push-button load form "C". Form "B" contains one variable "Private" "VarB". How I can read the value of "VarB" from form "C"? All the form come loaded with the following brace of instructions: Sub Button2_Click(ByVal sender As System.Object, ByVal and As System.EventArgs) Handles Button2.Click Dim frm As New frm<A/B/C > frm.ShowDialog() End Sub In form "B" have declared a Property "RetVarB" that encapsulates the value of "VarB". From form "C" I succeed to read the value of "VarB" with the instruction: m_varC =frm.RetVarB if I declare "frm" like variable it only Public of the Form "B". Not creed but that this is the only possible method. To me it does not appeal to declare like "Public" the variable one "frm" in the form "B" Hello and Thanks. Add a module to your project, call it something like "globals" which
contains your variable. If you set the variable with the "Private" keyword, you will not be able to read that variable in ANY of your forms. But if you set it with the "Public" keyword, it will be accessible to all your forms -- from anywhere in your application. BUT. that goes without saying: "Don't expose what you don't have to." which basically means, you SHOULD set your variable with the "Private" keyword, and then add either a "ReadOnly" accessor to read the variable. For example: Private _MyVariable As String 'this is a private variable: note the "_" before the variable name. 'the accessor to read the value Public ReadOnly Property MyVariable() As String 'note the readonly keyword Get Return _MyVariable 'Returns the value of the variable End Get End Property Now if you want to read & write to the variable: Private _MyVariable As String 'this is a private variable: note the "_" before the variable name. 'the accessor to read the value Public ReadOnly Property MyVariable() As String 'note the readonly keyword Get Return _MyVariable 'Returns as string value End Get Set (ByVal Value As String) _MyVariable = Value 'accepts a string value End Set End Property By Adding a module that is separate from your forms, you can contain any number of variables with different accessors. You are not limited to accessors to read/write to variables, you can use Subroutines "Sub" or Functions. Example: Public Sub SetMyVariable(ByVal Str As String) 'set the variable _MyVariable = Str 'note Str is the parameter that in the routine. End Sub Public Funtion MyVariable(ByVal Str As String) Try 'check to see if you're setting the variable 'or wanting to read from it Select Case Str Case "" 'nothing is being passed to the function Case Else 'something is being passed to the function 'handle the case here End Select Catch (Ex As Exception) Console.WriteLine("Exception: " & Ex.toString()) Exit Function End Try End Function These are just a few examples of how you can deal with variables over multiple forms. There are other, more efficient ways, but you'll discover those on your own. One other thing: Regarding WinForms -- they are objects just like
everything in .NET so they should be treated as such. If you've got variables that are contained in one form and you want to read them in another form, you'll run into a hell of a lot of headaches, expecially if you're destroying the form (object) after you close it -- which is what you SHOULD do -- that is the reason you want to make sure variables that are shared throught your application are centralized in a spot that can be easily accessed by ANY form. |
|||||||||||||||||||||||