locked
Convert Val() of vb in c# RRS feed

  • Question

  • hello to all
    i want to convert some vb 6.0 code to c#

    VB 6.0 Code is
      If val(.TextMatrix(RowIndex, .Cols - 1)) = 1 Then
     
    'dosomething
    End If

    What is in C#

    ........................

    kshama

    Monday, August 20, 2012 3:02 PM

Answers

  • There is no simple equivalent in C# - we use the following helper class, which you're free to use:

    //----------------------------------------------------------------------------------------
    //	Copyright © 2003 - 2012 Tangible Software Solutions Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class simulates the behavior of the classic VB 'Val' function.
    //----------------------------------------------------------------------------------------
    public static class SimulateVal
    {
    	public static double Val(string expression)
    	{
    		if (expression == null)
    			return 0;
    
    		//try the entire string, then progressively smaller
    		//substrings to simulate the behavior of VB's 'Val',
    		//which ignores trailing characters after a recognizable value:
    		for (int size = expression.Length; size > 0; size--)
    		{
    			double testDouble;
    			if (double.TryParse(expression.Substring(0, size), out testDouble))
    				return testDouble;
    		}
    
    		//no value is recognized, so return 0:
    		return 0;
    	}
    	public static double Val(object expression)
    	{
    		if (expression == null)
    			return 0;
    
    		double testDouble;
    		if (double.TryParse(expression.ToString(), out testDouble))
    			return testDouble;
    
    		//VB's 'Val' function returns -1 for 'true':
    		bool testBool;
    		if (bool.TryParse(expression.ToString(), out testBool))
    			return testBool ? -1 : 0;
    
    		//VB's 'Val' function returns the day of the month for dates:
    		System.DateTime testDate;
    		if (System.DateTime.TryParse(expression.ToString(), out testDate))
    			return testDate.Day;
    
    		//no value is recognized, so return 0:
    		return 0;
    	}
    	public static int Val(char expression)
    	{
    		int testInt;
    		if (int.TryParse(expression.ToString(), out testInt))
    			return testInt;
    		else
    			return 0;
    	}
    }


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

    • Proposed as answer by Norkk Monday, August 20, 2012 3:21 PM
    • Marked as answer by Jason Dot Wang Tuesday, August 28, 2012 8:21 AM
    Monday, August 20, 2012 3:18 PM

