Foxtable(狐表)用户栏目专家坐堂 → 能不能用For...Next给多个变量赋值?


  共有1801人关注过本帖树形打印复制链接

主题:能不能用For...Next给多个变量赋值?

帅哥哟,离线,有人找我吗?
guning008
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:婴狐 帖子:44 积分:600 威望:0 精华:0 注册:2016/10/1 11:15:00
能不能用For...Next给多个变量赋值?  发帖心情 Post By:2017/9/7 11:59:00 [只看该作者]

我需要给多个变量赋值,现在的代码如下
Dim rep As String = IIF(e.GetValues("rep") = "",e.PostValues("rep"),e.GetValues("rep"))
Dim cou As String = IIF(e.GetValues("cou") = "",e.PostValues("cou"),e.GetValues("cou"))
Dim sta As String = IIF(e.GetValues("sta") = "",e.PostValues("sta"),e.GetValues("sta"))
Dim cit As String = IIF(e.GetValues("cit") = "",e.PostValues("cit"),e.GetValues("cit"))
Dim key As String = IIF(e.GetValues("key") = "",e.PostValues("key"),e.GetValues("key"))
Dim quoid As String = IIF(e.GetValues("quoid") = "",e.PostValues("quoid"),e.GetValues("quoid"))
Dim soid As String = IIF(e.GetValues("soid") = "",e.PostValues("soid"),e.GetValues("soid"))

像这样的变量以后可能还会增加,所以想通过数据结合For...Next语句来给这些变量赋值,我的想法如下

Dim gks() As String = {"rep","cou","sta","cit","key","quoid","soid"}
For i As Integer = 0 To gks.Length - 1
    gks(i) =  IIF(e.GetValues(gks(i)) = "",e.PostValues(gks(i)),e.GetValues(gks(i)))
Next

我知道这样值会赋给gks(0), gks(1)等数组成员。想请教我该如何才能实现把值赋给 rep, cou 等这些变量?

先行谢过!


 回到顶部
帅哥哟,离线,有人找我吗?
cbt
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:574 积分:4252 威望:0 精华:0 注册:2015/5/13 8:32:00
  发帖心情 Post By:2017/9/7 12:16:00 [只看该作者]

 可以尝试用字典来储存变量!访问 字典的键来取值!
参考:

Dim jzd As new Dictionary(of String,String)
Dim gks() As String = {"rep","cou","sta","cit","key","quoid","soid"}
For i As Integer = 0 To gks.Length - 1
    Dim sz As String =  IIF(1>2,"a",gks(i))
    If jzd.ContainsKey(gks(i))Then
        jzd(gks(i))=sz
    Else
        jzd.Add(gks(i),sz)
    End If
Next

output.show(jzd("rep"))

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/9/7 12:18:00 [只看该作者]

Dim gks() As String = {"rep","cou","sta","cit","key","quoid","soid"}
Dim dic As new Dictionary(Of String, String)
For i As Integer = 0 To gks.Length - 1
    If dic.ContainsKey(gks(i)) = False Then
        dic.Add(gks(i), 0)
    End If
    dic(gks(i)) =  IIF(e.GetValues(gks(i)) = "",e.PostValues(gks(i)),e.GetValues(gks(i)))
Next

 

需要引用,就改成 dic("cou") 等。


 回到顶部
帅哥哟,离线,有人找我吗?
guning008
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:婴狐 帖子:44 积分:600 威望:0 精华:0 注册:2016/10/1 11:15:00
谢谢老师!  发帖心情 Post By:2017/9/7 12:20:00 [只看该作者]

多谢甜老师指教!
[此贴子已经被作者于2017/10/22 23:38:00编辑过]

 回到顶部