![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2007
Posts: 1
Rep Power: 0
![]() |
Declaring a Public Variable?
Hello everyone
,I need some help. I tried searching google for this, but still no luck, so I thought I'd ask you for help. In the source code, I put in: Dim a As Integer Dim b As Integer a = 500 b = 100 And in a button code, I put in: a = a-b But when I try to compile it, it says "a and b not defined." So I think I must need to declare the variable as Public, but I don't know how, so I thought you guys could help me :banana:. Thanks guys. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
Where is
vbnet Syntax (Toggle Plain Text)
vbnet Syntax (Toggle Plain Text)
[highlight=vbnet]Dim a As Integer = 500 Dim b As Integer = 100 [/hightlight] Last edited by PhilBon; Jul 21st, 2007 at 12:40 PM. Reason: I cant spell Highlight |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2005
Posts: 32
Rep Power: 0
![]() |
variable scope in vb.net
Hi,
Info on varaible scope: In addition to its type, a variable also has a scope. A programming element is available throughout the region in which we declare it. All code in the same region can refer to the element without qualifying its name. Block Scope A block is a set of statements terminated by an End, Else, Loop, or Next statement, for example within a For...Next or If...Then...Else...End If construction. A variable declared within a block can be used only within that block. In the following example, the scope of the integer variable i, is the block between If and End If, and can no longer be referenced when execution passes out of the block: If total < 100 Then Dim i As Integer i = sum + i End If Note: Even if the scope of an element is limited to a block, its lifetime is still that of the entire procedure. If we enter the block more than once during the procedure, a block variable retains its previous value. Procedure Scope An element declared within a procedure is not available outside that procedure. Only the procedure that contains the declaration can use it. Elements at this level are also known as local elements. We declare them with the Dim statement. Private Sub Savebutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Savebutton.Click Dim intTotal As Integer Dim i As Integer For i=0 to 50 intTotal = intTotal + i Next End Sub In the above example, the variables i and intTotal are local to the Savebutton_Click() procedure. If we attempt to set the value of the variable intTotal from within another procedure, Visual basic 2005 will complain that the variable has not been declared. The intTotal variable is said to have procedure-level scope. It is visible within the procedure only. Module Scope For convenience, the single term module level applies equally to modules, classes, and structures. We can declare elements at this level by placing the declaration statement outside of any procedure or block within the module, class or structure. When we make a declaration at the module level, the accessibility we choose determines the scope. The namespace that contains the module, class, or structure also affects the scope. Elements for which we declare Private accessibility are available for reference to every procedure in that module, but not to any procedure in a different module. The Dim statement at module level defaults to Private, if we do not use any accessibility keywords. However, we can make the scope and accessibility more obvious by using the Private keyword in the Dim statement. In the following example, the string variable strName is available to all procedures defined in the module. When the second procedure is called, it displays the contents of the string variable strName in a dialog box. Private strName As String Sub FirstProc() strName = “This variable is initialized and can be used in this module only .” End Sub Sub DisplayProc() MsgBox(strName) End Sub Namespace Scope If we declare an element at module level using the Friend or Public keyword, it becomes available to all procedures throughout the namespace in which the element is declared. In the statement below, the string variable strName can be used anywhere in the namespace of its declaration. Public strName As String ‘ strName is available throughout the namespace. Database programming using Visual Basic 2005 and Csharp 2005 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is this declaring a variable? | waveform | C++ | 21 | Jun 23rd, 2006 3:48 PM |
| Compiling Maverik 6.2 (from C) | megamind5005 | C | 16 | May 3rd, 2006 5:41 PM |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 5:41 AM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 5:45 PM |
| function | solomon_13000 | Java | 6 | Apr 2nd, 2005 11:42 PM |