Solved

Start Up code in VB.net Windows Application

Posted on 2011-02-25
8
395 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

12 Experts available now in Live!

Get 1:1 Help Now