none
Training Exercise - Need Some Direction - Add Class and Static Main RRS feed

  • Question

  • In an exercise on my beginner course I was asked to make an iteration program that counts all the numbers between 1 and 100 divisible by 3.

    The way I show it works. But I can't do it the way the instructor did.  I get a 'no static main error'.  Does this have to do with the namespace naming?  Did he use a separate folder?  

    My Answer:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace CSharpExamples
    {
        class Loops
            {
            static void Main(string[] args)
            {
                var count = 0;
                for (var i = 1; i <= 100; i++)
                {
                    if (i % 3 == 0)
                        count++;
                }
                Console.WriteLine("There are {0} numbers divisible by 3 and 100.", count);
            }
        }

    His Answer:  

    using System;
    namespace CSharp1Exercises.ControlFlow
    {
        public class Loops
        {
            /// <summary>
            /// Write a program to count how many numbers between 1 and 100 are divisible by 3 with no remainder.
            /// Display the result on the console.
            /// </summary>
            public void Exercise1()
            {
                var count = 0;
                for (var i = 1; i <= 100; i++)
                {
                    if (i%3 == 0)
                        count++;
                }
                Console.WriteLine("There are {0} numbers divisible by 3 between 1 and 100.", count);
            }



    Sunday, May 6, 2018 9:47 PM

Answers

  • Here is an example that may help

    • In the project create a folder ControlFlow
    • Create a new class in this folder named Loops.

    When creating loops the namespace will be the project namespace.Folder name

    using System;
    
    namespace CSharp1Exercises.ControlFlow
    {
        public class Loops
        {
            /// <summary>
            /// Write a program to count how many numbers between 1 and 100 are divisible by 3 with no remainder. 
            /// Display the result on the console.
            /// </summary>
            public void Exercise1()
            {
                var count = 0;
                for (var i = 1; i <= 100; i++)
                {
                    if (i % 3 == 0)
                        count++;
                }
    
                Console.WriteLine("There are {0} numbers divisible by 3 between 1 and 100.", count);
            }
        }
    }
    
    

    To access the class you need a using statement in program class e.g.

    using System;
    using CSharp1Exercises.ControlFlow;
    
    namespace CSharp1Exercises
    {
        class Program
        {
            static void Main(string[] args)
            {
                var ops = new Loops();
                ops.Exercise1();
                Console.ReadLine();
            }
        }
    }
    
    Hopefully that makes sense.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 7, 2018 1:11 AM
    Moderator
  • Greetings Learning To Fly 68.

    I'm not sure if Karen has made it clear what the problem was, so I thought I'd add to her explanation. My apologies if it's not necessary.

    Every application written in c# must have a static method called Main. This is the entry point, where the code starts executing.

    static void Main(string[] args) // Every program needs one of these, somewhere.

    Your solution puts all the executable code in that one method, whereas your instructor's code has been separated out into its own class. In order to get your instructor's code to work, you need to add that class to a project that includes a static Main, then call the class from there as per Karen's example.

    Monday, May 7, 2018 2:34 AM
  • Thank you so much!  So many things rattling around in my head the organization got lost.  

    I did have one error:  Severity Code Description Project File Line Suppression State
    Error CS0246 The type or namespace name 'Loops' could not be found (are you missing a using directive or an assembly reference?) CSharp1Exercises c:\users\admin\source\repos\CSharp1Exercises\CSharp1Exercises\Program.cs 13 Active

    Monday, May 7, 2018 3:57 AM
  • Thank you both very much. I learned that a few days ago, and I also new how to add a class separate from the main program.  I just couldn't organize and execute it properly.  You're a big help!!
    Monday, May 7, 2018 3:59 AM

All replies

  • Here is an example that may help

    • In the project create a folder ControlFlow
    • Create a new class in this folder named Loops.

    When creating loops the namespace will be the project namespace.Folder name

    using System;
    
    namespace CSharp1Exercises.ControlFlow
    {
        public class Loops
        {
            /// <summary>
            /// Write a program to count how many numbers between 1 and 100 are divisible by 3 with no remainder. 
            /// Display the result on the console.
            /// </summary>
            public void Exercise1()
            {
                var count = 0;
                for (var i = 1; i <= 100; i++)
                {
                    if (i % 3 == 0)
                        count++;
                }
    
                Console.WriteLine("There are {0} numbers divisible by 3 between 1 and 100.", count);
            }
        }
    }
    
    

    To access the class you need a using statement in program class e.g.

    using System;
    using CSharp1Exercises.ControlFlow;
    
    namespace CSharp1Exercises
    {
        class Program
        {
            static void Main(string[] args)
            {
                var ops = new Loops();
                ops.Exercise1();
                Console.ReadLine();
            }
        }
    }
    
    Hopefully that makes sense.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 7, 2018 1:11 AM
    Moderator
  • Greetings Learning To Fly 68.

    I'm not sure if Karen has made it clear what the problem was, so I thought I'd add to her explanation. My apologies if it's not necessary.

    Every application written in c# must have a static method called Main. This is the entry point, where the code starts executing.

    static void Main(string[] args) // Every program needs one of these, somewhere.

    Your solution puts all the executable code in that one method, whereas your instructor's code has been separated out into its own class. In order to get your instructor's code to work, you need to add that class to a project that includes a static Main, then call the class from there as per Karen's example.

    Monday, May 7, 2018 2:34 AM
  • Thank you so much!  So many things rattling around in my head the organization got lost.  

    I did have one error:  Severity Code Description Project File Line Suppression State
    Error CS0246 The type or namespace name 'Loops' could not be found (are you missing a using directive or an assembly reference?) CSharp1Exercises c:\users\admin\source\repos\CSharp1Exercises\CSharp1Exercises\Program.cs 13 Active

    Monday, May 7, 2018 3:57 AM
  • Thank you both very much. I learned that a few days ago, and I also new how to add a class separate from the main program.  I just couldn't organize and execute it properly.  You're a big help!!
    Monday, May 7, 2018 3:59 AM
  • Oooh, I got it. I added Using to the top and it fixed my latest error:

    using CSharp1Exercises.ControlFlow;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace CSharp1Exercises
    {
        class Program
        {
            static void Main(string[] args)
            {
                var ops = new Loops();
                ops.Exercise1();
                Console.ReadLine();
            }
        }
    }

    Monday, May 7, 2018 4:05 AM
  • Ooh, I fixed it by adding a using statement referring to the added folder class:

    using CSharp1Exercises.ControlFlow;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace CSharp1Exercises
    {
        class Program
        {
            static void Main(string[] args)
            {
                var ops = new Loops();
                ops.Exercise1();
                Console.ReadLine();
            }
        }
    }

    Monday, May 7, 2018 4:06 AM