I am trying to solve a simple problem. But I am stuck by a simple problem I am confused by "context" in c#.

So for this simple problem I am solving Euler problem 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Timers;

namespace Euler
{
    //Problem 1
    //If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
    //Find the sum of all the multiples of 3 or 5 below 1000.

    class Problem1
    {
        public int Sum53(int args)
        {
            int num = 0;
            int limit = 1000;
            int sum = 0;

            while (num < limit)
            {
                if ((num % 3 == 0) || (num % 5 == 0))
                {
                    sum = sum + num;
                    num++;
                }
                else
                {
                    num++;
                }
            }
            string myAnswer = sum.ToString();
            Console.WriteLine("This is the total :" + myAnswer);
            return sum;
        }

        }

    }

However I cannot actually get the sum value to print or return. The current code starts the console but does not print.

If I move the console.writleine out of this class or to its own class I receive myAnswer is not available in this context.

So what exactly is context and how do I use my value in context?

Recommended Answers

All 9 Replies

If you moved the WriteLine, you also have to move the definition on line 53.
This definition was still in the context or scope of your Sum53 method and out of the context of the moved WriteLine.

So how will it work? I can't see what is different to other basic programs.

Basically a method that calculates a sum(or does whatever) doesn't write or does some other stuff. It has to do what it does good: 1 thing in this case calculate and return the result.
Back in your main program, you now can use that result to do some other calculations or to print it to the console or whatever.

Ok still wont work but these are my updated files, I still keep receiving context errors.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Euler
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine(myAnswer);
        }
    }
}

and this is Problem1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Euler
{
    internal class Problem1
    {
        public string Sum53()
        {
            int num = 0;
            int limit = 1000;
            int sum = 0;
            string myAnswer = sum.ToString();

            while (num <= limit)
            {
                if ((num % 3 == 0) || (num % 5 == 0))
                {
                    sum = sum + num;
                    num++;
                }
                else
                {
                    num++;
                }
            } return myAnswer;
        }
    }
}

Couldn't solve it using to class files I couldn't overcome the context errors.

By using 1 file I was able to create the solution below. it would be interesting to understand what the context issue was if anyone knows. Because if I split the code into 2 classes the context issues reoccur.

namespace Euler
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(SumMe(1000));
        }

        public static int SumMe(int limit)
        {
            int sum = 0;

            for (int i = 0; i < limit; i++)
            {
                if ((i % 3 == 0) || (i % 5 == 0))
                {
                    sum += i;
                }
            }
            return sum;
        }
    }
}

I hope this little snippet clears the fog. If not please ask.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestEuler
{
    //placed this class here for conveniance
    //could be a separate class in your solution
    class MyCalculations 
    {
        public MyCalculations()
        {
        }

        public int SumIt(int total)
        {
            int sum = 0;
            for (int i = 0; i < total; i++)
            {
                sum += i;
            }
            return sum;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyCalculations Calc = new MyCalculations();
            int Result = Calc.SumIt(10);
            Console.WriteLine(Result.ToString());
            Console.ReadKey();
        }
    }
}

So that means you can't use a class elsewhere without creating a constructor to use it?

Unless the class is static. A typical example is the Math class. Here you don't have to use a constructor to call say Math.Sqrt(aValue);

An instance constructor is used to create an instance of a class. In C# it isn't necessary to create a default constructor that is empty. What do I mean by "a default constructor that is empty"?

Let's look at some of the code from above:

class MyCalculations 
{
    public MyCalculations()
    {
    }//default constructor

    public int SumIt(int total)
    {
          ...

    }//SumIt

}//class

Since the constructor is empty--doesn't contain any code--it isn't necessary to specify it. C# automatically includes a default (empty) constructor.

The above class can be written like this:

class MyCalculations 
{        
    public int SumIt(int total)
    {
          ...

    }//SumIt

}//class

However, once you define another constructor C# no longer automatically includes the default (empty) constructor, so you need to include it if you want it in your class.

Resources:

Instance Constructors (C# Programming Guide)

"...If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields..."

Static Classes and Static Class Members (C# Programming Guide)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.