All replies

  • As a general rule, rather than asking, "what is the equivalent of this function in language X in language Y?" it's often better to just ask, "How do I do [what I really need to do] in language Y?"

    There is a function that's more or less designed to be an exact port, but I would discourage it's use.  It would be better to use a more C# approach to this problem in the first place.

    You should use either int.Parse or int.TryParse (use TryParse if you're not sure if the value will be a valid number or not).

    If the type of the variable is "object" and not "string" you'll need to either cast it to a number if it is a number, cast it to a string if it is a string, or if you really want, call "ToString" on it (if you're sure it'll never be 'null').


    • Edited by servy42 Monday, August 20, 2012 3:16 PM
    Monday, August 20, 2012 3:16 PM
  • There is no simple equivalent in C# - we use the following helper class, which you're free to use:

    //----------------------------------------------------------------------------------------
    //	Copyright © 2003 - 2012 Tangible Software Solutions Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class simulates the behavior of the classic VB 'Val' function.
    //----------------------------------------------------------------------------------------
    public static class SimulateVal
    {
    	public static double Val(string expression)
    	{
    		if (expression == null)
    			return 0;
    
    		//try the entire string, then progressively smaller
    		//substrings to simulate the behavior of VB's 'Val',
    		//which ignores trailing characters after a recognizable value:
    		for (int size = expression.Length; size > 0; size--)
    		{
    			double testDouble;
    			if (double.TryParse(expression.Substring(0, size), out testDouble))
    				return testDouble;
    		}
    
    		//no value is recognized, so return 0:
    		return 0;
    	}
    	public static double Val(object expression)
    	{
    		if (expression == null)
    			return 0;
    
    		double testDouble;
    		if (double.TryParse(expression.ToString(), out testDouble))
    			return testDouble;
    
    		//VB's 'Val' function returns -1 for 'true':
    		bool testBool;
    		if (bool.TryParse(expression.ToString(), out testBool))
    			return testBool ? -1 : 0;
    
    		//VB's 'Val' function returns the day of the month for dates:
    		System.DateTime testDate;
    		if (System.DateTime.TryParse(expression.ToString(), out testDate))
    			return testDate.Day;
    
    		//no value is recognized, so return 0:
    		return 0;
    	}
    	public static int Val(char expression)
    	{
    		int testInt;
    		if (int.TryParse(expression.ToString(), out testInt))
    			return testInt;
    		else
    			return 0;
    	}
    }


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

    • Proposed as answer by Norkk Monday, August 20, 2012 3:21 PM
    • Marked as answer by Jason Dot Wang Tuesday, August 28, 2012 8:21 AM
    Monday, August 20, 2012 3:18 PM
  • There is no simple equivalent in C#

    Actually there is: Microsoft.VisualBasic.Conversion.Val.

    But it's better to follow Servy's advice.

    Monday, August 20, 2012 3:49 PM
  • Yes - sometimes it is better just to use the Microsoft.VisualBasic dll if no clear-cut equivalent exists.


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

    Monday, August 20, 2012 4:30 PM
  • Yes - sometimes it is better just to use the Microsoft.VisualBasic dll if no clear-cut equivalent exists.

    Usually not.  In this case it would be better to code it properly, rather than trying to access or recreate 'Val'.  It's just not a generally good method to begin with.
    Monday, August 20, 2012 5:22 PM
  • The only functionnalities I would borrow from that assembly would be the InputBox or the WindowsFormsApplicationBase. All the Strings and Conversions functions have generally better alternatives.
    Tuesday, August 21, 2012 6:44 AM
  • I think the behavior of your code is not exactly correct.... especially in case of object overload and when boolean passed as string only. I have modified it and results are pretty accurate. I tested it locally for below kind of data. Ignore the class and method name please.

                Class1.ConvertToNumber("123.4saddaas43das");
                Class1.ConvertToNumber("1234saddaasdas");
                Class1.ConvertToNumber("sadd2342aasdas");
                Class1.ConvertToNumber("sadd1232aasdas");
                Class1.ConvertToNumber("23232saddaasdas");
                Class1.ConvertToNumber("23.87saddaasdas");
                Class1.ConvertToNumber("23.87saddaasdas");
                Class1.ConvertToNumber("23.87saddaasdas");
                Class1.ConvertToNumber('a');
                Class1.ConvertToNumber('1');
                Class1.ConvertToNumber(System.DateTime.Now.ToString());
                Class1.ConvertToNumber("true");
                Class1.ConvertToNumber("false");
                Class1.ConvertToNumber(true);
                Class1.ConvertToNumber(false);

    But it also does not include support for radix expressions.... Below is the little modified and accurate version.

    using System;
    
    //This class is created to immitate the behavior of VB Val() numeric converter. Not included support for identifying radix expressions i.e. &o, &h
    namespace OnePlanner.CommonCS.Utils
    {
        public class DoubleConverter
        {
            public static double Val(string expression)
            {
                if (expression == null)
                    return 0;
    
                for (int size = expression.Length; size > 0; size--)
                {
                    double testDouble;
                    if (Double.TryParse(expression.Substring(0, size), out testDouble))
                        return testDouble;
                }
    
                return 0;
            }
    
            public static double Val(object expression)
            {
                try
                {
                    if (expression == null)
                        return 0;
    
                    bool testBool;
    
                    if (expression.GetType() == typeof(Boolean) || expression.GetType() == typeof(bool))
                    {
                        //Boolean.TryParse(expression.ToString(), out testBool);
                        testBool = Boolean.Parse(expression.ToString());
                        return testBool ? -1 : 0;
                    }
    
                    string param = expression.ToString();
                    return Val(param);
                }
                catch
                {
                    return 0;
                }
            }
    
            public static int Val(char expression)
            {
                int testInt;
                if (Int32.TryParse(expression.ToString(), out testInt))
                    return testInt;
                else
                    return 0;
            }
        }
    }
    

    • Edited by Dev_Vish Thursday, July 12, 2018 11:27 AM
    Thursday, July 12, 2018 11:24 AM
  • The helper method I provided many years ago has stood up pretty well (it hasn't been changed since then).

    Your method doesn't replicate the behavior of Microsoft.VisualBasic.Conversion.Val for your own tests.  For example, Microsoft.VisualBasic.Conversion.Val("true") yields 0, not -1.

    As far as hex numbers go, can you provide an example? I just tested it and could not find a hex number where our helper method didn't provide the same value as the VB 'Val' function.


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

    Thursday, July 12, 2018 1:59 PM