CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2013
    Posts
    16

    problematic Main need help to understand how to use the main

    some one told me that my main is too long and i need to break it to methods
    is it true? and if it is so How would you break up the Main?
    this is the code:

    Code:
     class Program
        {
            static void Main(string[] args)
            {
                string menu="y",name,branch,number;
                int id,g1,g2,g3;
    
                List<Student> students = new List<Student>();
    
                while (menu !="e" && menu!="E")
                {  
                   
                    Console.WriteLine("Student Menu Manu");
                    Console.WriteLine("Press 1 to Add Student");
                    Console.WriteLine("Press 2 to Add student grades");
                    Console.WriteLine("Press 3 to print studens marks");
                    number = Console.ReadLine();
                    switch (number)
                    {
                        case "1":
                            {
                                Console.WriteLine("Enter Student name:");
                                name = Console.ReadLine();
                                Console.WriteLine("Enter Student ID:");
                                id = Convert.ToInt16(Console.ReadLine());
                                Console.WriteLine("Enter Student branch:");
                                branch = Console.ReadLine();
                                if (branch == "CSE" || branch == "cse")
                                {
                                    Student sCSE = new ScienceStudent(name, id);
                                    students.Add(sCSE);
                                }
                                else if (branch == "ECE" || branch == "ese")
                                {
                                    Student sECE = new CommerceStudent(name, id);
                                    students.Add(sECE);
    
                                }
                                else
                                {
    
                                    Console.WriteLine("your branch is not correct,");
                                }
                                break;
                            }
                        case "2":
                            {
                                Console.WriteLine("Enter Student name:");
                                name = Console.ReadLine();
                                Console.WriteLine("Enter Student ID:");
                                id = Convert.ToInt16(Console.ReadLine());
                                Console.WriteLine("Enter Student branch:");
                                branch = Console.ReadLine();
                                if (branch == "CSE" || branch == "cse")
                                {
                                    for (int i = 0; i < students.Count; i++)
                                    {
                                        if ((students[i] is ScienceStudent) && (name == students[i].Name) && (id == students[i].RegisterID) )
                                        {
                                            Console.WriteLine("Enter ComputerNetwork grade:");
                                            g1 = Convert.ToInt16(Console.ReadLine());
                                            Console.WriteLine("Enter DataStruture grade:");
                                            g2 = Convert.ToInt16(Console.ReadLine());
                                            Console.WriteLine("Enter Testing grade:");
                                            g3 = Convert.ToInt16(Console.ReadLine());
                                            ((ScienceStudent)students[i]).SetGrades(g1, g2, g3);
    
                                        }
                                        
                                    }
                                }
                                else if (branch == "ECE" || branch == "ese")
                                {
                                    for (int i = 0; i < students.Count; i++)
                                    {
                                        if ((students[i] is CommerceStudent) && (name == students[i].Name) && (id == students[i].RegisterID))
                                        {
                                            Console.WriteLine("Enter elctronicCricuit grade:");
                                            g1 = Convert.ToInt16(Console.ReadLine());
                                            Console.WriteLine("Enter cricuitAnalysis grade:");
                                            g2 = Convert.ToInt16(Console.ReadLine());
                                            Console.WriteLine("Enter analogCommunication grade:");
                                            g3 = Convert.ToInt16(Console.ReadLine());
                                            ((CommerceStudent)students[i]).SetGrades(g1, g2, g3);
    
                                        }
    
                                    }
                                }
                                else
                                {
    
                                    Console.WriteLine("your branch is not correct,");
                                }
                                break;
                            }
                        case "3":
                            {
                                Console.WriteLine("Enter Student name:");
                                name = Console.ReadLine();
                                Console.WriteLine("Enter Student ID:");
                                id = Convert.ToInt16(Console.ReadLine());
    
                                foreach (Student s in students)
                                {
                                    if ((name == s.Name) && (id == s.RegisterID))
                                    {
                                        s.Print();
                                    }
                                }
                                Console.ReadLine();
    
                                break;
                            }
                        default:
                            {
                                Console.WriteLine("Press e  to Exit application");
                                menu = Console.ReadLine();
                                break;
                            }
    
                    }
    
    
    
                }
    
    
            }
        }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: problematic Main need help to understand how to use the main

    You break up a method by putting blocks of code into additional methods.

    For example, you could make the Main method smaller by pulling out the code in each of the case statements into separate methods...
    Code:
    static List<Student> _students = new List<Student>();  // Make the students list a static class variable
    
    static void Main(string[] args)
    {
      string menu="y";
    
      do
      {  
        menu = PromptForMenu();
    
        switch (menu) // Each case statement calls a method
        {
        case "1":
          AddStudent();  
          break;
        case "2":
          AddStudentGrades(); 
          ...
        }
        while (menu !="e" && menu!="E");
      }
    }
    Here's the PromptForMenu() and AddStudent() methods. Note: for the other methods you might have to pass in variables to the method.
    Code:
    private static string PromptForMenu()
    {
      Console.WriteLine("Student Menu Manu");
      Console.WriteLine("Press 1 to Add Student");
      Console.WriteLine("Press 2 to Add student grades");
      Console.WriteLine("Press 3 to print students marks");
      return Console.ReadLine();
    }
    
    private static void AddStudent( )
    {
      Console.WriteLine("Enter Student name:");
      var  name = Console.ReadLine();
      Console.WriteLine("Enter Student ID:");
      var id = Convert.ToInt16(Console.ReadLine());
    
      Console.WriteLine("Enter Student branch:");
      var  branch = Console.ReadLine();
     
      if (branch == "CSE" || branch == "cse")
      {
        var sCSE = new ScienceStudent(name, id);
        _students.Add(sCSE);
      }
      else if (branch == "ECE" || branch == "ese")
      {
        var sECE = new CommerceStudent(name, id);
        _students.Add(sECE);
      }
      else
      {
        Console.WriteLine("your branch is not correct,");
      }
    }
    Last edited by Arjay; October 7th, 2013 at 07:15 PM.

  3. #3
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: problematic Main need help to understand how to use the main

    Breaking up your main method into multiple methods allows for better organization and the ability to call code multiple times. I would recommend getting into a habit to make your methods public instead of private though, just because in the future you will use public much more often than private.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: problematic Main need help to understand how to use the main

    Quote Originally Posted by Mercifies View Post
    I would recommend getting into a habit to make your methods public instead of private though, just because in the future you will use public much more often than private.
    I respectfully disagree. Make them private, and if you need to call them from another assembly afterwards (or if you know you are going to call them from another assembly or class), only then make them public.

    There are many reasons to do this, but an important one is you want to publically expose the minimum in order for the user of the class to get the job done. Making everything public allows the users to call things that you may not want called and increases the testing of that class or assembly. It's also easier to find out what needs to be made public (i.e. it won't compile); whereas it's harder to go back and make unused class attributes private.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured