?
Solved

Cast object to int in C#

Posted on 2007-03-24
3
Medium Priority
?
13,401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 600 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:Fahad Mukhtar
Fahad Mukhtar earned 600 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 800 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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