【问题标题】:Variable '' is used before it has been assigned a value.变量 '' 在被赋值之前使用。
【发布时间】:2017-07-09 14:16:18
【问题描述】:

我正在尝试制作一个程序来下载一堆域并将它们添加到 Windows 主机文件中,但我遇到了一些麻烦。当我尝试将它们存储在列表中时,我不断收到错误消息。我不明白为什么它不起作用。

Sub Main()
    Console.Title = "NoTrack blocklist to Windows Hosts File Converter"
    Console.WriteLine("Downloading . . . ")
    Dim FileDelete As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt"
    If System.IO.File.Exists(FileDelete) = True Then
        System.IO.File.Delete(FileDelete)
    End If
    download()
    Threading.Thread.Sleep(1000)
    Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
    Dim tempRead As String ' = s.ReadLine
    Dim tempSplit As String() ' = tempRead.Split(New Char() {" "})
    Dim i As Integer = 0
    Dim tempStore As String()
    s.ReadLine()
    s.ReadLine()
    Do Until s.EndOfStream = True
        tempRead = s.ReadLine
        tempSplit = tempRead.Split(New Char() {" "})
        Console.WriteLine(tempSplit(0))
        tempStore(i) = tempSplit(0)'The part that gives me the error
        i = i + 1
    Loop
    Console.ReadKey()
End Sub


Sub download()
    Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    '"Enter file URL"
    Dim url As String = "https://quidsup.net/notrack/blocklist.php?download"
    '"Enter directory"
    Dim dirr As String = localDir & "/Downloads" & "/notracktemp.txt"
    My.Computer.Network.DownloadFile(url, dirr)
    'System.IO.File.Delete(localDir & "/notracktemp.txt")
End Sub

【问题讨论】:

标签: vb.net


【解决方案1】:

tempStore() 必须有一个大小 使用循环计算文件中的行数,然后将其声明为 tempStore(i) 其中 i 是行数。这是一个计算行数的函数。

    Function countlines()
    Dim count As Integer
    Dim s As New IO.StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/Downloads" & "/notracktemp.txt", True)
    s.ReadLine()
    s.ReadLine()
    count = 0
    Do Until s.EndOfStream = True
        s.ReadLine()
        count = count + 1
    Loop
    Console.WriteLine(count)
    Return count
    Console.ReadKey()
End Function

那么你要做的是:

    Dim count As Integer
    count = countlines()

    Dim tempStore(count) As String

【讨论】:

  • 不是一个好的答案,因为您实际上并没有显示解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 2014-06-08
  • 2019-01-16
  • 2020-11-13
  • 1970-01-01
  • 2011-05-02
相关资源
最近更新 更多