Link to home
Start Free TrialLog in
Avatar of ksfok
ksfok

asked on

Cast object to int in C#

Let's review the following:
    protected bool InvalidRangeOrName(object PlayerName, object Rank)
    {
        string name = (string)PlayerName;
        int rank = (int)Rank;
        if (name.Length == 0){
            lblMsg.Text = "Player Name is required";
            return true;
        }
         if (rank < 1 || rank > 25){
             lblMsg.Text = "Rank must be between 1 and 25";
             return true;
        }
        return false;
    }

    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        if (InvalidRangeOrName(e.Values["PlayerName"], e.Values["Rank"]))
            e.Cancel = true;
    }

    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
         if (InvalidRangeOrName(e.NewValues["PlayerName"], e.NewValues["Rank"]))
            e.Cancel = true;
    }

At runtime an error {"Specified cast is not valid."} shows at: int rank = (int)Rank;

Please advise.
SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial