如何将模块和功能从VB.NET转换为C#

时间:2018-10-30 19:53:04

标签: c# vb.net

我是c#的新手,之前我是在VB.NET中编写代码的,但是我试图使用c#来重新开发程序并作为学习c#的一课。我尝试了很长时间,但仍然无法从VB.NET正确转换为c#,任何人都可以帮助我进行转换,因为我会更容易理解c#,谢谢!

这是我来自VB.NET的代码

Module GetStaffList

Dim Url As String
Dim CorpID As String
Dim Secret As String
Const ErrCode As String = """errcode"":0,""errmsg"":""ok"""

Function Token(CorpID As String, Secret As String) As String

    CorpID = "wwe1f80304633b3"
    Secret = "Ev7_oVNNbTpzkfcZ_QhX9l0VjZnAQ"

    Dim http = CreateObject("MSXML2.ServerXMLHTTP")
    Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" & CorpID & "&corpsecret=" & Secret
    http.Open("get", Url, False)
    http.send()

    If http.Status = 200 Then
        Token = http.responseText
    End If

    If InStr(Token, "access_token") > 1 Then
        Token = Split(Token, ",")(2)
        Token = Split(Token, ":")(1)
        Token = Replace(Token, """", "")
        MainPage.TxtToken.Text = Token
    Else
        Token = ""
    End If

End Function

下面是我尝试转换为c#但仍然很难做到的

namespace SC_System

{     MSG级     {         const string ErrCode =“ \” errcode \“:0,\” errmsg \“:\” ok \“”;

    public void Token(string CorpID, string Secret)
    {
        var http = CreateObject("MSXML2.ServerXMLHTTP");
        string Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + PDC.CorpID + "&corpsecret=" + PDC.Secret +"";
        HttpWebRequest GetUrl = (HttpWebRequest)WebRequest.Create(Url);
        HttpWebResponse ResponseUrl = (HttpWebResponse)GetUrl.GetResponse();
        if (ResponseUrl.StatusCode == HttpStatusCode.OK)
        {
            Console.WriteLine(ResponseUrl.StatusDescription);
            ResponseUrl.Close();
        }

    }

    private object CreateObject(string v)
    {
        throw new NotImplementedException();
    }
}

}

2 个答案:

答案 0 :(得分:-1)

在VB中,Function返回一些值。在您的代码中,它返回一个string

  • Function Token(CorpID As String, Secret As String) As String

    成为

    string Token(string CorpID, string Secret){
      // do something...and then
      return "some string value";
    }
    

    ,并且需要return一些字符​​串值。函数名称​​不会推断与返回值具有相同名称(Token)的变量

  • 一个Sub不返回任何内容,

    Sub Token(CorpID As String, Secret As String)

    在c#中成为void

    void Token(string CorpID, string Secret)

希望您能成功...

答案 1 :(得分:-1)

这将翻译原始VB的工作部分:

internal static class GetStaffList
{
    //this doesn't seem to be used right now
    internal const string ErrCode = "\"errcode\":0,\"errmsg\":\"ok\"";

    internal static void Token(string CorpID, string Secret)
    {
        CorpID = CorpID ?? "wwe1f80304633b3";
        Secret = Secret ?? "Ev7_oVNNbTpzkfcZ_QhX9l0VjZnAQ";

        string token;    
        using (var wc = new WebClient())
        {
            token = wc.DownloadString($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}");
        }
        if (token.Contains("access_token"))
        {
            token = token.Split(",")[2].Split(":")[1].Replace("\"", "");
            MainPage.TxtToken.Text = token;
        }
        else 
        {
            token = ""; 
        }
    }
}

但是实际上最好返回一个值,而更新UI,就像在VB方法中所暗示的那样:

internal static string Token(string CorpID, string Secret)
{
    CorpID = CorpID ?? "wwe1f80304633b3";
    Secret = Secret ?? "Ev7_oVNNbTpzkfcZ_QhX9l0VjZnAQ";

    string token;    
    using (var wc = new WebClient())
    {
        token = wc.DownloadString($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}");
    }
    if (token.Contains("access_token"))
    {
        return token.Split(",")[2].Split(":")[1].Replace("\"", "");
    }
    return "";
}

然后您可以这样称呼它:

string token = GetStaffList.Token(null, null);
if (!string.IsNullOrEmpty(token))
{
    MainPage.TxtToken.Text = token;
}

您的实用程序方法或类从不直接更新UI。

同样,最好这样编写VB:

Public Module GetStaffList

    Const ErrCode As String = """errcode"":0,""errmsg"":""ok"""

    Public Function Token(Optional CorpID As String = Nothing, Optional Secret As String = Nothing) As String

        CorpID = If(CorpID,"wwe1f80304633b3")
        Secret = If(Secret,"Ev7_oVNNbTpzkfcZ_QhX9l0VjZnAQ")

        Dim token As String
        Using wc As New WebClient()
            token = wc.DownlaodString(string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", CorpID, Secret))
        End Using

        If token.Contains("access_token") Then
            Return token.Split(",")(2).Split(":")(1).Replace("""", "")
        End If
        Return ""
    End Function
End Module

最后,您应该考虑使用actual JSON parser从下载结果中提取所需的令牌值。 Split()方法因此类问题而臭名昭著,实际上比专用解析器要

相关问题