none
Can't Create Global Variables in Visual Basic Windows Form Application RRS feed

  • Question

  • Can't Create Global Variables in Visual Basic Windows Form Application.I've tried creating a class and module and using "Public Shared Test As String = ""' But every time I try to write to the variable, I get an error...

    Class Code:

    Public Shared Table1("nil") As String
    Public Shared Table2("nil") As String
    Public Shared Table3("nil") As String
    Public Shared Table4("nil") As String
    Public Shared Table5("nil") As String
    Public Shared Table6("nil") As String
    Public Shared GlobalFlag As String = ""


    Referencing the class:

    GlobalVariables.GlobalFlag = "test"

    Prodices this error...

    "conversion type from string "nil" to type 'Integer' is not valid."

    Thanks.

    More detailed error ...

    Exception thrown: 'System.TypeInitializationException' in SDD POS System.exe
    An unhandled exception of type 'System.TypeInitializationException' occurred in SDD POS System.exe
    The type initializer for 'SDD_POS_System.GlobalVariables' threw an exception.
    
    'SDD POS System.exe' (CLR v4.0.30319: SDD POS System.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    The program '[4512] SDD POS System.exe' has exited with code -1 (0xffffffff).

    • Edited by Jordaann.t Wednesday, June 26, 2019 2:19 AM Coz
    Wednesday, June 26, 2019 2:15 AM

Answers

  • Hi,

    What are you declaring? Array? This is a wrong way of declaring.

    Public Shared Table1("nil") As String
    Public Shared Table2("nil") As String
    Public Shared Table3("nil") As String
    Public Shared Table4("nil") As String
    Public Shared Table5("nil") As String
    Public Shared Table6("nil") As String

    Declare an array

     Public Shared Table(4) As String

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jordaann.t Wednesday, June 26, 2019 8:46 AM
    Wednesday, June 26, 2019 3:44 AM

All replies

  • Hi,

    What are you declaring? Array? This is a wrong way of declaring.

    Public Shared Table1("nil") As String
    Public Shared Table2("nil") As String
    Public Shared Table3("nil") As String
    Public Shared Table4("nil") As String
    Public Shared Table5("nil") As String
    Public Shared Table6("nil") As String

    Declare an array

     Public Shared Table(4) As String

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jordaann.t Wednesday, June 26, 2019 8:46 AM
    Wednesday, June 26, 2019 3:44 AM
  • Yeah sorry, I'm too used to the Swift syntax.

    I updated it to what you said ....

     Public Shared Table1(1) As String

    Then I tried to write to the variable ...

    GlobalVariables.Table1(0) = "test"

    However, I still get the error.

    Wednesday, June 26, 2019 4:31 AM
  • Hi,

    There is no problem with your code, it should be an error with other code,see my code:

    Public Class Form1
        Public Class GlobalVariables
            'Public Shared Table1(0) As String
            'Public Shared Table2(1) As String
            'Public Shared Table3(3) As String
            'Public Shared Table4("nil") As String
            'Public Shared Table5("nil") As String
            'Public Shared Table6("nil") As String
            'Public Shared GlobalFlag As String = ""
            Public Shared Table(4) As String
        End Class
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            GlobalVariables.Table(0) = "test0"
            GlobalVariables.Table(1) = "test1"
            GlobalVariables.Table(2) = "test2"
            GlobalVariables.Table(3) = "test3"
            GlobalVariables.Table(4) = "test4"
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox(GlobalVariables.Table(0))
            MsgBox(GlobalVariables.Table(4))
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 4:40 AM
  • Hmm... you are right. I copied and pasted your code into my actual project and it didn't work, but when I put it into a new project, it works... There must be some conflict with other lines of code or something. Thanks.
    Wednesday, June 26, 2019 4:51 AM
  • Hmm... you are right. I copied and pasted your code into my actual project and it didn't work, but when I put it into a new project, it works... There must be some conflict with other lines of code or something. Thanks.

    Do you resolve the issue?If you have anything else about this issue, please feel free to contact us.If you resolve the issue, please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 5:02 AM
  • Yes it did thanks.
    Wednesday, June 26, 2019 8:46 AM