"传递" VB.NET中的一个结构

时间:2016-05-22 11:46:29

标签: vb.net

我想在打开文件并分析其内容后在列表框中显示某些游戏的信息。 我已基本完成了我的项目,但我似乎无法完成它的工作。我想要做的是每当标题列表框的索引发生变化时,每个游戏的信息都会发生变化。 我的问题是我正在尝试使用一个结构来存储一个数组和其他一些数据,但是当下一个事件发生时(索引更改),结构变空了,我不知道我将如何传入(我认为它被称为实例)我的新功能或甚至有更好的方法来做到这一点。 这是我的代码:http://pastebin.com/i0Ga3fcD 任何帮助将不胜感激,如果您需要更多信息,我会尽我所能帮助您。

我的代码段:

Sub populateListboxWithInfo(ByRef strValidGames)  
        Dim intCount As Integer = 0  
        Dim intLengthOfArray As Integer = strValidGames.Length  

        Do While intCount < intLengthOfArray - 1  
            Dim strArraySplit() As String = strValidGames(intCount).Split(New Char() {","c})  
            lstInformation.Items.Add("Rating: " & strArraySplit(2))  
            lstInformation.Items.Add("Quantity: " & strArraySplit(4))  
            lstInformation.Items.Add("Price (per item): " & strArraySplit(5))  
        Loop  
    End Sub    

Function getValidGames(ByRef gameInfo)  
        Dim intLenghtOfArray As Integer  
        Dim intCount As Integer = 0  
        Dim strArrayValidGames() As String  
        Dim intArrayStore As Integer  

        intLenghtOfArray = gameInfo.strGameInfo.Length  
        Do While intCount < intLenghtOfArray - 2  
            Dim strArraySplit() As String =   gameInfo.strGameInfo(intCount).Split(New Char() {","c})  

            If strArraySplit(3) = gameInfo.CharType Then  
                If strArraySplit(1) = gameInfo.strPlatform Then  
                    ReDim Preserve strArrayValidGames(intArrayStore)  
                    strArrayValidGames(intArrayStore) = gameInfo.strGameInfo(intCount)  
                    intArrayStore += 1  
                End If  
            End If  
            intCount += 1  
        Loop  

        Return strArrayValidGames  
    End Function  

1 个答案:

答案 0 :(得分:0)

你的游戏实体应该是一个类 - 一个结构更倾向于小事(最多16个字节左右,IIRC)。通常不需要使用Hungarian notation - 样式的名称(当然,如果你有一个标签,文本框和关于同一事物的变量,那么匈牙利表示法的最小使用可能是有用的)。

Public Class Game
    Property Platform As String
    Property Title As String
    Property Game As String
    Property Price As Decimal
    Property Rating As Char
    Property Type As Char
    Property GameInfo As String()
End Class

声明函数时,需要声明参数的类型及其返回的类型,以便编译器确保变量类型匹配。

通常使用For循环来表示简单循环而不是While循环,通常使用ij和{等变量{1}}表示循环变量。

如果您发现自己使用的是k,那么现在可以考虑使用List,当您向其中添加新项目时会自动调整其大小。

因此,您的Redim函数看起来更像是:

GetValidGames

您没有显示实际上给您带来问题的代码;你需要这样的东西:

Function GetValidGames(gameInfo As Game) As String()
    Dim validGames As New List(Of String)

    For i As Integer = 0 To gameInfo.GameInfo.Length - 2
        Dim parts() As String = gameInfo.GameInfo(i).Split(",".ToCharArray())

        If parts(3) = gameInfo.Type Then
            If parts(1) = gameInfo.Platform Then
                validGames.Add(gameInfo.GameInfo(i))
            End If
        End If

    Next

    Return validGames.ToArray()

End Function