• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C# Equivalent to VB's Mid Statement

Instant C# converts calls to the legacy VB Mid statement (unrelated to the Mid function) via the following helper class inserted into the conversion output:

//----------------------------------------------------------------------------------------
// Copyright © 2003 - 2017 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// The methods in this class replicate the behavior of miscellaneous VB features.
//----------------------------------------------------------------------------------------
using System;

public static class ConversionHelper
{
    public static void MidStatement(ref string target, int oneBasedStart, char insert)
    {
        //These 'MidStatement' method overloads replicate the behavior of the VB 'Mid' statement (which is unrelated to the VB 'Mid' function)

        if (target == null)
            return;

        target = target.Remove(oneBasedStart - 1, 1).Insert(oneBasedStart - 1, insert.ToString());
    }

    public static void MidStatement(ref string target, int oneBasedStart, string insert)
    {
        //These 'MidStatement' method overloads replicate the behavior of the VB 'Mid' statement (which is unrelated to the VB 'Mid' function)

        if (target == null || insert == null)
            return;

        target = target.PadRight(target.Length + insert.Length).Remove(oneBasedStart - 1, insert.Length).Insert(oneBasedStart - 1, insert).Substring(0, target.Length);
    }

    public static void MidStatement(ref string target, int oneBasedStart, string insert, int length)
    {
        //These 'MidStatement' method overloads replicate the behavior of the VB 'Mid' statement (which is unrelated to the VB 'Mid' function)

        if (target == null || insert == null)
            return;

        int minLength = Math.Min(insert.Length, length);
        target = target.PadRight(target.Length + insert.Length).Remove(oneBasedStart - 1, minLength).Insert(oneBasedStart - 1, insert.Substring(0, minLength)).Substring(0, target.Length);
    }

    //... more helper methods
}


Use Instant C# to convert from VB.NET to C#.     

Additional resource: VB.NET and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.