zerodowned

Moderator
Is this the best way to update an int when a ulong updates without the int overloading?

Basically I want the int to mirror the ulong up to the int limit.


ulong tokensAmount

C#:
        private int tokensAmountConverted;
       
        [CommandProperty(AccessLevel.GameMaster)]
        public int TokensAmountConverted
        {
            get
            {
                return (int)tokensAmount < int.MinValue ? int.MinValue : (int)tokensAmount > int.MaxValue ? int.MaxValue : (int)tokensAmount;
            }
            set
            {
                tokensAmountConverted = value;
                InvalidateProperties();
            }
        }
 
Last edited:
or you can do something like this for example, edit to your use!

Code:
private int TokensAmountConverted = 0;

try
{
TokensAmountConverted = Convert.ToInt32(tokensAmount);
}
catch
{
TokensAmountConverted = 0;
}
 
thank you, i decided to just change tokenamount to int instead. realistically no one should be able to earn over 2 billion tokens
 
good idea, but in a case where you have a conversion from larger to smaller or a string to int etc etc, and you don't think it'll be used a lot, meaning that the wrong value won't be entered or the chances a string will have a non convertible char etc etc then I would do the try, catch instead of any long drawn out method of checks! It is quick and dirty but works 100% =]
 
Back