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(ob ject sender, FormViewInsertEventArgs e)
{
if (InvalidRangeOrName(e.Valu es["Player Name"], e.Values["Rank"]))
e.Cancel = true;
}
protected void FormView1_ItemUpdating(obj ect sender, FormViewUpdateEventArgs e)
{
if (InvalidRangeOrName(e.NewV alues["Pla yerName"], e.NewValues["Rank"]))
e.Cancel = true;
}
At runtime an error {"Specified cast is not valid."} shows at: int rank = (int)Rank;
Please advise.
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(ob
{
if (InvalidRangeOrName(e.Valu
e.Cancel = true;
}
protected void FormView1_ItemUpdating(obj
{
if (InvalidRangeOrName(e.NewV
e.Cancel = true;
}
At runtime an error {"Specified cast is not valid."} shows at: int rank = (int)Rank;
Please advise.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.