Solved

Start Up code in VB.net Windows Application

Posted on 2011-02-25
8
397 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
 
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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now