试图将VB.Net转换为C#ViewState问题

时间:2014-12-16 10:27:42

标签: c# vb.net

VB.NET代码

If ViewState("lstSetting") IsNot Nothing Then
  username = ViewState("lstSetting")("Email")
  password = ViewState("lstSetting")("Psw")
  mHost = ViewState("lstSetting")("Host").ToString.Split(":")(1)
  mPort = ViewState("lstSetting")("Port").ToString.Split(":")(1)
End If
password = Trim(dec(ondecrypt(Trim(password))))

C#代码

if (ViewState["lstSetting"] != null)
{
  username = ViewState["lstSetting"]("Email");
  password = ViewState["lstSetting"]("Psw");
  mHost = ViewState["lstSetting"]("Host");
  //.ToString.Split(":")(1)
  mPort = ViewState["lstSetting"]("Port");
  //.ToString.Split(":")(1)
}
password=Strings.Trim(ModCommon.dec(ModCommon.ondecrypt(Strings.Trim(password))));`
  

错误:预期方法名称......

此处显示错误

  

ViewState["lstSetting"]("Email")

2 个答案:

答案 0 :(得分:2)

ViewState["lstSetting"]("Email")

应该是

((Dictionary<string, string>)ViewState["lstSetting"])["Email"]

实际上,您应该检索ViewState["lstSetting"] 一次,然后再使用它。但这适用于两个代码片段。

答案 1 :(得分:1)

由于lstSettingDictionary<string, string>,您需要先修改变量,然后使用Item访问者[]获取变量:

Dictionary<string, string> dic = ViewState["lstSetting"] as Dictionary<string, string>;

if (dic != null)
{
    username = dic["Email"];

    // etc
}