Scope of Variables

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 17
Reputation: OldQBasicer is an unknown quantity at this point 
Solved Threads: 0
OldQBasicer OldQBasicer is offline Offline
Newbie Poster

Scope of Variables

 
0
  #1
Oct 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,212
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
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]
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: OldQBasicer is an unknown quantity at this point 
Solved Threads: 0
OldQBasicer OldQBasicer is offline Offline
Newbie Poster

Code

 
0
  #3
Oct 23rd, 2009
Thanks. Here's what I put in the module:

  1. Module Module1
  2. Public O As String
  3. Public orgArray() As String
  4. Public org(35, 4) As String
  5. Public d As Integer = 32
  6. Public organisms As Integer
  7. Public organisminfo As Integer
  8. Public i As Integer = 0
  9.  
  10. End Module

...and here's what's on Form1:

  1. Public Class Form1
  2.  
  3. Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4. PutOrganismDataIn:
  5. 'Puts data into one-dimensionsal array
  6. 'Dim O As String, orgArray() As String
  7. O = "Smeglitious nobilis,monera,,s," _
  8. & "Computicus garianus,monera,,s," _
  9. & "Fastidyus hassica,monera,p,s," _
  10. & "Diminutia scotia,protista,p,s," _
  11. & "Strongellica sphinctius,protista,,s," _
  12. & "Leigekonius dinero,protista,p,," _
  13. & "Slumbricus cameronus,fungi,,," _
  14. & "Conifera donaldi,fungi,,," _
  15. & "Lasorderrico loco,fungi,,s," _
  16. & "Reisus chaos,plant,p,," _
  17. & "Wallius arizonica,plant,p,," _
  18. & "Boggesnia ampulla,plant,p,," _
  19. & "Pylorica thurlobius,animal,,," _
  20. & "Drastium confusum,animal,,," _
  21. & "Kathinius wispica,animal,,," _
  22. & "Dibleria conus,monera,,s," _
  23. & "Krelnius beautius,monera,,s," _
  24. & "Bromlica graunus,monera,p,s," _
  25. & "Pioneerica simia,protista,p,s," _
  26. & "Mlefera grandia,protista,,s," _
  27. & "Climficus uranus,protista,p,," _
  28. & "Kladia mortia,fungi,,," _
  29. & "Nerdius royalia,fungi,,," _
  30. & "Blemius facia,fungi,,s," _
  31. & "Scheterius krelmin,plant,p,," _
  32. & "Ablimus californica,plant,p,," _
  33. & "Robinsonius franka,plant,p,," _
  34. & "Nametheria josepia,animal,,," _
  35. & "Flarmica beautius,animal,,," _
  36. & "Unermus blanca,animal,,," _
  37. & "Querbica blondis,protista,p,," _
  38. & "Blebnius clognica,plant,p,,"
  39. orgArray = Split(O, ",") 'makes a new array splitting old _
  40. ' array (O) at commas
  41. 'MsgBox(orgArray(5)) 'Shows "monera" in message box
  42. 'MsgBox(orgArray(4)) 'Shows "Computicus garianus" in message box
  43.  
  44. ReadOrganismData:
  45. 'Following not necessary because declared as Public in Module
  46. 'Dim org(35, 4) As String
  47. 'Dim d As Integer = 32
  48. 'Dim organisms As Integer
  49. 'Dim organisminfo As Integer
  50. 'Dim i As Integer = 0
  51. For Me.organisms = 1 To d 'Me necessary because variable is in For statement
  52. For Me.organisminfo = 1 To 4
  53. org(organisms, organisminfo) = orgArray(i)
  54. i = i + 1
  55. Next
  56. Next
  57. 'MsgBox(org(1, 1))
  58. MsgBox(org(32, 1))
  59.  
  60. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 94
Reputation: yorro has a little shameless behaviour in the past 
Solved Threads: 6
yorro yorro is offline Offline
Junior Poster in Training
 
1
  #4
Oct 24th, 2009
Me is the form.

Your code is sorta like Form1.organisms

organisms is not a member of Me/Form, because the variable is a declared on the module. Thats why when you declare organisms in Me/Form, it runs fine.

Remove Me
Last edited by yorro; Oct 24th, 2009 at 7:59 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: OldQBasicer is an unknown quantity at this point 
Solved Threads: 0
OldQBasicer OldQBasicer is offline Offline
Newbie Poster
 
0
  #5
Oct 24th, 2009
Thanks for the explanation and the tip... I'll try that.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: OldQBasicer is an unknown quantity at this point 
Solved Threads: 0
OldQBasicer OldQBasicer is offline Offline
Newbie Poster

Still a Problem

 
0
  #6
Oct 24th, 2009
Originally Posted by OldQBasicer View Post
Thanks for the explanation and the tip... I'll try that.
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').
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 94
Reputation: yorro has a little shameless behaviour in the past 
Solved Threads: 6
yorro yorro is offline Offline
Junior Poster in Training
 
0
  #7
Oct 25th, 2009
Originally Posted by OldQBasicer View Post
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').
Any variable used to initiate a ForLoop does not need declaration

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: OldQBasicer is an unknown quantity at this point 
Solved Threads: 0
OldQBasicer OldQBasicer is offline Offline
Newbie Poster
 
0
  #8
34 Days Ago
Thanks... right you are.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC