| | |
Scope of Variables
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
I put some variable declarations in the module of my project. Of the six variables, two of them remained unavailable to the code on the form... I got an error message. Those two, which are within a FOR...NEXT loop have to be declared in the form code for the thing to run. I'm using "Public" to declare the variables in the module... is there some other way to declare them in the module so they'll be available even within a loop in a form? BTW I'm a real novice and don't really know what I'm doing.
0
#2 Oct 23rd, 2009
Try posting your code here so we can see the issue. Be sure to use code tags:
[code]
...code here...
[/code]
[code]
...code here...
[/code]
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
Thanks. Here's what I put in the module:
...and here's what's on Form1:
In the Visual Studio error window I get two errors:
Error 1 'organisms' is not a member of 'KingLab1.Form1'.
Error 2 'organisminfo' is not a member of 'KingLab1.Form1'
If I declare the two variables on the form, instead of the module, the error messages disappear and the program runs fine.
VB.NET Syntax (Toggle Plain Text)
Module Module1 Public O As String Public orgArray() As String Public org(35, 4) As String Public d As Integer = 32 Public organisms As Integer Public organisminfo As Integer Public i As Integer = 0 End Module
...and here's what's on Form1:
VB.NET Syntax (Toggle Plain Text)
Public Class Form1 Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PutOrganismDataIn: 'Puts data into one-dimensionsal array 'Dim O As String, orgArray() As String O = "Smeglitious nobilis,monera,,s," _ & "Computicus garianus,monera,,s," _ & "Fastidyus hassica,monera,p,s," _ & "Diminutia scotia,protista,p,s," _ & "Strongellica sphinctius,protista,,s," _ & "Leigekonius dinero,protista,p,," _ & "Slumbricus cameronus,fungi,,," _ & "Conifera donaldi,fungi,,," _ & "Lasorderrico loco,fungi,,s," _ & "Reisus chaos,plant,p,," _ & "Wallius arizonica,plant,p,," _ & "Boggesnia ampulla,plant,p,," _ & "Pylorica thurlobius,animal,,," _ & "Drastium confusum,animal,,," _ & "Kathinius wispica,animal,,," _ & "Dibleria conus,monera,,s," _ & "Krelnius beautius,monera,,s," _ & "Bromlica graunus,monera,p,s," _ & "Pioneerica simia,protista,p,s," _ & "Mlefera grandia,protista,,s," _ & "Climficus uranus,protista,p,," _ & "Kladia mortia,fungi,,," _ & "Nerdius royalia,fungi,,," _ & "Blemius facia,fungi,,s," _ & "Scheterius krelmin,plant,p,," _ & "Ablimus californica,plant,p,," _ & "Robinsonius franka,plant,p,," _ & "Nametheria josepia,animal,,," _ & "Flarmica beautius,animal,,," _ & "Unermus blanca,animal,,," _ & "Querbica blondis,protista,p,," _ & "Blebnius clognica,plant,p,," orgArray = Split(O, ",") 'makes a new array splitting old _ ' array (O) at commas 'MsgBox(orgArray(5)) 'Shows "monera" in message box 'MsgBox(orgArray(4)) 'Shows "Computicus garianus" in message box ReadOrganismData: 'Following not necessary because declared as Public in Module 'Dim org(35, 4) As String 'Dim d As Integer = 32 'Dim organisms As Integer 'Dim organisminfo As Integer 'Dim i As Integer = 0 For Me.organisms = 1 To d 'Me necessary because variable is in For statement For Me.organisminfo = 1 To 4 org(organisms, organisminfo) = orgArray(i) i = i + 1 Next Next 'MsgBox(org(1, 1)) MsgBox(org(32, 1)) End Sub
In the Visual Studio error window I get two errors:
Error 1 'organisms' is not a member of 'KingLab1.Form1'.
Error 2 'organisminfo' is not a member of 'KingLab1.Form1'
If I declare the two variables on the form, instead of the module, the error messages disappear and the program runs fine.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
When I remove Me. from the variable, which is only declared in the module, I get the following warnings from Visual Studio:
Warning 1 The type for variable 'organisms' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisms', of use the fully qualified name (for example, 'Me.organisms' or 'MyBase.organisms').
Warning 2 The type for variable 'organisminfo' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisminfo', of use the fully qualified name (for example, 'Me.organisminfo' or 'MyBase.organisminfo').
Warning 1 The type for variable 'organisms' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisms', of use the fully qualified name (for example, 'Me.organisms' or 'MyBase.organisms').
Warning 2 The type for variable 'organisminfo' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisminfo', of use the fully qualified name (for example, 'Me.organisminfo' or 'MyBase.organisminfo').
•
•
Join Date: Aug 2009
Posts: 94
Reputation:
Solved Threads: 6
0
#7 Oct 25th, 2009
•
•
•
•
When I remove Me. from the variable, which is only declared in the module, I get the following warnings from Visual Studio:
Warning 1 The type for variable 'organisms' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisms', of use the fully qualified name (for example, 'Me.organisms' or 'MyBase.organisms').
Warning 2 The type for variable 'organisminfo' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'organisminfo', of use the fully qualified name (for example, 'Me.organisminfo' or 'MyBase.organisminfo').
For X=1 to 10
MsgBox(x)
Next
Whereas X does not need declarations (Dim X as Integer = 0 '<<< Don't need)
I also noticed that your loop is using manual increment(i = i + i). In FOR loops, X automatically adds 1 to itself on loop.
You should try my example in a test project. To better understand how FOR Loop Works
Last edited by yorro; Oct 25th, 2009 at 12:24 am.
![]() |
Similar Threads
- JavaScript Custom Object Scope: Why does it work? (JavaScript / DHTML / AJAX)
- scope of variables (C)
- C++ Performance Tips (C++)
- Having troubles with variables and array... (C++)
- Trouble with module-scope variables (Python)
- Couple of questions (PHP)
- "click Counter" help Please (Visual Basic 4 / 5 / 6)
- non-static and variable errors (Java)
- C++ tips (C++)
- problems with referring to class variables (Java)
Other Threads in the VB.NET Forum
- Previous Thread: Retrieve Data to ListBox
- Next Thread: Help Saving Opacity - Trackbar1
Views: 367 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 30minutes 2008 access add application array assignment basic binary box button buttons center click code connectionstring convert cpu data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dll dosconsolevb.net editvb.net employees error excel exists firewall folder image images isnumericfuntioncall listview login math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan peertopeervideostreaming picturebox plugin port print printing printpreview problem record regex reports" reuse right-to-left save savedialog search serial sorting sql sqldatbase storedprocedure string structures studio temp textbox timer upload useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml






