Reputation: 297
I am reading the book Essentials C# 3.0 for .NET framework 3.5 from Mark Michaelis. Since there are more classes involved I was hoping somebody had worked through the book and maybe had the same problem.
The code in chapter 7 fails(page 300). Listing 7.2 shows how to integrate an interface, I've written all of the code like it says in the book. I'm getting the error:
'xxxx.ConsoleListControl.DisplayHeader(string[])': not all code path returns a value.
The code in question is:
public static void List(string[] headers, Ilistable[] items)
{
int[] columnWidths = DisplayHeaders(headers);
for (int count = 0; count < items.Length; count++)
{
string[] values = items[count].ColumnValues;
DisplayItemsRow(columnWidths, values);
}
}
/// <summary>
/// Displays the column headers
/// </summary>
/// <returns>returns an array of column widths</returns>
private static int[] DisplayHeaders(string[] headers)
{
}
private static void DisplayItemsRow(int[] columnWidths,string[] values)
{
}
}
The string[]
headers arefilled with 4 items (FirstName, LastName, Address, Phone).
I don't know what is causing this problem, or how to fix it. I see DisplayHeaders
has no value, and columnwidths
also has no value.
I haven't put all of the code here; there are 5 classes and 1 interface. I thought maybe that would be to much and not be needed. If somebody wants all the code I will be happy to put it here.
Upvotes: 2
Views: 160
Reputation: 151604
Turn the page, or read again. I guess you're supposed to write code in the method, as it has a return type but no return statement.
Edit: alright, downloaded the PDF, the book explicitly says above this code listing:
Consider another example
And in the code it says:
private static int[] DisplayHeaders(string[] headers)
{
// ...
}
The // ...
part indicates something not interesting to the concept being explained is left out for brevity.
The code is shown to explain what an interface can do (in this case printing a list of any kind of object that implements Ilistable
), the static helper methods are irrelevant to this. The code is not meant to be run.
Upvotes: 4
Reputation: 2479
The method DisplayHeaders says it returns an array of integers (int[]
) but it is not in fact returning anything. There is quite likely code a little bit later on that fills in the method to do something useful, but in order to make the code compile, it needs to return an array. A simple way to do that would be to change it to
private static int[] DisplayHeaders(string[] headers)
{
return new int[0];
}
This causes it to return an empty array of integers.
Upvotes: 0
Reputation: 1833
Any method that has a type other than void must return an object of that type. So DisplayHeaders must return an integer array.
private static int[] DisplayHeaders(string[] headers)
private
- access modifier; indicates this method can only be called from within the class
static
- static modifier; this method does not need an instance to be called
int[]
- return type; this is the type of the object that this method will return
DisplayHeaders
- method name; this is how you refer to this method
(string[] headers)
- parameters; this indicates which arguments you need to pass to the method
We can infer from the method summary that its implementation may look something like this:
/// <summary>
/// Displays the column headers
/// </summary>
/// <returns>returns an array of column widths</returns>
private static int[] DisplayHeaders(string[] headers)
{
// builds a new int array with the same
// number of elements as the string array parameter
int[] widths = new int[headers.Length];
for (int i = 0; i < headers.Length; i++)
{
Console.WriteLine(headers[i]); // displays each header in the Console
widths[i] = headers[i].Length; // populates the array with the string sizes
}
// the return keyword instructs the program to send the variable
// that follows back to the code that called this method
return widths;
}
I would continue reading the chapter. More than likely the author fills in the implementation details of that method later on.
Upvotes: 3