Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indexing Error

Status
Not open for further replies.

liamba

Technical User
Jan 6, 2007
21
IE
Hi,
Im getting an error and i have no idea what it means.Hoping someone could shed some light on it for me.Thank you

Cannot apply indexing with [] to an expression of type 'int'

Here is the code
public void FindMaxValue()
{
objDataTable = objDataSet.Tables["tblScore"];
foreach (int drowRow in objDataTable.Rows)
{
if (drowRow["Total"] > decMaxValve)
{
decMaxValve = drowRow["Total"];
}
}
decXScale = ImageWidth / (objDataTable.Rows.Count - 1);
decYScale = ImageHeight / decMaxValve;
}
 
this line is wrong
Code:
foreach (int drowRow in objDataTable.Rows)

should be
foreach(DataRow...

Age is a consequence of experience
 
Hi,

After changing to a data row im still getting a error

Operator '>' cannot be applied to operands of type 'object' and 'decimal'

What i am trying to do is loop throw all the "Total" columns in my datatable and pick out the highest

public void FindMaxValue()
{
objDataTable = objDataSet.Tables["tblScore"];
foreach (DataRow drowRow in objDataTable.Rows)
{
if (drowRow["Total"] > decMaxValue)
{
decMaxValue = drowRow["Total"];
}
}
decXScale = ImageWidth / (objDataTable.Rows.Count - 1);
decYScale = ImageHeight / decMaxValue;
}

maybe a better way to do this?
Thank you
 
. Operator '>' cannot be applied to operands of type 'object' and 'decimal'

> Do proper casting.


. maybe a better way to do this?

> Yes. Have a look at the datatable's '.Select' method!



Regards
 
I don;t know if i helped enough...

The .Select method has a criteria and a sort arguments (both strings). One way is to set the sort: "Total DESC" and grab te 1st item. This will be the max!


Regards
 
you could give this a try
Code:
public void FindMaxValue()
{
    int myInt = 0;
    objDataTable = objDataSet.Tables["tblScore"];
    foreach (DataRow drowRow in objDataTable.Rows)
    {
       myInt = Int32.Parse(drowRow["Total"].ToString());
       if (myInt > decMaxValue)
       {
           decMaxValue = drowRow["Total"];
       }
     } 
     decXScale = ImageWidth / (objDataTable.Rows.Count - 1);
     decYScale = ImageHeight / decMaxValue;
    }


Age is a consequence of experience
 
Typo ALERT, typing in MS Word sorry :(

change this:
decMaxValue = drowRow["Total"];
to:
decMaxValue = myInt

also i should add, if there is a chance that myInt could be 'null' then use Int32.TryParse so you will always be safe :)

Age is a consequence of experience
 
Thats works perfectly.
Thanks very much.
 
Just curious....

what is the main difference between:

myInt = Int32.Parse(drowRow["Total"].ToString());

and

Convert.ToInt32(drowRow["Total"].ToString());

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top