简体   繁体   English

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

[英]how can i Translate this vb.net code to c# code?

how can i Translate this vb.net code to c# code? 我怎样才能将此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

i tried this code but i got an error 我尝试了此代码,但出现错误

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;
        }

the error is in SetPiece 错误在SetPiece中

i solve it guys here's the code: 我解决了,这是代码:

 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; } 

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);
}

Also while i'm at it, just go here ( CodeTranslator ) instead of asking translation questions. 另外,当我在这里时,只需到这里( CodeTranslator ),而不要问翻译问题。 Only ask when you have a problem you cant figure out 只问有问题时就无法解决

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);
}

Actually, your original VB code has a flaw that you may not be aware of. 实际上,您原始的VB代码有一个您可能不会意识到的缺陷。 The assignment SetPiece = False has no effect. 分配SetPiece = False无效。 VB allows assignment to the method name temporary variable, but the function doesn't return at that point. VB允许分配给方法名称临时变量,但该函数此时不返回。 If no actual 'Return' statements are encountered, then the value of the temporary SetPiece variable is returned when the function exits (when either End Function is encountered or the first Exit Function ). 如果没有遇到实际的“ Return”语句,则在函数退出时(遇到End Function或第一个Exit Function )返回临时SetPiece变量的值。 In your sample, only the final assignment to SetPiece is relevant. 在您的示例中,只有与SetPiece的最终分配相关。 The original flaw can be fixed by changing your 'If' to an 'If/Else'. 可以通过将“ If”更改为“ If / Else”来修复原始缺​​陷。

The actual C# equivalent, with the original flaw being a little more obvious is: 实际的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM