locked
help to convert from VB to C# RRS feed

  • Question

  • Hi All.

    How to convert from VB to C#

    Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As System.Windows.Controls.ValidationResult
    If _ObjectType = "Date" Then

    If Not IsDate(value) Then
    Return New ValidationResult(False, "A value is required")
    End If
    End If

    Return New ValidationResult(True, Nothing)
    End Function

    I tried like

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
    if (_ObjectType == "Date")
    {
    if (!IsDate(value))
    { return new ValidationResult(false, "A value is required"); }
    }
    }

    But unfortunately it is not correct. Problem in second IF statement condition description.

    Thanks.




    • Edited by zleug Wednesday, August 12, 2020 2:38 AM
    Wednesday, August 12, 2020 2:07 AM

Answers

  • A basic example is shown in Microsoft documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.validationrule?view=netcore-3.1

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Controls;
    
    namespace WpfApp1.Classes
    {
        public class AgeRangeRule : ValidationRule
        {
            public int Min { get; set; }
            public int Max { get; set; }
    
            public AgeRangeRule()
            {
            }
    
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
                int age = 0;
    
                try
                {
                    if (((string)value).Length > 0)
                        age = Int32.Parse((String)value);
                }
                catch (Exception e)
                {
                    return new ValidationResult(false, $"Illegal characters or {e.Message}");
                }
    
                if ((age < Min) || (age > Max))
                {
                    return new ValidationResult(false,
                        $"Please enter an age in the range: {Min}-{Max}.");
                }
                return ValidationResult.ValidResult;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Wednesday, August 12, 2020 4:17 AM
    • Marked as answer by zleug Thursday, August 20, 2020 12:06 PM
    Wednesday, August 12, 2020 3:03 AM

All replies

  • A basic example is shown in Microsoft documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.validationrule?view=netcore-3.1

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Controls;
    
    namespace WpfApp1.Classes
    {
        public class AgeRangeRule : ValidationRule
        {
            public int Min { get; set; }
            public int Max { get; set; }
    
            public AgeRangeRule()
            {
            }
    
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
                int age = 0;
    
                try
                {
                    if (((string)value).Length > 0)
                        age = Int32.Parse((String)value);
                }
                catch (Exception e)
                {
                    return new ValidationResult(false, $"Illegal characters or {e.Message}");
                }
    
                if ((age < Min) || (age > Max))
                {
                    return new ValidationResult(false,
                        $"Please enter an age in the range: {Min}-{Max}.");
                }
                return ValidationResult.ValidResult;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Wednesday, August 12, 2020 4:17 AM
    • Marked as answer by zleug Thursday, August 20, 2020 12:06 PM
    Wednesday, August 12, 2020 3:03 AM
  • I have problem to convert only second IF statement. Can help to convert or show correct code in C# if value is not date?

    Thanks.



    • Edited by zleug Wednesday, August 12, 2020 3:42 AM
    Wednesday, August 12, 2020 3:38 AM
  • You can use automatic services for VB.NET to C# conversion. For example, https://converter.telerik.com/ produces the following result for your code:

    public override System.Windows.Controls.ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        if (_ObjectType == "Date")
        {
            if (!Information.IsDate(value))
                return new ValidationResult(false, "A value is required");
        }
    
        return new ValidationResult(true, null);
    }
    

    Another good tool is https://github.com/icsharpcode/CodeConverter, which can be used from Visual Studio (via extension)

    Wednesday, August 12, 2020 4:19 AM
  • So my own idea was to use DateTime.TryParse method and then I did a Google search and found old ASP.NET forums post by S G Wellens (I believe I remember him from my days of hanging in that forum) (I copy that answer verbatim, but it can be slightly adjusted)

    protected bool CheckDate(String date)
        {
            DateTime Temp;
    
    
            if (DateTime.TryParse(date, out Temp) == true)
                return true;
            else
                return false;
        }


    Looking for new opportunities

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi N Wednesday, August 12, 2020 4:24 AM
    Wednesday, August 12, 2020 4:24 AM
  • Hi zleug,

    Thank you for posting here.

    IsDate is a method in Microsoft.VisualBasic.Core.dll, we can use it directly in c#, just use Vitaliy Shibaev's code, and then add a reference to Microsoft.VisualBasic.dll.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 12, 2020 5:48 AM
  • You have two choices:

    1. As mentioned by Vitaliy S. and Timon Yang, you can just use the original (extremely well-tested) VB function via the Microsoft.VisualBasic assembly.

    2. As mentioned by Naomi N., you can create a simple 'IsDate' (or 'CheckDate') function yourself. Remember to check for null though:

    	public static bool IsDate(object expression)
    	{
    		if (expression == null)
    			return false;
    
    		DateTime testDate;
    		return DateTime.TryParse(expression.ToString(), out testDate);
    	}



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


    • Edited by Dave Doknjas Wednesday, August 12, 2020 1:34 PM
    • Proposed as answer by Naomi N Wednesday, August 12, 2020 2:36 PM
    Wednesday, August 12, 2020 1:32 PM