Page 1 of 1

Scope of Variables Where to put stuff so you can access it. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Post icon  Posted 25 July 2008 - 12:43 PM

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

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.
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:

Is This A Good Question/Topic? 2
  • +

Replies To: Scope of Variables

#2 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Posted 23 January 2009 - 07:14 PM

For someone like me...self-taught at home (poor me, eh?)...this proved to be helpful. Keep up the good work AdamSpeight! ;)
Was This Post Helpful? 0
  • +
  • -

#3 bohemian9485  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 05-August 09

Posted 07 August 2009 - 02:11 PM

Although I'm already familiar with programming, your topic is like a refresher course. From someone who is new to VB.Net like me, it is easy to understand. Lots of thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1