谢谢楼上的回复,这个提议有人说过。我在函数内部使用静态变量定义,
 Static Counts As Integer,不知道我理解的是否正确?不过问题还是没有解决,还是会累加!

解决方案 »

  1.   

    代码有点长,谢谢诸位帮忙看一下!我是要判断只有叶分类节点才去产品数据表里统计数量的!
        '得到各级分类的产品数量
        Function GetTypeCount(ByVal TypID As String) As String
            Dim MyCls As New MainClass
            Static Counts As Integer        Dim IsLast0 As New OleDb.OleDbCommand("select cname from products_type where parentid=" & TypID, MyCls.Perconn)
            Dim LastLe0 As String = String.Concat(IsLast0.ExecuteScalar)
            IsLast0.Dispose()        If LastLe0 = "" Then
                Dim CountCom1 As New OleDb.OleDbCommand("select count(id) from products where typeid=" & TypID, MyCls.Perconn)
                Counts += Integer.Parse(CountCom1.ExecuteScalar)
                CountCom1.Dispose()
            Else
                Dim MyAdp As New OleDb.OleDbDataAdapter("select id,parentid from products_type", MyCls.Perconn)
                Dim MyDr As New DataSet
                MyAdp.Fill(MyDr, "products_type")            Dim MyRow() As DataRow
                MyRow = MyDr.Tables("products_type").Select("parentid=" & TypID)
                Dim I As Integer
                For I = 0 To MyRow.Length - 1
                    Dim IsLast As New OleDb.OleDbCommand("select cname from products_type where parentid=" & MyRow(I).Item("id"), MyCls.Perconn)
                    Dim LastLe As String = String.Concat(IsLast.ExecuteScalar)
                    IsLast.Dispose()                If LastLe = "" Then
                        Dim CountCom As New OleDb.OleDbCommand("select count(id) from products where typeid=" & MyRow(I).Item("id"), MyCls.Perconn)
                        Counts += Integer.Parse(CountCom.ExecuteScalar)
                        CountCom.Dispose()
                    Else
                        GetTypeCount(String.Concat(MyRow(I).Item("id")))
                    End If
                Next
            End If        Return String.Concat(Counts)
            Counts = 0        MyCls.PerClose()
            MyCls = Nothing
        End Function
      

  2.   

    int test = Counts;
     Counts = 0
    Return String.Concat(test)
    在返回前把Counts 给另外一个变量》
    然后把counts清0
    然后再返回
    你先返回了,Counts=0 执行不到
      

  3.   

    to vzxq(灵感人),是的。你说的我的确写错了,不过函数还是有问题,如果遇到需要循环调用函数自身的时候,
    int test = Counts;
     Counts = 0
    应该被执行的!结果变成如果循环调用,记数结果就是0,我使用int test += Counts;也无济于事!
      

  4.   

    断点提示:
    名称                 值      类型
    GetTypeCount Nothing String
    Test          3 Integer
    Counts          0 Integertest等于3和counts等于0都没有错,为什么函数GetTypeCount----
    Return String.Concat(Tem)的值为Nothing呢?不是应该为3的么?
      

  5.   

    对不起,应该是Return String.Concat(Test),我实际用的是另一个变量名。
    ……
            Dim Test As Integer= Counts
            Counts = 0
            Return String.Concat(Test)
    ……