变量在被赋值之前已被使用 [英] Variable has been used before it has been assigned a value

查看:72
本文介绍了变量在被赋值之前已被使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在任何地方都找不到我的具体问题的答案,所以我想我会提出一个新问题.

I can't find the answer to my specific problem anywhere so I figured I'd open a new question.

我有一个程序可以在 ASCII 和二进制之间转换文本.它通过在第一个数组中查找输入,获取该输入的索引并查看第二个数组中的索引号,然后将它找到的内容写入另一个变量来完成此操作.

I have a program that converts text between ASCII and Binary. It does this by finding the input in the first array, getting the index of that input and looking at that index number in the second one, then writes what it found to another variable.

代码如下:

Function ConvertBinaryToASCII(ByVal input As String) As String
    Dim ASCIIList() As String = {" ", "!", "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "x", "X", "y", "Y", "z", "Z"}
    Dim BinaryList() As String = {"00100000", "00100001", "01100001", "01000001", "01100010", "01000010", "01100011", "01000011", "01100100", "01000100", "01100101", "01000101", "01100110", "01000110", "01100111", "01000111", "01101000", "01001000", "01101001", "01001001", "01101010", "01001010", "01101011", "01001011", "01101100", "01001100", "01101101", "01001101", "01101110", "01001110", "01101111", "01001111", "01110000", "01010000", "01110001", "01010001", "01110010", "01010010", "01110011", "01010011", "01110100", "01010100", "01110101", "01010101", "01110110", "01010110", "01110111", "01010111", "01111000", "01011000", "01111001", "01011001", "01111010", "01011010"}
    Dim BinarySubstrings = Enumerable.Range(0, input.Length \ 8).[Select](Function(i) input.Substring(i * 8, 8)).ToArray()
    Dim counter As Integer = 0
    Dim result() As String
    Dim binaryMatch As Integer

    For Each e As String In BinarySubstrings
        binaryMatch = Array.IndexOf(BinaryList, e)
        result(counter) = ASCIIList(CInt(binaryMatch))
        counter += 1
    Next

    Return String.Join("", result)
End Function

该函数按预期工作,但问题是无论我如何尝试调用或初始化 result,它总是给我某种错误,我无法弄清楚如何使用它.

The function works as intended except the problem is that no matter how I try to call or initialize result it always gives me some sort of error and I can't figure out how to use it.

推荐答案

使用 List(Of String) 更好,因为您不需要知道数组的大小.
当然你需要在使用前初始化List(Of String).

Using a List(Of String) is better because you don't need to know the size of the array.
Of course you need to initialize the List(Of String) before usage.

Function ConvertBinaryToASCII(ByVal input As String) As String
    Dim ASCIIList() As String = .....
    Dim BinaryList() As String = ......
    Dim BinarySubstrings = Enumerable.Range(0, input.Length \ 8).[Select](Function(i) input.Substring(i * 8, 8)).ToArray()
    Dim result = new List(Of String)()
    Dim binaryMatch As Integer

    For Each e As String In BinarySubstrings
        binaryMatch = Array.IndexOf(BinaryList, e)
        result.Add(ASCIIList(CInt(binaryMatch)))
    Next

    Return String.Join("", result.ToArray())
End Function

这篇关于变量在被赋值之前已被使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