-2

I'm trying to get the following bit of code to print out the "sum" to the console, it compiles fine and everything but never prints out the sum. Can someone give me a hint please? Thanks.

class Program
{
    static void Main(string[] args)
    {
        // Not sure how to call "ArraySum" from here.
    }

    public void ArraySum()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int sum = 0;

        foreach (int x in arr)
        {
            sum += x;
        }
        Console.WriteLine(sum); 
    }

EDIT: Sorry for not posting the whole code, but yeah. Basically I'm not sure how to call the method from main and have it print out the sum.

Valeri Lagunov
  • 89
  • 1
  • 1
  • 7
  • 3
    Do you ever call `ArraySum`? – Carcigenicate Sep 30 '16 at 16:52
  • 2
    I think the duplicate is wrong. He didn't say it closes before he can see it, he said it just doesn't print. – Carcigenicate Sep 30 '16 at 16:53
  • 3
    You either don't call this method from your `main()`, or the console window disappears before you get a chance to read the output. See [duplicate](http://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-without-displaying-my-output) for the latter. If that doesn't apply, read [ask] and provide a [mcve] that contains enough code to properly demonstrate the issue. – CodeCaster Sep 30 '16 at 16:53
  • As @CodeCaster said OP is probably posting outside of `main()` it prints properly if that is done. – confusedandamused Sep 30 '16 at 16:54
  • 1
    That was Carcigenicate's observation, @confusedandamused. It's equally plausible as the console window closing, thus the OP not being able to read the output. It's off-topic at the moment either way. I'll happily reopen it if the OP provides additional detail; as currently stated it's unanswerable and will remain closed as far as I'm concerned. – CodeCaster Sep 30 '16 at 16:55
  • 1
    @val did you try debugging? – rory.ap Sep 30 '16 at 16:55
  • 1
    Ah sorry didn't even see that above @CodeCaster - need more coffee :) – confusedandamused Sep 30 '16 at 16:56
  • 1
    @rory.ap I think the latest community edition of VS just sends you to SO in a browser window when you hit F9. That's my best guess. – 15ee8f99-57ff-4f92-890c-b56153 Sep 30 '16 at 17:07
  • Everything in your program is a constant. Why are you writing this code instead of just putting `Console.WriteLine(15)` in your `Main`? – Dour High Arch Sep 30 '16 at 19:54
  • @Carcigenicate h-how do I call it good sir? – Valeri Lagunov Sep 30 '16 at 20:35
  • @DourHighArch homework I guess... pls help. – Valeri Lagunov Sep 30 '16 at 20:35
  • @CodeCaster added more info, sorry. – Valeri Lagunov Sep 30 '16 at 20:36
  • 1
    @ValeriLagunov Are you asking how to call a function? You should review the basics if you don't know that (no offense). You call a function like: `ArraySum()`. – Carcigenicate Sep 30 '16 at 20:38
  • `var program = new Program(); program.ArraySum();` - your title is very misleading. – Ant P Sep 30 '16 at 20:39
  • @AntP thank you... that worked. – Valeri Lagunov Sep 30 '16 at 20:43
  • @Carcigenicate sorry, one of the repliers told me to use "var program = new Program(); program.ArraySum()" which worked but I don't think I've seen an implementation like this anywhere before (online coruses etc.). – Valeri Lagunov Sep 30 '16 at 20:44
  • @ValeriLagunov The way you call a function is by ending it with parenthesis that contain the arguments to the function. How that function is namespaced depends on the language (I don't know C# honestly). I would have expected you to be able to call `ArraySum` without instantiating a `Program`, since it seems `Program` is the the main class, but that could be a difference between C# code and similar Java code, which I'm thinking of. levelonehuman's answer below seems to suggest that I'm right though. – Carcigenicate Sep 30 '16 at 20:49
  • Whoops, nvm, I missed the fact that `ArraySum` isn't `static`. – Carcigenicate Sep 30 '16 at 20:53

1 Answers1

1

To get this to work like you've got it:

static void Main(string[] args)
{
    ArraySum();
}

But if you try to compile this, you'll see an error message similar to an object reference is required ... Program.ArraySum().

Why?

Main() is static - simply, this means you don't create an instance of it - there's only one. So in addition to the above, update ArraySum() to be static as well:

static void ArraySum()
{
    //your code
}

and it should work like you expect it to.

Edit:

As an aside of sorts, this tutorial helped me out a lot when I first started, and I'd highly recommend it.

levelonehuman
  • 1,465
  • 14
  • 23