Solved

Start Up code in VB.net Windows Application

Posted on 2011-02-25
8
401 Views
Last Modified: 2012-05-11
I want to fetch some data from the database and place it in memory for the entire life span of the application. BEsides putting it in my login form where can I put this kind of code in vb.net windows application. Also how do I declare these variables so they wil be global and acesible throughout the application until the application is closed. Thanks
0
Comment
Question by:TrialUser
8 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34982624
Declare the variable as Public WithEvents at the top of the startup form.
Populate them in the Form.Load event of the startup form
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 100 total points
ID: 34982643
Use the singleton design pattern.

More here:

http://www.codeproject.com/KB/vb/design_patterns.aspx

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 total points
ID: 34982717
You can use a class with a set of "Shared" members. That way they will persist and be available for the lifetime of the app, you just then need to initialize it from somewhere which you could feasibly do from the Main() method before any forms are displayed. Using the singleton pattern as a mechanism for storing global variables is a very sloppy practice.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Assisted Solution

by:MikkelAStrojek
MikkelAStrojek earned 100 total points
ID: 34982741
You could create a singleton class.
That is a class that is only instantiated once.
Make this available through a shared property/method returning the instance.
It's done in a class by defining a private shared variable of type the class it self.
And make a private constructor, making sure it does not get instantiated elsewhere.
In the property/method for the instance define somthing like this:
public class MySingletonClass
private shared _instance as MySingletonClass

private sub new()

end sub

public shared function getInstance() as MySingletonClass
if _instance is Nothing then
_instance = new MySingletonClass()
end if
return _instance
end function

end class

Open in new window



Make that class hold the information in private variables, and create methods for retrieving and accessing.

private _listOfStuff as List(of string) = nothing

public function getListOfStuff() as List(Of String)
if _listOfStuff is Nothing then
'access data layer and fill list 
 _listOfStuff = new List(Of String) 'Set/fill the variable with the required data
end if
return _listOfStuff 
end function

Open in new window

0
 
LVL 7

Assisted Solution

by:vbigham
vbigham earned 200 total points
ID: 35030057
I usually Create a file called GlobalVariables.vb with a Module called GlobalVariables, then all globally defined variables / properties can be defined within that module and available to any any form by using GlobalVariables.PropertyName.  Sometimes this the module even has a PopulateVariables() method (which gets the values from a db or web service or whatever).  PopulateVariables is called on the main form.Loaded event or even from a custom application entry point.

Here is an example to get you started:
Friend Module GlobalVariables

    Private _sCompanyName As String

    Public ReadOnly Property CompanyName As String
        Get
            ' Access this from other classes / forms with GlobalVariables.CompanyName '
            Return _sCompanyName
        End Get
    End Property


    ' Define other properties.. '


    Public Sub PopulateVariables()
        _sCompanyName = "My Company"

        ' Get and set other variable values.. '
    End Sub

End Module

Open in new window

0
 

Author Comment

by:TrialUser
ID: 35057538
vbigham:

I like the idea of Globalvarible.cs class. I understand the implementation of this class. I am still unclear as to where to call the Populatevariable method from. Can this be called from a splash screen by any chance? What exactly do you mean y :"or even from a custom application entry point."

Thanks for your suggestions.

0
 
LVL 7

Assisted Solution

by:vbigham
vbigham earned 200 total points
ID: 35058559
Sorry I should have paid more attention that you are coding in C#.

I believe that the splash screen would be an excellent place to call PopulateVariables since it really is a part of your initialization code.

By :"or even from a custom application entry point.",  I meant that it could be called from void main(string() args) (which is usually not as transparant to vb users).  

Again, sorry for the confusion I must have thought I was filtering by the VB language still..

Here's a (rough) translation of the same thing in c# just to make sure we're on the same page:
 
internal static class GlobalVariables {     

    private static string _sCompanyName;
  
    public static string CompanyName {
        Get { Return _sCompanyName; }
    }

    public static void PopulateVariables() {
        _sCompanyName = "My Company";
    }
}

Open in new window


One further note, most of the time I don't expose this class to external assemblies (hence the "internal" scope qualifier), but in situations where that is necessary it can be scoped "public" instead.
0
 

Author Closing Comment

by:TrialUser
ID: 35089512
thx a ton
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
how to check to see if datatable has headers or not 2 30
Getting error in connectionstring with Excel. 30 35
C# Gridview 1 33
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question