How to match strings using wildcards in C# and VB.NET

To match strings using wildcards in C# and VB.NET you can use the following snippet.
It will internally convert the wildcard string to a Regex.

The Console-Output of this sample will be:

C:\Test\myFile01.xml
C:\Test\myFile02.xml

Sample C#

#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

#endregion

namespace de.fesslersoft.RegexWildCard
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var fileList = new List<String>();
            fileList.Add(@"C:\Test\myFile01.xml");
            fileList.Add(@"C:\Test\myFile02.xml");
            fileList.Add(@"C:\Test\myFolder01.xml");
            fileList.Add(@"C:\Test\myFolder02.xml");

            var wildCard = new Wildcard("*File*.xml", RegexOptions.IgnoreCase);
            foreach (var file in fileList.Where(file => wildCard.IsMatch(file)))
            {
                Console.WriteLine(file);
            }
            Console.Read();
        }
    }


    public class Wildcard : Regex
    {
        public Wildcard(string pattern) : base(ToRegex(pattern))
        {
        }

        public Wildcard(string pattern, RegexOptions options) : base(ToRegex(pattern), options)
        {
        }

        public static string ToRegex(string pattern)
        {
            return "^" + Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".") + "$";
        }
    }
}

Sample VB.NET (autoconverted)

#Region ""

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions

#End Region

Namespace de.fesslersoft.RegexWildCard
	Friend Class Program
		Private Shared Sub Main(args As String())
			Dim fileList = New List(Of [String])()
			fileList.Add("C:\Test\myFile01.xml")
			fileList.Add("C:\Test\myFile02.xml")
			fileList.Add("C:\Test\myFolder01.xml")
			fileList.Add("C:\Test\myFolder02.xml")

			Dim wildCard = New Wildcard("*File*.xml", RegexOptions.IgnoreCase)
			For Each file As var In fileList.Where(Function(file) wildCard.IsMatch(file))
				Console.WriteLine(file)
			Next
			Console.Read()
		End Sub
	End Class


	Public Class Wildcard
		Inherits Regex
		Public Sub New(pattern As String)
			MyBase.New(ToRegex(pattern))
		End Sub

		Public Sub New(pattern As String, options As RegexOptions)
			MyBase.New(ToRegex(pattern), options)
		End Sub

		Public Shared Function ToRegex(pattern As String) As String
			Return "^" + Escape(pattern).Replace("\*", ".*").Replace("\?", ".") + "$"
		End Function
	End Class
End Namespace

Please rate this snippet

How to map Objects with Constructor parameters using Automapper

To map Objects that need Constructor Parameters using Automapper you need to use the ConstructUsing Method while Creating the Map.
See the Sample Console Applications below.

Sample Console Application C#

using System;
using AutoMapper;

namespace de.Fesslersoft.AutomapperConstructorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Mapper.CreateMap<Item, Product>()
                .ConstructUsing(x => new Product(x.Id,x.Price)) //without this line there will be an System.Argumentexception
                .ForMember(destination => destination.Number, x => x.MapFrom(source => source.Id))
                .ForMember(destination => destination.Price, x => x.MapFrom(source => source.Price));

            var item = new Item {Id = "Testitem", Price = new decimal(145.69)};
            var product = Mapper.Map<Item, Product>(item);

            Console.WriteLine(product.Number); //prints Testitem
            Console.WriteLine(product.Price); //prints 145.69

            Console.Read();
        }

        internal class Item
        {
            public string Id { get; set; }
            public decimal Price { get; set; }
        }

        internal class Product
        {
            public string Number { get; set; }
            public decimal Price { get; set; }

            public Product(string number, decimal price)
            {
                Number = number;
                Price = price;
            }
        }
    }
}

Sample Console Application VB.NET (autoconverted code)

Imports AutoMapper

Namespace de.Fesslersoft.AutomapperConstructorTest
	Class Program
		Private Shared Sub Main(args As String())
			'without this line there will be an System.Argumentexception
			Mapper.CreateMap(Of Item, Product)().ConstructUsing(Function(x) New Product(x.Id, x.Price)).ForMember(Function(destination) destination.Number, Function(x) x.MapFrom(Function(source) source.Id)).ForMember(Function(destination) destination.Price, Function(x) x.MapFrom(Function(source) source.Price))

			Dim item = New Item() With { _
				Key .Id = "Testitem", _
				Key .Price = New Decimal(145.69) _
			}
			Dim product = Mapper.Map(Of Item, Product)(item)

			Console.WriteLine(product.Number)
			'prints Testitem
			Console.WriteLine(product.Price)
			'prints 145.69
			Console.Read()
		End Sub

		Friend Class Item
			Public Property Id() As String
				Get
					Return m_Id
				End Get
				Set
					m_Id = Value
				End Set
			End Property
			Private m_Id As String
			Public Property Price() As Decimal
				Get
					Return m_Price
				End Get
				Set
					m_Price = Value
				End Set
			End Property
			Private m_Price As Decimal
		End Class

		Friend Class Product
			Public Property Number() As String
				Get
					Return m_Number
				End Get
				Set
					m_Number = Value
				End Set
			End Property
			Private m_Number As String
			Public Property Price() As Decimal
				Get
					Return m_Price
				End Get
				Set
					m_Price = Value
				End Set
			End Property
			Private m_Price As Decimal

			Public Sub New(number__1 As String, price__2 As Decimal)
				Number = number__1
				Price = price__2
			End Sub
		End Class
	End Class
End Namespace

Please rate this snippet

What is the best datatype for money in C# and VB.NET

The best Datatype for money in C# and VB.NET is Decimal.
What is the MSDN saying about Decimal.

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.

MSDN SOURCE

Please rate this snippet