Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,054 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,618 people online right now. Registration is fast and FREE... Join Now!




Scope of Variables

 
Reply to this topicStart new topic

> Scope of Variables, Where to put stuff so you can access it.

AdamSpeight2008
Group Icon



post 25 Jul, 2008 - 11:43 AM
Post #1


This tutorial gives a basic understanding of variable scope.
The scope of a variable is from which sections of code it can accessed.

vb

Public Class Form1
Dim Variable_A As string="Hello from A"
Public Variable_B as string ="Hello from B"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Variable_C as string="Hello from C"
End Sub
End Class


In the above example
Variable_A
It is able to be used by all functions and routines inside Form1 but not from outside of Form1 (Local to Form1)
Variable_B
It is able to to used by all functions and routines inside Form1 and from outside of Form1 (via Form1.Variable_A)
Variable_C
It is local to this routine, it can not be access from outside of it or the form.

But suppose you want a variable to be shared between 2 Forms, how?
You can use a module.
vb

Module ShareStuff
Public Variable_D as string="Hello from D"
Dim Variable_E as string = "Hello"

End Module

Variable_D
It is able to be used both inside and outside of Shared stuff.
Variable_E
It is able to to used by all functions and routines inside SharedStuff but not outside it

This is the same for different types not just strings.

I hope this help you to understand where to put things. ph34r.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/1/08 05:51PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month