Core Spark (Vb To C# Converter)

Vb To C# Converter

A GTP that converts VB to C# with code blocks that can be copied pasted into Visual Studio

Public Module Utility Public Function wpm_ApplyHTMLFormatting(ByVal strInput As String) As String strInput = "~" & strInput strInput = Replace(strInput, ",", "-") strInput = Replace(strInput, "'", """) strInput = Replace(strInput, """", """) strInput = Replace(strInput, "~", String.Empty) ' strInput = Replace(strInput, " " , "_") Return strInput End Function ' '******************************************************************************** Public Function wpm_GetDBString(ByVal dbObject As Object) As String Dim strEntry As String = String.Empty If Not (IsDBNull(dbObject) Or dbObject Is Nothing) Then strEntry = CStr(dbObject) If strEntry = " " Then strEntry = String.Empty End If Return strEntry.Trim End Function Public Function wpm_GetStringValue(ByVal myString As String) As String If (IsDBNull(myString) Or myString Is Nothing) Then myString = String.Empty End If Return myString End Function Public Function wpm_GetDBString(ByVal dbObject As Object, Byval DefaultValue As String) As String Dim strEntry As String = String.Empty If Not (IsDBNull(dbObject) Or dbObject Is Nothing) Then strEntry = CStr(dbObject) If strEntry = " " Then strEntry = DefaultValue End If Return strEntry.Trim End Function '******************************************************************************** Public Function wpm_GetDBDate(ByVal dbObject As Object) As DateTime If IsDBNull(dbObject) Then Return New DateTime ElseIf dbObject Is Nothing Then Return New DateTime ElseIf String.IsNullOrWhiteSpace(dbObject.ToString) Then Return New DateTime ElseIf wpm_IsDate(dbObject.ToString) Then Return CDate(dbObject) Else Return New DateTime End If End Function Public Function wpm_GetDBDouble(ByVal dbObject As Object) As Double If IsDBNull(dbObject) Then Return Nothing ElseIf dbObject Is Nothing Then Return Nothing Else If IsNumeric(dbObject) Then Return CDbl(dbObject) Else Return Nothing End If End If End Function Public Function wpm_GetDBInteger(ByVal dbObject As Object) As Integer If IsDBNull(dbObject) Then Return Nothing ElseIf dbObject Is Nothing Then Return Nothing Else If IsNumeric(dbObject) Then Return CInt(dbObject) Else Return Nothing End If End If End Function Public Function wpm_GetDBInteger(ByVal dbObject As Object, ByVal DefaultValue As Integer) As Integer If IsDBNull(dbObject) Then Return DefaultValue ElseIf dbObject Is Nothing Then Return DefaultValue Else If IsNumeric(dbObject) Then Return CInt(dbObject) Else Return DefaultValue End If End If End Function Public Function wpm_GetDBBoolean(ByVal dbObject As Object) As Boolean If IsDBNull(dbObject) Then Return False Else Return CBool(dbObject) End If End Function Public Function wpm_IsDate(ByVal strDate As String) As Boolean Dim dtDate As DateTime Dim bValid As Boolean = True Try dtDate = DateTime.Parse(strDate) Catch eFormatException As FormatException ' the Parse method failed => the string strDate cannot be converted to a date. bValid = False End Try Return bValid End Function End Module


Responses From Spark Variants that Implement the Core Spark (vb to C# Converter)

using System;

public static class Utility
{
    public static string ApplyHTMLFormatting(string strInput)
    {
        strInput = "~" + strInput;
        strInput = strInput.Replace(",", "-");
        strInput = strInput.Replace("'", """);
        strInput = strInput.Replace("\"", """);
        strInput = strInput.Replace("~", string.Empty);
        // strInput = strInput.Replace(" ", "_");
        return strInput;
    }

    public static string GetDBString(object dbObject)
    {
        string strEntry = string.Empty;
        if (dbObject != DBNull.Value && dbObject != null)
        {
            strEntry = Convert.ToString(dbObject);
            if (strEntry == " ") strEntry = string.Empty;
        }
        return strEntry.Trim();
    }

    public static string GetStringValue(string myString)
    {
        if (myString == null || myString == DBNull.Value)
        {
            myString = string.Empty;
        }
        return myString;
    }

    public static string GetDBString(object dbObject, string defaultValue)
    {
        string strEntry = string.Empty;
        if (dbObject != DBNull.Value && dbObject != null)
        {
            strEntry = Convert.ToString(dbObject);
            if (strEntry == " ") strEntry = defaultValue;
        }
        return strEntry.Trim();
    }

    public static DateTime GetDBDate(object dbObject)
    {
        if (dbObject == DBNull.Value || dbObject == null || string.IsNullOrWhiteSpace(dbObject.ToString()))
        {
            return new DateTime();
        }
        else if (IsDate(dbObject.ToString()))
        {
            return Convert.ToDateTime(dbObject);
        }
        else
        {
            return new DateTime();
        }
    }

    public static double GetDBDouble(object dbObject)
    {
        if (dbObject == DBNull.Value || dbObject == null)
        {
            return default;
        }
        else if (double.TryParse(dbObject.ToString(), out double result))
        {
            return result;
        }
        else
        {
            return default;
        }
    }

    public static int GetDBInteger(object dbObject)
    {
        if (dbObject == DBNull.Value || dbObject == null)
        {
            return default;
        }
        else if (int.TryParse(dbObject.ToString(), out int result))
        {
            return result;
        }
        else
        {
            return default;
        }
    }

    public static int GetDBInteger(object dbObject, int defaultValue)
    {
        if (dbObject == DBNull.Value || dbObject == null)
        {
            return defaultValue;
        }
        else if (int.TryParse(dbObject.ToString(), out int result))
        {
            return result;
        }
        else
        {
            return defaultValue;
        }
    }

    public static bool GetDBBoolean(object dbObject)
    {
        if (dbObject == DBNull.Value)
        {
            return false;
        }
        else
        {
            return Convert.ToBoolean(dbObject);
        }
    }

    public static bool IsDate(string strDate)
    {
        return DateTime.TryParse(strDate, out _);
    }
}

Explanation:

  1. Module to Static Class: The VB.NET Module is converted to a C# static class.
  2. Function to Method: VB.NET Function is converted to C# methods.
  3. Type Conversions: Used Convert.ToString, Convert.ToDateTime, Convert.ToBoolean for type conversions.
  4. Null and DBNull Checks: Used DBNull.Value and null checks appropriately.
  5. String Manipulations: Used string.Replace for string manipulations.
  6. TryParse Methods: Used double.TryParse and int.TryParse for parsing numeric values safely.
  7. DateTime Parsing: Used DateTime.TryParse for date validation.

This C# class should be compatible with .NET Core 8 and follows best practices for readability and maintainability.