Left, Right, Mid methods for C#

This snippet let’s you use the Left, Right, Mid methods known from VisualBasic in C# without referencing Microsoft.VisualBasic Namespace.

public static string Left(string input, int index)
{
	return input.Substring(0, index);
}
public static string Right(string input, int index)
{
	return input.Substring(input.Length - index, index);
}

public static string Mid(string input, int startingIndex, int endingIndex)
{
	return input.Substring(startingIndex, endingIndex);
}

public static string Mid(string input, int startingIndex)
{
	return input.Substring(startingIndex);
}

How to use the HTML5 datalist tag

HTML5 Datalist Element

Description

HTML5 introduced the new <datalist> tag, which specifies a list of predefined <input> element options. It is used to provide the autocomplete feature on <input> elements. Users will see a drop-down list of pre-defined options as they input data. This element has no other attributes than the global attributes, common to all elements. The id of the datalist element must match with the list attribute of the input box.Keep in mind that Safari doesn’t support datalist yet. If you are using this element with an unsupported (older) browser version, the most browsers will simply ignores the datalist tag and render rest of the webpage. Internet Explorer 9 and below is a little bit special, it will simply print the option values to the page (a possible workaround can be found at http://viralpatel.net/).

Usage

Here you can see a Basic example on how to use the datalist tag in HTML5.

Live Sample

Keep in mind that this Live-Example only works on supported Browsers.



JSFiddle’s

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Compatibility:
working Chrome
working Internet Explorer
not tested Firefox
not working Opera
not working Safari

How to deep copy a IList in C# and VB.NET

To deep copy a IList in C# and VB.NET you can use the snippet.

Sample C#

public static IList<T> Clone<T>(IList<T> listToClone) where T : ICloneable
{
	return listToClone.Select(item => (T)item.Clone()).ToList();
}

Sample VB.NET

Public Shared Function Clone(Of T As ICloneable)(listToClone As IList(Of T)) As IList(Of T)
	Return listToClone.[Select](Function(item) DirectCast(item.Clone(), T)).ToList()
End Function

How to enumerate an Enum in C# and VB.NET

To enumerate an Enum in C# and VB.NET you can use the snippet below.

Sample C#

public enum TestEnum
{
	These = 0,
	Are = 1,
	Sample = 2,
	Enums = 3,
	Duplicates = 3,
	Included = 4
}

static void Main(string[] args)
{
	foreach (var testEnum in Enum.GetNames(typeof(TestEnum)))
	{
		System.Console.WriteLine(testEnum);
	}
	Console.Read();
}

Sample VB.NET

Public Enum TestEnum
	These = 0
	Are = 1
	Sample = 2
	Enums = 3
	Duplicates = 3
	Included = 4
End Enum

Private Shared Sub Main(args As String())
	For Each testEnum As var In [Enum].GetNames(GetType(TestEnum))
		System.Console.WriteLine(testEnum)
	Next
	Console.Read()
End Sub

What is the difference between mutable and immutable in C# and VB.NET

What is the difference between a mutable and immutable string in C# and VB.NET?

  • A mutable string can be changed.
  • An immutable string cannot be changed.
  • Mutable is the english word for can change. Immutable is the english word for cannot change.

    if you alter a String, you are creating a new string object internally. So String is immutable. If you alter a StringBuilder no new Objects are created internally so, StringBuilder is mutable.

    How to get Hours Difference of 2 Dates in C# and VB.NET

    To get Hours Difference of 2 Dates in C# and VB.NET you can use the following snippet.

    Sample C#

    /// <summary>
    /// Returns the Hour Difference of two Datetimes as Double Value.
    /// </summary>
    /// <param name="dateOne">The original date.</param>
    /// <param name="dateTwo">The Datei to be Checked against the Original Date.</param>
    /// <returns></returns>
    public static Double DifferenceInHours(this DateTime dateOne, DateTime dateTwo)
    {
    	return (dateOne - dateTwo).TotalHours;
    }
    

    Sample VB.NET

    ''' <summary>
    ''' Returns the Hour Difference of two Datetimes as Double Value.
    ''' </summary>
    ''' <param name="dateOne">The original date.</param>
    ''' <param name="dateTwo">The Datei to be Checked against the Original Date.</param>
    ''' <returns></returns>
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function DifferenceInHours(dateOne As DateTime, dateTwo As DateTime) As [Double]
    	Return (dateOne - dateTwo).TotalHours
    End Function
    

    As always, this extension method has been added to the Github Fesslersoft.Extensions Repository

    How to make a multicolumn listbox in C# and VB.NET

    To make a multicolumn listbox in C# and VB.NET you can use the following snippet.
    First you need to add the Columns to the Listview.

    multicolumnslistviewcsharp_add

    Adding columns to the Listview

    Sample C#

    listView1.Items.Add("Column1Text").SubItems.AddRange(new string[] { "col1;row1", "col2;row1", "col3;row1" });
    listView1.Items.Add("Column2Text").SubItems.AddRange(new string[] { "col1;row2", "col2;row2", "col3;row2" });
    listView1.Items.Add("Column3Text").SubItems.AddRange(new string[] { "col1;row3", "col2;row3", "col3;row3" });
    

    Sample VB.NET

    listView1.Items.Add("Column1Text").SubItems.AddRange(New String() {"col1;row1", "col2;row1", "col3;row1"})
    listView1.Items.Add("Column2Text").SubItems.AddRange(New String() {"col1;row2", "col2;row2", "col3;row2"})
    listView1.Items.Add("Column3Text").SubItems.AddRange(New String() {"col1;row3", "col2;row3", "col3;row3"})
    
    multicolumnslistviewcsharp

    Result: Listview with Multiple Columns

    How to load project references from a sub directory in C# and VB.NET

    To load project references from a sub directory in C# and VB.NET you simply need to add the element to your app.config file.

  • assemblyBinding: Contains information about assembly version redirection and the locations of assemblies.
  • configuration: The root element in every configuration file used by the common language runtime and .NET Framework applications.
  • runtime: Contains information about assembly binding and garbage collection.
  • Sample XML

    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="bin;bin2\subbin;bin3"/>
          </assemblyBinding>
       </runtime>
    </configuration>
    

    Top 10 reads of February 2015

    These are the Top 10 reads of February 2015.

  • How to show or hide a variable if null in AngularJS
  • How to remove an element using Javascript
  • How to replace the inner HTML of a div using jQuery
  • How to reload or rerender the page using AngularJS
  • How to check if an array contains a value in Javascript
  • How to define global Variables in AngularJs
  • How to set the HTTP Header for a request using AngularJs
  • How to resize a image proportionally with CSS
  • How to join two lists in C# and VB.NET
  • How to clear notifications in IOS using Objective-C
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    How to reload a ViewController in IOS using Objective-C

    To reload a UITableViewController in IOS using Objective-C you can simply use the reloadData method of the ViewController.

    //MyTablViewController is a UITableViewController
    [MyTableViewController.tableView reloadData]