Solved

Cast object to int in C#

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
javascript call 6 35
How do I get the id from URL? 19 46
How to Get total minutes between two hours 4 26
XML & .net 5 16
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

14 Experts available now in Live!

Get 1:1 Help Now