我怎样才能将此vb.net代码转换为C#代码?

时间:2018-06-20 06:51:32

标签: c# vb.net vb.net-to-c#

我如何将vb.net代码转换为C#代码?

 Public Function SetPiece(ByVal strGlobal As String, ByVal strNodes As String, ByVal strCode As String, ByVal intPiece As Integer, ByVal strNewVal As String) As Boolean
        Initialize()
        If strGlobal = "" Or strNodes = "" Or strCode = "" Then SetPiece = False
        SetPiece = mobjUtility.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal)
    End Function

我尝试了此代码,但出现错误

public bool SetPiece(string strGlobal, string strNodes, string strCode, int intPiece,string strNewVal )
        {
            bool setPiece = true;
            if (strGlobal == "" || strCode == "" )
            {
                setPiece = false;
                setPiece = mobjUtility.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal);
            }
            else if (strNodes == "")
            {
                setPiece = false;
            }
            return setPiece;
        }

错误在于SetPiece

我解决了,这是代码:

public static bool SetPiece(string strGlobal, string strNodes, string strCode, int intPiece, string strNewVal)
        {
            
            Initialize();
            if (((strGlobal == "")
                        || ((strNodes == "")
                        || (strCode == ""))))
            {
                return false;
            }

            var obj = mCache.Static("AGSP.UTILS");
            string test = obj.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal);
            return obj;
        }

3 个答案:

答案 0 :(得分:4)

Wallah

public bool SetPiece(string strGlobal, string strNodes, string strCode, int intPiece, string strNewVal)
{
      Initialize();

      if (strGlobal == "" || strNodes == "" ||strCode == "")
         return false;       

      return mobjUtility.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal);
}

在我忙的时候,也只是去这里(CodeTranslator),而不是问翻译问题。只问有什么问题无法解决

答案 1 :(得分:0)

public bool SetPiece(string strGlobal, string strNodes, string strCode, int intPiece, string strNewVal)
{
    Initialize();
    if (strGlobal == "" | strNodes == "" | strCode == "")
        SetPiece = false;
    SetPiece = mobjUtility.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal);
}

答案 2 :(得分:0)

实际上,您原始的VB代码有一个您可能不会意识到的缺陷。 分配SetPiece = False无效。 VB允许分配给方法名称临时变量,但该函数此时不返回。如果没有遇到实际的“ Return”语句,则在函数退出时(遇到End Function或第一个Exit Function时)将返回临时SetPiece变量的值。在您的示例中,只有与SetPiece的最终分配相关。可以通过将“ If”更改为“ If / Else”来修复原始缺​​陷。

等效的实际C#,更明显的是原始缺陷:

public bool SetPiece(string strGlobal, string strNodes, string strCode, int intPiece, string strNewVal)
{
    bool tempSetPiece = false;
    Initialize();
    if (string.IsNullOrEmpty(strGlobal) || string.IsNullOrEmpty(strNodes) || string.IsNullOrEmpty(strCode))
        tempSetPiece = false; //no effect

    return mobjUtility.SetPiece(strGlobal, strNodes, strCode, intPiece, strNewVal);
}