Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 18:28pm
Edited 29-Dec-12 18:45pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 29-Dec-12 23:37pm
   
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 at 29-Dec-12 23:41pm
   
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 at 29-Dec-12 23:46pm
   
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)



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100