none
Can someone convert this VB code to c sharp. RRS feed

  • Question

  •  dbTaskOutput = Replace(dbTaskOutput, vbCrLf, "")

            If dbTaskOutput <> Nothing Then
                errorCount = 0

                ' Iterate through the set of defined keywords, and search the output string for each
                For Each KeywordRow In KeywordMessages.Tables(0).Rows

                    bValidate = False

                    ' Check if the keyword is defined for use with a specific task_ID
                    If IsDBNull(KeywordRow("Task_ID")) Then
                        bValidate = True
                    Else
                        If (CInt(KeywordRow("Task_ID")) = TaskID) Then
                            bValidate = True
                        End If
                    End If

                    If bValidate Then

                        'Apply word search or string search as defined for this keyword
                        If CBool(KeywordRow("FindAsWord")) = True Then
                            MatchCount = CountInWord(dbTaskOutput, CStr(KeywordRow("Keyword")))
                        Else
                            MatchCount = CountInstr(dbTaskOutput, CStr(KeywordRow("Keyword")))
                        End If

                        'Apply logic (If found & not required) OR (If not found and required)
                        If (MatchCount <> 0 And CBool(KeywordRow("Required")) = False) Or (MatchCount = 0 And CBool(KeywordRow("Required")) = True) Then
                            If MatchCount = 0 Then
                                MatchCount = 1 ' this is to detect 'not found' but required matches
                            End If
                            'Increment or decrement errorCount with the number of matches, depending on the 'Increment' flag
                            'Decrement is used to cancel keyword matches that also match an 'innocent' string
                            errorCount = errorCount + (MatchCount * CInt(KeywordRow("Increment")))
                        End If
                    End If

                Next

                Select Case errorCount
                    Case 0
                        stepResult = "Success"
                    Case 1
                        stepResult = "Failed, " & errorCount & " error"
                    Case Else
                        stepResult = "Failed, " & errorCount & " errors"
                End Select

            Else    'The dbTaskOutput string was empty, so flag this as a failure
                stepResult = "Failed, no output found"
            End If

            Dts.Variables("User::gvTaskResult").Value = stepResult

            If stepResult <> "Success" Then
                Dts.Variables("User::gvJobResult").Value = "Failed"
            End If

            Dts.TaskResult = ScriptResults.Success

        End Sub
    Thursday, May 3, 2018 1:55 PM

All replies

  • The best thing to do is run the code through a VB.NET to C# converter, usually the code will need modifications no matter how good it is.

    So I ran some of your code through a converter and tweaked it but still needs work as some of the variables you have in there I don't know what they are.

    A running start and note one of the simply tweaks was 

    KeywordRow("Keyword")

    To

    KeywordRow["Keyword"]

    foreach (DataRow KeywordRow in KeywordMessages.Tables[0].Rows)
    {
        bValidate = false;
    
        // Check if the keyword is defined for use with a specific task_ID
        if (Convert.IsDBNull(KeywordRow["Task_ID"]))
        {
            bValidate = true;
        }
        else
        {
            if (Convert.ToInt32(KeywordRow["Task_ID"]) == TaskID)
            {
                bValidate = true;
            }
        }
    
        if (bValidate)
        {
    
            //Apply word search or string search as defined for this keyword
            if (Convert.ToBoolean(KeywordRow["FindAsWord"]) == true)
            {
                MatchCount = CountInWord(dbTaskOutput, Convert.ToString(KeywordRow["Keyword"]));
            }
            else
            {
                MatchCount = CountInstr(dbTaskOutput, Convert.ToString(KeywordRow["Keyword"]));
            }
    
            //Apply logic (If found & not required) OR (If not found and required)
            if ((MatchCount != 0 && Convert.ToBoolean(KeywordRow["Required"]) == false) || (MatchCount == 0 && Convert.ToBoolean(KeywordRow["Required"]) == true))
            {
                if (MatchCount == 0)
                {
                    MatchCount = 1; // this is to detect 'not found' but required matches
                }
                //Increment or decrement errorCount with the number of matches, depending on the 'Increment' flag
                //Decrement is used to cancel keyword matches that also match an 'innocent' string
                errorCount = errorCount + (MatchCount * Convert.ToInt32(KeywordRow["Increment"]));
            }
        }
    }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, May 3, 2018 2:14 PM
    Moderator
  • Now why would you want to do that? ;-)

    Did you try an online code conversion tool?

    http://www.carlosag.net/tools/codetranslator/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 3, 2018 2:16 PM
  • When you do run this through a converter, be aware that a converter only knows what you've input.

    Karen pointed out that "KeywordRow" was probably an indexer and she's probably right, but unless you include the declaration of KeywordRow and KeywordMessages in the code being converted, then most converters will not make that assumption.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, May 3, 2018 2:48 PM