473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Index out of bounds

Hi

I'd like to know how to inlcude a check to make sure the index requested is not out of bounds in C#.

In the following code I have created an array to store ten numbers at most. The array must be accessible using an Indexer. In the Indexer I would like to include the check.


Expand|Select|Wrap|Line Numbers
  1. using System; 
  2.  class Numbers {
  3.  
  4.  private int[] nums = new int[10]; 
  5.  public int this [uint index] {
  6.  
  7.    get {
  8.                 // can I add a check here and if so what is the syntax?
  9.         return nums[index];
  10.     }
  11.     set {
  12.      // will I need it here too?
  13.           lots[index] = value; 
  14.     }
  15.  
  16.   }
  17.  
  18.  
  19. static void Main() {
  20.     Numbers num = new Numbers(); 
  21.     num[0] = 1; 
  22.     num[1] = 23; 
  23.     num[2] = 5; 
  24.     num[3] = 5;
  25.     num[4] = 24;
  26.     num[5] = 5;
  27.     num[6] = 5;
  28.     num[7] = 67;
  29.     num[8] = 90;
  30.     num[9] = 11;
  31.  
  32.  
  33.     for (uint i = 0; i < 10; i++) {
  34.         Console.WriteLine("The numbers " + num[i]);
  35.  
  36.  
  37.    }
  38.  } 
  39.  
  40.  }
Thanks for your help.

M
Mar 7 '08 #1
1 1487
Plater
7,872 Expert 4TB
All the arrays have a .Length property that tells how many there are.
In your case:
Expand|Select|Wrap|Line Numbers
  1. nums.Length
  2.  
It will return a 10. Your index should be allowed 0-9 (assuming C#)
So index <nums.Length
Mar 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: kim | last post by:
Scenario (I'm a newbie): I have a datagrid with countries listed and 5 parameters in each row. I want to add a row to this datagrid via an Event Handler. Very basic stuff. This method then call a...
0
by: Eugene | last post by:
Hello all, I've been trying to figure this out for a few days now, and still have no clue what's going on... I have a few related tables in MS Access (Clients, Cars, Sales), and a datagrid,...
1
by: Joe | last post by:
This is a weird problem. when doing trying to add a tabpage to a tabcontrol I get an error: Index was outside the bounds of the array. Now this only happens when I call a function from within...
1
by: iCeCLoW | last post by:
Hi there, I´m using the rowFilter property for filtering the rows in a DataView object. If the filter is set to an inexistent value , that is, after filtering i have 0 rows in the DataView,...
0
by: scotthutchinson | last post by:
I have a .NET Remoting object hosted in IIS6 on Windows Server 2003 (happens before and after installing SP1) at an endpoint (ASP.NET application virtual folder) named "CompanyXYZReporting". The...
2
by: Denis C | last post by:
Hi there, I'm trying to convert part of a byte array into a series of fixed length strings but half way through the for loop I get an error that I'm accessing outside the buffer bounds. The...
4
by: Antoine | last post by:
Herfried and Cor:- I used tracing and actually tracked down the code that was causing the problem most likely. I wonder if you wanted to comment on it. Also I wonder if there is a better way...
4
by: Steph. | last post by:
I have a List view displaying data in Detail mode with several columns. How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column...
0
by: Beaker | last post by:
I am having some trouble with an array object and a web service I have. I have a farly simple user object public class User { private Guid myUserId; public Guid UserId { get { return...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.