Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
I am new to c#. Struggling with printing Array values. Throwing error "not all code paths return a value". Pls help me. Below is the code. Im calling this class from aspx file. Want to print in my web page. Thanks in advance.
 
public class ArrayExample
{
    int[,] myTable = new int[2, 3];
    public ArrayExample()
	{
		//
		// TODO: Add constructor logic here
		//
        myTable[0, 0] = 1;
        myTable[0, 1] = 2;
        myTable[0, 2] = 3;
        myTable[1, 0] = 4;
        myTable[1, 1] = 5;
        myTable[1, 2] = 6;
	}
    public int CallArray()
    {
        for (int row = 0; row < myTable.GetLength(0); row++)
        {
            for (int col = 0; col < myTable.GetLength(1); col++)
            {
                return myTable[row,col];
                
            }
        }
    }
 
}
 
Im calling this class from by Aspx.cs file.
 
 protected void btnArray_Click(object sender, EventArgs e)
    {
        ArrayExample aex = new ArrayExample();
        Response.Write(aex.CallArray());
    }
i want to print this in array format in my web page
Posted 29 Dec '12 - 17:28
Edited 29 Dec '12 - 17:45


1 solution

The reason for this compiler error is what if myTable.Legth returns zero? program execution will not go inside the loop and its not possible to know what value to return.
 
change your code like below
public int CallArray()
 {
     for (int row = 0; row < myTable.GetLength(0); row++)
     {
         for (int col = 0; col < myTable.GetLength(1); col++)
         {
             return myTable[row,col];
 
         }
     }
return -1;// you can treat this as error condition or array is empty 
 }
  Permalink  
Comments
BalajiJayaram - 29 Dec '12 - 23:37
Thank for your reply Jibesh. Its printing the first value of array myTable[0,0]. But i want to print all values in the array format. Pls suggest me the right code. Thanks
jibesh - 29 Dec '12 - 23:41
Ofcourse yes. because you are exiting from the loop for the first iteration itself, to print all the line you need to call the print method inside the loop. can you copy your print code too.
BalajiJayaram - 29 Dec '12 - 23:46
Updated my print code. pls do the needful

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 355
1 OriginalGriff 330
2 Arun Vasu 264
3 Tadit Dash 221
4 CPallini 203
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid