C# 如何转换模块&;从VB.NET到c的函数#

C# 如何转换模块&;从VB.NET到c的函数#,c#,vb.net,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,""

我是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
{ 类味精 { 常量字符串ErrCode=“\”ErrCode\”:0,\“errmsg\”:\“ok\”


}

在VB中,
函数
返回一些值。在代码中,它返回一个
字符串

  • 函数令牌(CorpID作为字符串,Secret作为字符串)作为字符串

    变成

    string Token(string CorpID, string Secret){
      // do something...and then
      return "some string value";
    }
    
    并且需要
    返回
    一些字符串值。函数名不会推断出与返回值同名(
    标记
    )的变量

  • Sub
    不会返回任何内容,因此:

    子令牌(CorpID作为字符串,Secret作为字符串)

    在c中变为
    void
    #

    void令牌(字符串CorpID,字符串Secret)


希望您能继续……

这将转换原始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:

string token = GetStaffList.Token(null, null);
if (!string.IsNullOrEmpty(token))
{
    MainPage.TxtToken.Text = token;
}
公共模块getStatffList
常量ErrCode As String=“”ErrCode”“:0”,“errmsg”“:”确定“”
作为字符串的公共函数标记(可选CorpID作为字符串=Nothing,可选Secret作为字符串=Nothing)
CorpID=If(CorpID,“wwe1f80304633b3”)
机密=如果(机密,“Ev7_oVNNbTpzkfcZ_QhX9l0VjZnAQ”)
作为字符串的Dim标记
将wc用作新的WebClient()
token=wc.DownlaodString(string.Format(“https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&CorbitCret={1},软毛,秘密)
终端使用
如果token.Contains(“access_token”),则
返回标记。拆分(“,”)(2)。拆分(“:”)(1)。替换(“,”)
如果结束
返回“”
端函数
端模块

最后,您应该考虑使用AN从下载结果中提取您需要的令牌值。<代码> SPLIT()/代码>方法对于这种类型的GOTCHAS是臭名昭著的,并且实际上比专用的解析器慢。甚至在VB.Net中,更不用说C了。@JoelCoehoorn,你是说要创建COM对象,就应该使用System.Activator?还是其他什么?@MacroMarc当然“曾经”是夸张的。更重要的是,找到既不是完全过时又不是冗余的COM对象(如本例所示)变得非常罕见或者至少已经有了托管包装器(你可以说这也是)。如果你仍在构建自己的新包装器…请停下来。难做不是问题…我们帮助解决特定问题,我们不是代码翻译服务。请更新你的帖子,说明你的困境。此外,你正在混合

vb
vb6
。。。