none
*URGENT* Why won't it let me use Console.WriteLine() ??????? RRS feed

  • Question

  • My program is not done but can someone please tell me why I can't use Console.WriteLine in the Main class?

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    
    namespace Huffman_Codes
    {
        class Main
        {
            Console.WriteLine("blah blah");
        }
        class Node : IComparable
        {
            public char Character { get; set; }
            public int Frequency { get; set; }
            public Node Left { get; set; }
            public Node Right { get; set; }
    
            public Node (char character, int frequency, Node left, Node right)
            {
                
            }
    
            public int CompareTo (Object obj)
            {
    
            }
        }
    
        class Huffman
        {
            private Node HT; // Huffman tree to create codes and decode text
            private Dictionary<char, string> D; // Dictionary to encode text
            private string S;
    
            // Constructor
            public Huffman (string strings)
            {
                S = strings;
            }
    
            // Return the frequency of each character in the given text (invoked by Huffman)
            private int[] AnalyzeText (string S)
            {
                int[] F = new int[S.Length];
                var characterCount = new Dictionary<char, int>();
                foreach (var ch in S)
                {
                    if (characterCount.ContainsKey(ch))
                        characterCount[ch]++;
                    else
                        characterCount[ch] = 1;
                }
    
                int index = 0;
                foreach(var val in characterCount)
                {
                    F[index] = (val.Value);
                    index++;
                }
    
                return F;
    
               /* foreach (var pair in characterCount)
                {
                    Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
                }
                Console.ReadLine();
               */
            }
    
            // Build a Huffman tree based on the character frequencies greater than 0 (invoked by Huffman)
            private void Build ( int[] F)
            {
                PriorityQueue<Node> PQ;
            }
    
            // Create the code of 0s and 1s for each character by traversing the Huffman tree (invoked by Huffman)
            private void CreateCodes ()
            {
    
            }
    
            // Encode the given text and return a string of 0s and 1s
            public string Encode (string S)
            {
    
            }
    
            // Decode the given string of 0s and 1s and return the original text
            public string Decode (string S)
            {
    
            }
    
          
        }
    
    }
    

    Saturday, November 3, 2018 9:37 PM

All replies

  • You can only write Console.WriteLine inside a method, but you have written it directly inside a class, without adding a method.

    That is, you wrote

    class Main { Console.WriteLine(...); }

    but you need:

    class Something { public static void Main { Console.WriteLine(...); } }

    Note that you named your class "Main". This is legal, but it will cause confusion because we normally use "Main" to name the method that serves as entry point to the program.

    Saturday, November 3, 2018 9:50 PM
    Moderator