Hi! I want to ask if anyone can help me with my problem. I'm trying to write a program for an address book in C# using arrays.I'm still in the beginning and already have a problem.
This is the code i've written so far : - using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace ConsoleApplication1
-
{
-
class Hope
-
{
-
static void Main(string[] args)
-
{
-
string str1, str2, str3, str4, str5;
-
-
Console.WriteLine("Enter Family name");
-
string s1 = Console.ReadLine();
-
-
Console.WriteLine("Enter first name");
-
string s2 = Console.ReadLine();
-
-
Console.WriteLine("Enter road number");
-
string s3 = Console.ReadLine();
-
-
Console.WriteLine("Enter road name");
-
string s4 = Console.ReadLine();
-
-
Console.WriteLine("Enter town name");
-
string s5 = Console.ReadLine();
-
-
string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
-
-
for (int i = 0; i < addressBook.Length / 2; i++)
-
{
-
str1 = addressBook[i, 0];
-
str2 = addressBook[i, 1];
-
str3 = addressBook[i, 2];
-
str4 = addressBook[i, 3];
-
str5 = addressBook[i, 4];
-
-
Console.WriteLine(" {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
-
Console.ReadLine();
-
}
-
Console.WriteLine();
-
-
}
-
}
-
}
In the for loop when the program goes to this line - str2 = addressBook[i, 1];
it breaks and shows me this message "Index was outside the bounds of the array."
I can't understand why it's happening and can't figure out a way to fix it.
I'll be happy for any help you can give me .
P.S. I'm a novice with C#.
5 3528
well, "string[,]" is double array.
single array, "string[]" is good enough.
try this: - using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace ConsoleApplication1
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
string str1, str2, str3, str4, str5;
-
-
Console.Write("Enter Family name: ");
-
string s1 = Console.ReadLine();
-
-
Console.Write("Enter first name: ");
-
string s2 = Console.ReadLine();
-
-
Console.Write("Enter road number: ");
-
string s3 = Console.ReadLine();
-
-
Console.Write("Enter road name: ");
-
string s4 = Console.ReadLine();
-
-
Console.Write("Enter town name: ");
-
string s5 = Console.ReadLine();
-
-
string[] addressBook = { s1, s2, s3, s4, s5 };
-
-
for (int i = 0; i < addressBook.Length; i++)
-
{
-
str1 = addressBook[0];
-
str2 = addressBook[1];
-
str3 = addressBook[2];
-
str4 = addressBook[3];
-
str5 = addressBook[4];
-
-
Console.WriteLine("\nResult: {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
-
Console.ReadLine();
-
}
-
Console.WriteLine();
-
}
-
}
-
}
if you wish to store more than 1 record dynamically, you can use "List<>". - using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace ConsoleApplication1
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
List<string[]> lst = new List<string[]>();
-
-
bool ExitProgram = false;
-
-
while (!ExitProgram)
-
{
-
Console.Clear();
-
Console.WriteLine("Total records: " + lst.Count);
-
Console.WriteLine("\nAdd new record\n");
-
string str1, str2, str3, str4, str5;
-
-
Console.Write("Enter Family name: ");
-
string s1 = Console.ReadLine();
-
-
Console.Write("Enter first name: ");
-
string s2 = Console.ReadLine();
-
-
Console.Write("Enter road number: ");
-
string s3 = Console.ReadLine();
-
-
Console.Write("Enter road name: ");
-
string s4 = Console.ReadLine();
-
-
Console.Write("Enter town name: ");
-
string s5 = Console.ReadLine();
-
-
string[] addressBook = { s1, s2, s3, s4, s5 };
-
-
lst.Add(addressBook);
-
-
Console.WriteLine();
-
-
for (int i = 0; i < lst.Count; i++)
-
{
-
str1 = addressBook[0];
-
str2 = addressBook[1];
-
str3 = addressBook[2];
-
str4 = addressBook[3];
-
str5 = addressBook[4];
-
-
Console.WriteLine("Record " + (i + 1) + ": {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
-
}
-
Console.Write("\nPress any key to continue, or [Q] to exit: ");
-
-
string nextTask = Console.ReadLine().ToUpper();
-
-
if (nextTask == "Q")
-
ExitProgram = true;
-
}
-
}
-
}
-
}
Thanks adriancs! This is really helpful but can you tell me why str2 = addressBook[i, 1]; is out of the bounds if the array? I can't figure that out and I want to understand it so I won't make the same mistake in the future.
Hi, qjmigo,
lets say, we create 5 strings: - string a1, b1, c1, d1, e1;
because this >> "[,]" is only 2 dimensional array, we can understand it with a 2-D graph.
for example, columns and rows.
so, if we initialize the string array with this: - string[,] addressBook = { { a1 }, { b1 }, { c1 }, { d1 }, { e1 } };
will create 5 rows with 1 column. Lets asume Y = row, and X = column. - X1
-
Y1 a1
-
Y2 b1
-
Y3 c1
-
Y4 d1
-
Y5 e1
as you can see, addressBook only contains 5 strings.
results: -
addressBook.Length = 5;
-
addressBook[0,0] = a1;
-
addressBook[3,0] = d1;
lets try another example. We initialize the string array with this: -
string a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, e1, e2, e3;
-
string[,] addressBook = { { a1, a2, a3 }, { b1, b2, b3 }, { c1, c2, c3 }, { d1, d2, d3 }, { e1, e2, e3 } };
this will create 5 rows and 3 columns, - X1 X2 X3
-
Y1 a1 a2 a3
-
Y2 b1 b2 b3
-
Y3 c1 c2 c3
-
Y4 d1 d2 d3
-
Y5 e1 e2 e3
Results: - addressBook.Length = 15;
-
addressBook[0,0] = a1;
-
addressBook[2,1] = c2;
-
addressBook[4,2] = e3;
-
addressBook[5,0] = outside the bounds of array, there is no Row 6.
-
addressBook[0,3] = outside the bounds of array, there is no Column 4.
now we come back to your codes, this block: - for (int i = 0; i < addressBook.Length / 2; i++)
-
{
-
-
}
in this case: - string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
-
addressBook.Length = 5;
which means: - for (int i = 0; i < (5 / 2); i++)
-
{
-
-
}
then - addressBook[i, 0]; << exist
-
addressBook[i, 1]; << there is no column 2
-
addressBook[i, 2]; << there is no column 3
-
addressBook[i, 3]; << there is no column 4
-
addressBook[i, 4]; << there is no column 5
and because your loop condition is " i < (5 / 2) "
this "for loop" will have at least 3 loops.
first loop, i = 0
this loop is ok. the number zero refers to first row.
but start from 2nd loop, i = 1, refers to 2nd row.
but there is no 2nd row, thus, create another error >> outside the bounds of array.
3rd loop, i = 2, refers to 3rd row.
>> outside the bounds of array. There is no row 3.
adriancs that is a really rich answer! Now I understand where is the problem. Thanks a lot! :-)
Hi, you are welcome.
by the way, you may consider to mark "choose as best answer button" on the most appropriate post that has solved your problems. :-)
thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: ORC |
last post by:
I have a large array where the first index must be removed, but how is the
best way to do that when the application must be optimized to speed?
Thanks
Ole
|
by: B-Dog |
last post by:
Any idea why when I try to fill my dataset using
xContacts.Fill(xds.Contacts) I get the error:
: Index was outside the bounds of the array.
I've tried rebuilding my dataadapters and dataset...
|
by: Allen Maki |
last post by:
Why can I get the index of the item of the array when I use string*, but
can not get the index of the array when I use any other type (such as
Int32)?
This code will compile perfectly, but if I...
|
by: routeslip |
last post by:
I'm refering to an entry in an array by it's string key, as in
foo. Is there a way to get the numeric index of that array
without iterating through the entire array?
What I need to do is find...
|
by: writebrent |
last post by:
I wrote a little interface for users to post data to a website. On
their local machines, it produces CSV from an Excel spreadsheet, then
posts it to the site. In some cases, the CSV will contain...
|
by: Erwin Moller |
last post by:
Hi,
Consider the following code:
-------------------------
function giveArr(){
return array("one","two","three");
}
echo giveArr();
|
by: sgottenyc |
last post by:
Hello,
If you could assist me with the following situation, I would be very
grateful.
I have a table of data retrieved from database displayed on screen.
To each row of data, I have added...
|
by: foolios |
last post by:
private void CreateMaze(int h, int w)
{
int r = h * w;
Room rooms = new Room;
Random rm = new Random();
int newrm = rm.Next(0, r);
...
|
by: bouy78 |
last post by:
Quick Question Seems to be an easy task, but apparently I'm not very
bright. I have an array of checkboxes, created at runtime and
inserted in a panel. Upon creation, they're each attached to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |