.net里的一个算法、变量定义的问题!

jasonpower 2004-07-13 08:18:50
一个简单的算法问题。
有一个无级分类的产品目录,使用Datalist嵌套绑定。我想要得到每个级别所包含的产品的数量,使用一个函数GetTypeCount(TypID),返回String.Concat(Counts)。
问题出现了,如果我把Counts定义在函数的内部,如果发生循环调用函数,则Counts每次会被清零。
如果我把Counts定义在函数的外面,每个分类会得到前面一个分类(同一层的)的数量。即A分类记数结果为3,因为页面没有重新执行,所以同级的B分类调用函数Counts的值就从3开始了,我在String.Concat(Counts)语句之后,添加了一句
Counts=0,也无济于事!
函数本身是可以完成无级分类的记数工作的,我的设计是只有页分类结点才可以添加产品数据。所以循环调用函数是必需的!好象就是如何定义这个变量,或者说该使用什么别的途径,解决这个问题!
先谢谢各位了!
...全文
130 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jason108 2004-07-15
  • 打赏
  • 举报
回复
关注!
jasonpower 2004-07-14
  • 打赏
  • 举报
回复
再顶上去!
jasonpower 2004-07-13
  • 打赏
  • 举报
回复
对不起,应该是Return String.Concat(Test),我实际用的是另一个变量名。
……
Dim Test As Integer= Counts
Counts = 0
Return String.Concat(Test)
……
jasonpower 2004-07-13
  • 打赏
  • 举报
回复
断点提示:
名称 值 类型
GetTypeCount Nothing String
Test 3 Integer
Counts 0 Integer

test等于3和counts等于0都没有错,为什么函数GetTypeCount----
Return String.Concat(Tem)的值为Nothing呢?不是应该为3的么?
vzxq 2004-07-13
  • 打赏
  • 举报
回复
你加早断点,我这里不能调试。看看执行情况
jasonpower 2004-07-13
  • 打赏
  • 举报
回复
to vzxq(灵感人),是的。你说的我的确写错了,不过函数还是有问题,如果遇到需要循环调用函数自身的时候,
int test = Counts;
Counts = 0
应该被执行的!结果变成如果循环调用,记数结果就是0,我使用int test += Counts;也无济于事!
vzxq 2004-07-13
  • 打赏
  • 举报
回复
int test = Counts;
Counts = 0
Return String.Concat(test)
在返回前把Counts 给另外一个变量》
然后把counts清0
然后再返回
你先返回了,Counts=0 执行不到
jasonpower 2004-07-13
  • 打赏
  • 举报
回复
代码有点长,谢谢诸位帮忙看一下!我是要判断只有叶分类节点才去产品数据表里统计数量的!
'得到各级分类的产品数量
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
vzxq 2004-07-13
  • 打赏
  • 举报
回复
我觉的是你清零的方法不对,能把代码帖出来吗?
boy119 2004-07-13
  • 打赏
  • 举报
回复
我觉得设置清零应该可以解决,怎么会没用呢?
jasonpower 2004-07-13
  • 打赏
  • 举报
回复
谢谢楼上的回复,这个提议有人说过。我在函数内部使用静态变量定义,
Static Counts As Integer,不知道我理解的是否正确?不过问题还是没有解决,还是会累加!
dama2003 2004-07-13
  • 打赏
  • 举报
回复
静态变量

好运
vzxq 2004-07-13
  • 打赏
  • 举报
回复
用静态变量

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