2017-06-13 3 views
1

関数からリストをクリアしたいと思っています。 別の関数からリストをクリアしようとすると、リストが見つかりません。 あなたは私が働いている私の完全なコードを表示する場合、それはここにある:https://pastebin.com/jrLiNZKd関数からリストをクリアする方法VB.NET

Module Module1 

    Structure Person 
     Dim Name As String 
     Dim DoB As Date 
    End Structure 
    Dim sFileName As String = "Z:/" ' the name and file path of the saved file 

    Public Sub Main() 
     Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 
     localDir = localDir & "/People.txt" 
     sFileName = localDir 
     Dim tempDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/temp.tmp" 
     ' create a new list to store our structure data type 'Person' 
     Dim lstPeople As New List(Of Person) 
     Dim chMenuOpt As Char 
     Do 
      DisplayMenu() ' call sub and display menu options 
      Console.WriteLine("There are currently " & lstPeople.Count & " people entered.") 

      chMenuOpt = Console.ReadKey.KeyChar ' get keyboard character and store in chMenuOpt 

      Select Case chMenuOpt 
       Case "1" 
        lstPeople.AddRange(GetPeople) ' get more people and add to lstPeople 
       Case "6" 
        Exit Do 
       Case "7" 

        IO.File.Create(tempDir).Dispose() 

        Dim s As New IO.StreamWriter(tempDir, True) 
        s.WriteLine(lstPeople.Count) 
        s.Close() 
        lstPeople.AddRange(TestCrapThing) 
       Case "8" 
        MakeFile() 
       Case "0" 
        'lstPeople.Clear() 
        CLearPeople(lstPeople) 
      End Select 
     Loop 
     ' user chose to exit program 
    End Sub 
    Sub CLearPeople(lst As List(Of Person)) 
     Console.Clear() 
     Console.WriteLine("All loaded people have been cleared!") 
     lst.Clear() 
     Console.WriteLine("Press any key to continue . . . ") 
     Console.ReadKey() 
    End Sub 

    Sub MakeFile() 
     Console.Clear() 
     Console.WriteLine("Do you really want to continue [Yes/No]") 
     Dim innnn As Char = Console.ReadKey.KeyChar 
     If LCase(innnn) = "y" Then 
      Console.Clear() 
      IO.File.Create(sFileName).Dispose() 
      Console.WriteLine("The file renewed!") 
      Console.WriteLine("Press any key to continue . . . ") 
      Console.ReadKey() 
     Else 

     End If 
    End Sub 
    Sub DisplayMenu() 
     Console.Clear() 
     Console.WriteLine("Please pick an option. There are loads to choose from xD") 
     Console.WriteLine() 
     Console.WriteLine("1) Add people") 
     Console.WriteLine("6) Quit") 
     Console.WriteLine("7) Load from file") 
     Console.WriteLine("8) Make file/Clear file") 
     Console.WriteLine("0) Unload people") 
     Console.WriteLine() 
    End Sub 

    Function TestCrapThing() As List(Of Person) 
     Console.Clear() 
     Dim count As Decimal = 0 
     Dim d As New IO.StreamReader(sFileName, True) 
     Do Until d.EndOfStream = True 
      d.ReadLine() 
      count = count + 1 
     Loop 
     d.Close() 
     Console.Clear() 
     Dim s As New IO.StreamReader(sFileName, True) 
     ' builds a list of people from the user. The user types "Stop" to end the list 
     ' when function ends, the list is returned 

     Dim tmpPerson As Person ' Used while entering data, added at end 
     Dim tmpDate As String ' stores user's DOB. This can be checked to see if valid, before storing 
     Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list 
     Console.Clear() 
     ' the loop goes around an infinite number of times. Only exits when user types 'Stop' 
     Console.WriteLine("In progress...") 
     Console.WriteLine("There are " & count & " lines.") 
     Console.WriteLine("") 
     Do Until s.EndOfStream = True 
      ' ############ 
      ' # Get Name # 
      ' ############ 
      Dim countt As Decimal = 0 
      Dim tempstuff As String = s.ReadLine 
      Dim tempsplit As String() = tempstuff.Split(New Char() {"|"}) 
      Dim part As String = tempsplit(1) 
      For Each part In tempsplit 
       countt = countt + 1 
       tmpPerson.Name = tempsplit(0) 
       tmpPerson.DoB = CDate(tempsplit(1)) 
       If countt = 2 Then 
        Console.WriteLine("Added: " & tempsplit(0)) 
        Console.WriteLine("with date of: " & tempsplit(1)) 
        tmpPeopleList.Add(tmpPerson) 
        countt = 0 
       End If 
      Next 
     Loop ' loop back around to get next person 
     Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 
     Dim tmp As New IO.StreamReader(localDir & "/temp.tmp", True) 
     If tmp.ReadLine = "0" Then ' If no people are loaded then it won't ask to clear it. 
      Console.WriteLine("") 
      Console.WriteLine("Imported new people!") 
      Console.WriteLine("Press any key to continue . . . ") 
      Console.ReadKey() 
      tmp.Close() 
      s.Close() 
      Return tmpPeopleList 
     Else 
      Console.WriteLine("") 
      Console.WriteLine("Do you want to clear all the existing people loaded into the program? [Yes/No]") 
      Dim inputty As Char = Console.ReadKey.KeyChar 
      If inputty = "y" Then 
       'need finish !!!!!!!!!!!!THIS IS WHERE I WANT TO CLEAR THE LIST !!!!!! 
       Return tmpPeopleList ' send temporary list back for processing 
       Console.WriteLine("") 
       Console.WriteLine("Cleared people and imported new people!") 
       Console.WriteLine("Press any key to continue . . . ") 
      Else 
       tmp.Close() 
       s.Close() 
       Return tmpPeopleList ' send temporary list back for processing 
       Console.WriteLine("") 
       Console.WriteLine("imported new people!") 
       Console.WriteLine("Press any key to continue . . . ") 
      End If 
     End If 
     Console.ReadKey() 
     tmp.Close() 
     s.Close() 
    End Function 
    Function GetPeople() As List(Of Person) 
     ' builds a list of people from the user. The user types "Stop" to end the list 
     ' when function ends, the list is returned 

     Dim tmpPerson As Person ' Used while entering data, added at end 
     Dim tmpDate As String ' stores user's DOB. This can be checked to see if valid, before storing 
     Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list 
     Console.Clear() 
     ' the loop goes around an infinite number of times. Only exits when user types 'Stop' 
     Do 
      ' ############ 
      ' # Get Name # 
      ' ############ 
      Console.WriteLine("Please enter the person's name. Type 'Stop' or 'Quit' to finish entering names.") 
      tmpPerson.Name = Console.ReadLine ' get name 
      If LCase(tmpPerson.Name) = "stop" Or LCase(tmpPerson.Name) = "quit" Then 
       Exit Do ' immediately exit, do not add to the list 
      End If 

      ' ########### 
      ' # Get DOB # 
      ' ########### 
      Do ' loop infinitely until user gives a valid date. 
       Console.WriteLine("Please enter " & tmpPerson.Name & "'s DOB.") 
       tmpDate = Console.ReadLine ' read into temporary string to validate date 

       If IsDate(tmpDate) = False Then ' user entered an invalid date 
        Console.WriteLine("Sorry, that was an invalid date. Please try again.") 
       Else 
        tmpPerson.DoB = CDate(tmpDate) ' convert to a date and store 
        Exit Do ' quit infinite loop as we have a valid date 
       End If 
      Loop 

      tmpPeopleList.Add(tmpPerson) ' remember, a function becomes the return value. 
     Loop ' loop back around to get next person 
     Return tmpPeopleList ' send temporary list back for processing 
    End Function 
