Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 21st, 2007, 4:25 AM   #1
sharkbate24
Newbie
 
Join Date: Jul 2007
Posts: 1
Rep Power: 0 sharkbate24 is on a distinguished road
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.
sharkbate24 is offline   Reply With Quote
Old Jul 21st, 2007, 12:40 PM   #2
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
Where is
vbnet Syntax (Toggle Plain Text)
  1. Dim a As Integer
  2. Dim b As Integer
  3. a = 500
  4. b = 100
and
vbnet Syntax (Toggle Plain Text)
  1. a = a+b
? They have to be in the same procedure, or put the dim a as integer and dim b as interger, at class level (i think thats what you wanted instead). A Class level variable means that everything in the class can use it. A public variable means that ANY other programs can use it, it's tricky but they still can. Suggestion is look up class level variables (it's not to hard, it's just where you place the declaration). Suggestion use this:
[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
PhilBon is offline   Reply With Quote
Old Aug 9th, 2007, 2:18 AM   #3
Bharathi
Programmer
 
Join Date: Apr 2005
Posts: 32
Rep Power: 0 Bharathi is on a distinguished road
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
Bharathi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC