简体   繁体   中英

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

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

 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

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. 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. The assignment SetPiece = False has no effect. VB allows assignment to the method name temporary variable, but the function doesn't return at that point. 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 ). In your sample, only the final assignment to SetPiece is relevant. The original flaw can be fixed by changing your 'If' to an 'If/Else'.

The actual C# equivalent, with the original flaw being a little more obvious is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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