Solved

Start Up code in VB.net Windows Application

Posted on 2011-02-25
8
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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:…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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