[Mono-list] how to use 'Conversions.ToInteger' in mono?

Andy Hume andyhume32 at yahoo.co.uk
Sun Jun 22 08:19:17 EDT 2008



Josh Hammond wrote:
> 
> On Sat, June 21, 2008 12:45 pm, Lauri Kotilainen wrote:
>> Can't you substitute Convert.ToInt32 for Conversions.ToInteger?
>>
> That works, but then I have the same issue for Strings.Right()
> 

Well, System.String itself has lots of useful methods quite like the ones
duplicated from VB.  The following is equivalent to your line:
   strBin = strBin.SubString(1)

BTW the use of Double as your variable types seems odd in these cases,
instead use Integer (Int32), or Int64 if you need the range, etc.


Or, you could just substitute the whole method with something like the
following:

    // The return type and the accumulator can be any numerical type.
    private static UInt64 Binary_Parse(String strBin)
    {
        if (String.IsNullOrEmpty(strBin))
            return 0;
        UInt64 a = 0;
        foreach (char ch in strBin) {
            checked { a *= 2; } //overflow!?  Should we check and thrown
something manually?
            if (ch == '1') {
                a += 1;
            } else if (ch == '0') { //nop
            } else
                throw new FormatException("Input contains a non-binary digit
'" + ch + "'.");
        }
        return a;
    }

Though you may want to change what it returns in invalid input cases, yours
returns MinValue, mine throws.

Andy
-- 
View this message in context: http://www.nabble.com/how-to-use-%27Conversions.ToInteger%27-in-mono--tp18042836p18054045.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list