Click here to Skip to main content
15,937,066 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help with accessing array element by index in C#. I have an array of services we offer and I want at the click of a button somewhere to accumulate totals per type of service. I have declared an array of service totals and a simple method to accumulate totals
Here is the skeleton of my code:
private int[] serviceTotals = new int[10];  

 public void AccumulateTotals()
 int indexInteger=0;
 int serviceCount +=1;

The problem is somewhere else in the program I call the the method & use this code:

Textbox1.Text = serviceTotals[0].ToString //for the first item in the array

I get an error: "Cannot apply indexing with[] to an expression of type 'int'
What am I doing wrong??

[Edit:Leroy Gibbs]: Adjusted code-tags
Updated 29-Jul-13 3:07am
Fredrik Bornander 29-Jul-13 4:57am    
Have you defined serviceTotals more than once?
It sounds like to have a definition (probably a local one) that is defined as an int.
joshrduncan2012 29-Jul-13 9:16am    
You also need to add the () on ToString.
[no name] 30-Jul-13 11:03am    
I think you are assigning and declaring in a single statement here. "int serviceCount +=1;". Make them separate then you will get the solution.

C# is case sensitive. ServiceTotals is not the same as serviceTotals.
Share this answer
Try this :

public int[] serviceTotals = new int[10];

to fill the array :

int serviceCount =1;
         for (int i = 0; i < serviceTotals.Count(); i++)
             serviceTotals[i] = serviceCount;

to call the first item :

Share this answer
it must work unless you're pointing to a different variable with different type! Case sensitivity may be the issue.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900