在循环时循环使用类属性

问题描述 投票:0回答:1

我有一个像下面的代码,找到一个名为“模型”的类别属性,然后我将得到模型的名称。

但正如您所见,“parent”属性显示参数的上一级。我在参数组中的参数及其类似级联。我不知道他们目前在哪个级别使用下面的代码,但这还不够,因为如果我的参数非常低,我必须写下这个条件。

有没有快速的解决方案,使它更容易和明智的方式?

Public Class ParameterInfoClass
    Public Shared Sub GetSubvar(ByVal ParameterGroups As IScrNamedObjectList)
        Dim ParameterGroup As IScrParameterGroup
        Dim nParameterGroup As Integer
        Dim ParameterClass As String
        nParameterGroup = ParameterGroups.count
        For i As Integer = 0 To nParameterGroup - 1
            ParameterGroup = ParameterGroups.item(i)
            If ParameterGroup.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.name
            ElseIf ParameterGroup.parent.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.parent.name
            ElseIf ParameterGroup.parent.parent.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.parent.parent.name
                '...
                'This should be continue like this because i don't know in which level i will find the category name as "Model"
                '.
            End If
            DataGridView1.Rows.Add(ParameterClass, ParameterGroup.name)
        Next
    End Sub
End Class

用正确的解决方案制作这一部分会很棒。我认为do-while循环可以是一个选项但我不知道如何应用因为焦点是在这里查看参数的上层以找到“模型”类别之后我正在编写该模型的名称。

For i As Integer = 0 To nParameterGroup - 1
            ParameterGroup = ParameterGroups.item(i)
            If ParameterGroup.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.name
            ElseIf ParameterGroup.parent.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.parent.name
            ElseIf ParameterGroup.parent.parent.parent.category.name = "Model" Then
                ParameterClass = ParameterGroup.parent.parent.parent.name
                '...
                'This should be continue like this because i don't know in which level i will find the category name as "Model"
                '.
            End If
vb.net while-loop do-while
1个回答
1
投票

我没有太多时间去挖掘细节,但递归是我喜欢的东西所以我给你一个提示(我会帮助更多,但现在是我现在的时间)。

您应该创建一个简单的递归函数,而不是遍历每个可能的父级别,它将查找是否找到"Model",然后返回它,或者通过调用自己查找它来查找它的父级。

你必须使用这个新函数而不是你的If ElseIf ElseIf...潜在的无限功能。

我起草了一些看起来像我的意思的东西:

Private Function GetParameterClass(rootClassName As rootClass) As String
    If ParameterGroup.category.name = "Model" Then
        Return ParameterGroup.category.name
    End If
    If ParameterGroup.parent IsNot Nothing Then
        Return GetParameterClass(ParameterGroup.parent)
    End If

    Return ""
End Function

通过调用一个类似的函数,你将递归遍历每个父项,直到它找不到,并且第一次找到"Model"时,它将停止递归并返回它。

抱歉没有更精确,因为我必须自己回去工作!我会尽可能在今晚看你的帖子,以防万一。玩得开心!

编辑:

我对你正在上课的课程并不熟悉,所以在这个编辑中有很多猜测。以下是我将尝试解决您的问题的方法:

Public Class ParameterInfoClass
    Public Shared Sub GetSubvar(ByVal ParameterGroups As IScrNamedObjectList)
        For Each parameterGroup As IScrParameterGroup In ParameterGroups
            Dim parameterClass As String = GetParameterClassName(parameterGroup)

            If parameterName <> "" Then
                DataGridView1.Rows.Add(parameterClass, parameterGroup.Name)
            End If
        Next
    End Sub

    Private Shared Function GetParameterClassName(parameterGroup As IScrParameterGroup) As String
        If parameterGroup.category.name = "Model" Then
            Return parameterGroup.name
        End If
        If parameterGroup.parent IsNot Nothing Then
            Return GetParameterClass(parameterGroup.parent)
        End If

        Return ""
    End Function
End Class

GetParameterClassName背后的主要思想是,它要么找到parameterGroup.category.name = "Model",要么它将返回一个空字符串。我替换了你计划用For Each循环迭代的方式,这应该适用于大多数列表,但你可能需要调整那部分是IScrNamedObjectList不是或不包含列表或数组或其他东西。

每当找到GetParameterClassName时,parameterClass就不是空的,所以我们可以将这些信息添加到DataGridView1中。

如果您有任何问题,可以在评论中提出您的问题,我将很乐意为您服务。我喜欢递归!

© www.soinside.com 2019 - 2024. All rights reserved.