none
Trouble Organizing Programs and Folders RRS feed

  • Question

  • I am taking a beginner C# course. I created a new folder for this class, separate from main program.  Maybe something is needed in the main? I had to add the console using statement on the top and it isn't writing to console (see bold)Not sure how to proceed.  Any help is really appreciated!  Thanks!!

    using System;
    using System.Collections.Generic;
    using static System.Console;
    
    namespace CSharp1Exercises.ArraysAndLists
    {
        public class Lists
        {
            /// <summary>
            /// Write a program and continuously ask the user to enter different names, until the user presses Enter 
            /// (without supplying a name). Depending on the number of names provided, display a message based on the above pattern.
            /// </summary>
            public void Exercise1()
            {
                var names = new List<string>();
    
                while (true)
                {
                    Console.WriteLine("Type a name (or hit ENTER to quit): ");
    
                    var input = ReadLine();
                    if (String.IsNullOrWhiteSpace(input))
                        break;
                    names.Add(input);
                }
    
                if (names.Count > 2)
                    WriteLine("{0}, {1} and {2} others like your post", names[0], names[1], names.Count - 2);
                else if (names.Count == 2)
                    WriteLine("{0} and {1} like your post", names[0], names[1]);
                else if (names.Count == 1)
                    WriteLine("{0} likes your post.", names[0]);
                else
                    WriteLine();
            }
        }
    }




    Thursday, May 10, 2018 2:46 AM

Answers

  • Sorry to ask an "are you sure it's plugged in" type of question, but are you calling the Exercise1() method in Main (or anywhere else)?

    Something like so.

    Lists lists = new Lists();
    lists.Exercise1();

    And you only need that "using static System.Console" if you want to not qualify your calls to WriteLine. That is, if you always use Console.WriteLine() instead of just WriteLine, the using directive is not needed (and the same deal for other Console methods like ReadLine()).

    Thursday, May 10, 2018 3:18 AM
  • The code in Main doesn't use any classes from any of the namespaces that are not highlighted. This means that including the using statements for those namespaces doesn't get you any advantage, but it doesn't hurt either.

    You have to include the using statements in all files (not classes, but the files those classes are in) where you want to use classes from those namespaces without qualifying them (without putting the namespace in front). For example, by "using System" you can call Console.Writeline rather than System.Console.WriteLine() (less typing).

    Thursday, May 10, 2018 4:58 AM

All replies

  • Sorry to ask an "are you sure it's plugged in" type of question, but are you calling the Exercise1() method in Main (or anywhere else)?

    Something like so.

    Lists lists = new Lists();
    lists.Exercise1();

    And you only need that "using static System.Console" if you want to not qualify your calls to WriteLine. That is, if you always use Console.WriteLine() instead of just WriteLine, the using directive is not needed (and the same deal for other Console methods like ReadLine()).

    Thursday, May 10, 2018 3:18 AM
  • I have been fumbling with the Main program.cs  I don't know what to put there.  When I add your code to that  it works. But only one namespace is highlighted in Main.

    I would have thought the appropriate namespaces would be highlighted in Main and not necessary again in the Lists class? 

    using CSharp1Exercises.ControlFlow;
    using CSharp1Exercises.ArraysAndLists;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CSharp1Exercises
    {
        public class Program
        {
            static void Main(string[] args)
            {
                var lists = new Lists();
                lists.Exercise1();
            }
        }
    }


    Thursday, May 10, 2018 3:34 AM
  • Thanks you for your help :)
    Thursday, May 10, 2018 3:44 AM
  • The code in Main doesn't use any classes from any of the namespaces that are not highlighted. This means that including the using statements for those namespaces doesn't get you any advantage, but it doesn't hurt either.

    You have to include the using statements in all files (not classes, but the files those classes are in) where you want to use classes from those namespaces without qualifying them (without putting the namespace in front). For example, by "using System" you can call Console.Writeline rather than System.Console.WriteLine() (less typing).

    Thursday, May 10, 2018 4:58 AM
  • Thanks.

    Thursday, May 10, 2018 7:27 AM