Frage

In the code below, I want to be able to access the enteredusername and enteredpassword variables from any sub routine. How would I accomplish this?

 Using rdr As New FileIO.TextFieldParser("f:\Computing\Spelling Bee\stdnt&staffdtls.csv")
         rdr.TextFieldType = FieldType.Delimited
        rdr.Delimiters = New String() {","c}
        item = rdr.ReadFields()
    End Using
    Console.Write("Username: ")
    enteredusername = Console.ReadLine
    Console.Write("Password: ")
    Dim info As ConsoleKeyInfo
    Do
        info = Console.ReadKey(True)
        If info.Key = ConsoleKey.Enter Then
            Exit Do
        End If
        If info.Key = ConsoleKey.Backspace AndAlso enteredpassword.Length > 0 Then
            enteredpassword = enteredpassword.Substring(0, enteredpassword.Length - 1)
            Console.Write(vbBack & " ")
            Console.CursorLeft -= 1
        Else
            enteredpassword &= info.KeyChar
            Console.Write("*"c)
        End If
    Loop
    Dim foundItem() As String = Nothing
    For Each line As String In File.ReadAllLines("f:\Computing\Spelling Bee\stdnt&staffdtls.csv")
        Dim item() As String = line.Split(","c)
        If (enteredusername = item(0)) And (enteredpassword = item(1)) Then
            foundItem = item
            Exit For
        End If
    Next
War es hilfreich?

Lösung

To allow ALL classes within your program access the variable, you need to make it class-level and define it with Public and Shared.

Demonstration:

Public Class MainClass

    Public Shared enteredusername As String
    Public Shared enteredpassword As String

    Private Sub SomeSub()
        ' Some Code ...

        ' You can access it here:
        enteredusername = "something"
        enteredpassword = "something else"

        ' ... More Code ...
    End Sub
End Class

Public Class AnotherClass
    'Also, please note, that this class can also be in another file.

    Private Sub AnotherSub()
        ' Some Code ...

        ' You can also access the variable here, but you need to specify what class it is from, like so:
        Console.WriteLine(MainClass.enteredusername)
        Console.WriteLine(MainClass.enteredpassword)

        ' ... More Code ...
    End Sub
End Class

 

Also, on a separate note, the Public and Shared modifiers can also be used on methods. If you make a method Private or don't specify anything, the method will only be accessible from methods in the same class. If you use only Public, other classes can access the method, but they will need to create a instance of the class, like so:

Dim AC As New AnotherClass
AC.AnotherSub()

If you use both the Public and the Shared modifiers, other classes will be able to access the method directly, without creating a new instance. But, you must note, that Shared methods cannot access non-Shared methods or variables. Other classes can access Public Shared methods like so:

AnotherClass.AnotherSub()

Andere Tipps

It depends on the scope. If you want all of the subroutines in the current class to be able to access them then make them a field of the class

Class TheClassName
  Dim enteredusername As String
  Dim enteredpassword As String
  ...
End Class

If you want all subroutines in all classes and modules to be able to access them then make them a module level field

Module TheModuleName
  Dim enteredusername As String
  Dim enteredpassword As String
  ...
End Module

I recommend against this approach though. Sure it's easier in the short term because it requires less ceremony and thought on the uses of the values. But long term it serves to reduce the maintainability of your code base

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top