Solved

Start Up code in VB.net Windows Application

Posted on 2011-02-25
8
410 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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