Best practice for sharing variables across forms in VB.NET -


I need to share the variables in VB.NET in two forms. One of these is the main form and the other one child is.

I am searching, and I have met in some ways. I want to use the best method to do this. I have listed some options below. Please note which best option will be:

  1. Create a fixed / shared variable in a form and access it in other forms :

      Form 1 FM = new Form 1 (); // Creating the purpose of the parent to access the shared variable frm.a = "abc"; // Passing value    
  2. Send an example of the main form when creating a child form. Variables can be accessed through a property function.

  3. Create global variables in a single module. This is the easiest option, but I doubt that this is the best option.

  4. I have read anything about representatives and events, but I do not know how to implement it. < P> If there is any other way that I have not mentioned, please share it with me.

    There is no answer to the question: All the methods listed by you must 'work'. The one you should use depends on why you want to share the variable. For example:

    1. You have a form with a list of records, and the user double clicks on a record, so that you open a new form to edit the record In this case I will add a constructor method to another form: sub-new (Record ID as string) "Add code to load record" end sub

    2. Something For In it or database path in your application might want something else to know global. For this, I create the proper variable as a parent in a public variable (called field) and use it as the mainform.field name. (Disclaimer: The Perchists will say that you should not rely on the somewhat crappy fact that VB.NET automatically instructs an installation of the form class and lets you refer to it as the form name, and instead you have a pointer Take the example of the actual form and keep it as your child and enter it as a parent, actually, it is like '2' in your post. It is not really necessary if you do not take care of the logical error.)

    3. Say something in your app, when the app started, you have to You can tell "You are using the app for 5 hours, get life!" These things can be stored in a module. (Or in the application class, but it is quite hidden)

Comments