Solved

Cast object to int in C#

Posted on 2007-03-24
3
13,089 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now