Solved

Cast object to int in C#

Posted on 2007-03-24
3
13,200 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:ksfok
3 Comments
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 150 total points
ID: 18787469
u sure this object return int always 'Rank' ?

try this before casting

MessageBox.Show(Rank.GetType());
MessageBox.Show(Rank.ToString());
0
 
LVL 19

Assisted Solution

by:Desp
Desp earned 150 total points
ID: 18787502
Try to debug the code to see whats inside "Rank" in FormViewInsertEventArgs ... did you try Convert.toint32 ?
0
 
LVL 15

Accepted Solution

by:
stanscott2 earned 200 total points
ID: 18789041
The FormViewInsertEventArgs  e.Values is made up of DictionaryEntry objects.  If I wanted to iterate through e, I would do this:

Foreach (DictionaryEntry entry in e.Values) {
  String desc = entry.Key.ToString();
  String val = entry.Value;
}

Each DictionaryEntry, as you can see, has a Key and a Value.  Based on this logic, the Rank you're getting from this:

e.Values["Rank"]

comes back as a string, not an object.  If this is right, the arguments for your boolean test should be strings, not objects.  If they are objects, then you should explicitly convert Rank to a string before casting it:

int rank = Convert.toInt32(Rank.ToString());

an addition to Desp's idea.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC -Add authentication 2 29
C# Gridview 1 47
jquery + C#.net radiobuttonlist 3 30
Unlocking a column in excel using C# 17 18
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question