以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  能不能用For...Next给多个变量赋值?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=106363)

--  作者:guning008
--  发布时间:2017/9/7 11:59:00
--  能不能用For...Next给多个变量赋值?
我需要给多个变量赋值,现在的代码如下
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
--  发布时间: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"))

--  作者:有点甜
--  发布时间: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
--  发布时间:2017/9/7 12:20:00
--  谢谢老师!
多谢甜老师指教!
[此贴子已经被作者于2017/10/22 23:38:00编辑过]