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.

Recommended Answers

All 7 Replies

Try posting your code here so we can see the issue. Be sure to use code tags:

[code]

...code here...

[/code]

Thanks. Here's what I put in the module:

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:

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.

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

Thanks for the explanation and the tip... I'll try that.

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').

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

Thanks... right you are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.