End Module 
+0

「それはリストが見つかりません」という意味ですか?それはあなたがその明確な方法を呼び出すように見えません – Plutonix

+0

私は関数からリストをクリアする方法を見つけたいと思います。それは宣言されていないと言います – Simas

+0

@Simasそれはそれが宣言されていないと言う行ですか? – Jaxi

答えて

0

..だからあなたは呼び出された関数(かなり悪い練習TBH)とからlstPeopleをクリアしたいと仮定して、次のものが必要いくつかのことを変える。

OK - あなたは...

Function TestCrapThing(ByRef originalList As List(Of Person)) As List(Of Person) 

はまた、あなたのコードをバックアップするために、関数宣言を変更...あなたの関数にlstPeopleへの参照を渡す必要があり、あなたは今...

を使用して関数を呼び出すことができます
lstPeople.AddRange(TestCrapThing(lstPeople)) 

これは、パラメータlstPeopleTestCrapThingを呼び出すと、その関数への参照を渡すことを意味します。この機能では、これはoriginalListを使用して参照されます。この機会にlstPeople - あなたのコメント行で

..だから

..

'need finish !!!!!!!!!!!!THIS IS WHERE I WANT TO CLEAR THE LIST !!!!!! 

は、それが参照するリストをクリアします。このライン

originalList.Clear() 

を追加します。

ヴィオラ

+0

ありがとうございます:D – Simas

関連する問題