Solved

Cast object to int in C#

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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