p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 6.0 Programming with Visual Studio 2015 (http://p2p.wrox.com/forumdisplay.php?f=798)
-   -   Chapter 26 Intro to LINQ (http://p2p.wrox.com/showthread.php?t=100168)

Eureka117 March 11th, 2019 01:14 AM

Chapter 26 Intro to LINQ
 
This issue is actually from beginning-microsoft-visual-c-2008-isbn-978-0-470-19135-4-400. I have that book and unfortunately the thread is closed there so I want to post my issue here so any others can benefit from a solution. Since this chapter is just the introduction to LINQ I don't think there will be too many differences from the code then (2008) as compared to now (2019).

On page 865, I typed the code exactly to create the generateLotsOfNumbers() function but receive this error when trying to compile:

Code:

'Program.generateLotsOfNumbers(int)': not all code paths return a value
I'm not at all sure what's going on as the book indicates there is nothing wrong with this code.

code:
Code:

private static int[] generateLotsOfNumbers(int count)
        {
            Random generator = new Random(0);
            int[] result = new int[count];
           
            for (int i = 0; i < count; i++)
            {
                result[i] = generator.Next();
            }
        }


Eureka117 March 11th, 2019 02:31 AM

I forgot to add return result;
 
Well I found out what I did wrong. It turns out that I forgot to return the result. So not wonder the program would not compile.

Here's the code I needed to add:

Code:

return result;
So the whole function needs to look like this:
Code:

private static int[] generateLotsOfNumbers(int count)
        {
            Random generator = new Random(0);
            int[] result = new int[count];
           
            for (int i = 0; i < count; i++)
            {
                result[i] = generator.Next();
            }
           
            return result;

        }



All times are GMT -4. The time now is 11:18 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.