0

I'm trying to declare a variable in a sub that can be used by other forms. For Example.

Public Class TestClass
    Public Sub MakeVars()
        for i as Int16 = 0 to NumOfVars
            Public Var(i) as String
        Next
    End Sub
End Class

Then on another file.

Var3 = 200

I know the naming vars with i doesn't work its just as an example.

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    You can't declare a "public" variable in a sub - it needs to be a member of the class. – Matt Hogan-Jones Feb 26 '18 at 13:49
  • Possible duplicate of [How do I declare a global variable in VBA?](https://stackoverflow.com/questions/2722146/how-do-i-declare-a-global-variable-in-vba) – Matt Hogan-Jones Feb 26 '18 at 13:50
  • Is this VBA or Visual Studio. The two are not the same and the rules are not the same. Please [edit] to remove the inappropriate tag. – Cindy Meister Feb 26 '18 at 13:50
  • Study the concept of `Scope`. Where you declare a variable determines its scope - where you can use it. Your `Public Var` (if it was declared properly) would only exist inside that `For Loop`. Also look up `Public` because it has nothing to do with scope. Please read [ask] and take the [Tour] – Ňɏssa Pøngjǣrdenlarp Feb 26 '18 at 14:39

1 Answers1

0

Approach this as a property of your class, rather than variables. Remember to declare an instance of the class in your main code to make them useable.

A couple of ideas to get you started:

Public Class TestClass
    Property Var() as String()
    ReadOnly Property Var(index as Int16) as string ' code returns Var(i)
' Other Class code in here too - including setting up the Var when initialising the Class.
End Class
AJD
  • 2,400
  • 2
  • 12
  • 22