Programming Languages Project

Programming Languages Project

I’m currently on the last project for C# and I’m sorta stuck. Will someone help me fix my errors and explain the next step to me? The project link is: https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-programming-languages

Program.cs code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

namespace ProgrammingLanguages

{

class Program

{

static void Main()

{

  List<Language> languages = File.ReadAllLines("./languages.tsv")

    .Skip(1)

    .Select(line => Language.FromTsv(line))

    .ToList();

  foreach (Language language in languages)

  {

    Console.WriteLine(language.Prettify());

    

    var lang = languages.Select(l => );

  }

  

}

}

}

Your “foreach” loop appears to work OK for Exercise #1 in the Project:

foreach (Language language in languages)
{
Console.WriteLine(language.Prettify());
}

Each of the languages and their four elements will be printed out.

Is your question in reference to Exercise #2 where we are supposed to print out the languages by writing a LINQ query?

Hey im running into i think the same issue - having trouble with exercise 2 - this was my solution to the problem, but i dont think thats what it was wanting me to do. - copied the code below.

var langString = languages.Select(s => s.Prettify());

    foreach (var a in langString)

    {

      Console.WriteLine(a);

    }

Update! i think i figured it out :slight_smile: copied code below just in case

var langString = languages.Select(s => $"{s.Year}, {s.Name}, {s.ChiefDeveloper}");

    foreach (var a in langString)

    {

      Console.WriteLine(a);

    }
1 Like

The challenge here was to extract all the items from each line except for the “Predecessors” item.
I ultimately ended up using syntax similar to the expression that you pasted above.

I had no clue that Select can return more than one item at a time or even be formatted like a string until reviewing the Microsoft documentation (Select Clause - C# Reference).

@java1003212033 Did you finish the project? I had real trouble with printing the methods created: PrettyPrintAll() and PrintAll()

Did you manage to use them instead of a foreach loop?

It is not part of the tasks but i wanted to try, but could not get it to work.

Yes; I somehow managed to get all the way down to Exercise #12.
If you could paste the code that you have so far for the PrettyPrintAll() and PrintAll() methods I could compare it to my notes.

#10)
The hint advises to begin with:

public static void PrettyPrintAll(IEnumerable filename)
{
}

One note of caution is that the Visual Studio threw out Error CS0106 “the modifier is not valid for this item”. I honestly don’t know the reason; Therefore, I removed the from the method definition.

At first I was confused about how “IEnumerable filename” could be used as an argument. However, the documentation explains that the data extracted by a LINQ query is considered to be of a type “IEnumerable”. So, we do not need to be concerned if there are multiple data types within a list. Our Language List contains data of both type “int” and type “string”. After extraction, they are of type “IEnumerable”.

The hint advises that we need to use the Prettify() method inside of a “foreach” loop. Is it possible to make the method function without using a “foreach” loop? It may be possible but I could not figure out how to iterate over each element without it.

So, the method should have the general form of:

static void PrettyPrintAll(IEnumerable filename)
{
foreach (T item in filename)
{
Console.WriteLine(item.Prettify());
}
}

Then, the method should be called using an argument that contains all four items (Year, Name, ChiefDeveloper, Predecessors) from each data line.

PrettyPrintAll(languages);

This worked OK for me and printed out all four items from each line of data in the Language List.

#11)
The hint recommends that the first line of the Method is:

public static void PrintAll(IEnumerable sequence)
{
}

(Again, the VS threw an error so I deleted the modifier.)
Initially, I could not figure out why we could not simply re-use the argument from Exercise #10 and then replace the Prettify() method inside the loop with a different WriteLine() statement.
After reviewing the documentation, it appears that we need to use a different argument because after data is extracted by the <.Select> function of the query it is no longer in the “Language” Class. Specifically, data that is initially extracted by the <.Where> function of the query is in the “Language” Class. However, after further processing by the <.Select> function, it is in the “Object” Class. In my case, the Selected data was of a “string” type because I concatenated two data types (int, string) into a single string.

In general form:

static void PrintAll(IEnumerable filename)
{
foreach (Object variable in filename)
{
Console.WriteLine($"{variable}");
}
}

And, to call the method:
PrintAll(SelectedData);

Note that the var SelectedData contains the item that was extracted by the .Select function from the lines that were initially screened by the .Where function in the query.

Finally, verify that you have all four of these headers at the top of your Program.cs file:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Some items inside the carat symbols were erased from the previous post.
Correction:

#10)
The first line of the method:

public static void PrettyPrintAll(IEnumerable<Language> filename)

Error CS0106 warns that “the PUBLIC modifier is not valid for this item”. Therefore, only the STATIC VOID modifiers are used.

#11)
The recommended first line of the method from the hint is:

public static void PrintAll(IEnumerable<Object> sequence)

Again, I deleted the PUBLIC modifier.

Hi this is what i managed to make i did not get a error code from these:

Pretty print all:


public static void PrettyPrintAll(IEnumerable<Language> langs)

{

 foreach (var printsAll in langs)

  {

    Console.WriteLine(printsAll.Prettify());

  }
}

print all:


public static void PrintAll(IEnumerable<Object> sequence)

{

  foreach (var prints in sequence)

  {

    Console.WriteLine(prints);

  }

}

The problem now is that i dont now how to print them i did not manage to.

I had a similar problem earlier when i was trying to do task nr 3, it turned out i was missing part of the syntax.

Here is what i tried first:


var msLangs = languages.Where(lang => ChiefDeveloper.Contains("Microsoft"));

Here is the code that worked:


var msLangs = languages.Where(lang => lang.ChiefDeveloper.Contains("Microsoft"));

I will take a look at our codes and see how they compare but i figured i am better off trying to learn more and then come back to do this project one more time.

All the correct namespaces are added in the project when you start.

Also one final tip, you can format your code in your posts with 4 spaces so your code becomes more readable, i learned this on stackoverflow got roasted when i did not do it correctly lol. i See that you have done it in the second post though.

When i try this i get: error CS0103: The name ‘PrettyPrintAll’ does not exist in the current context [/home/ccuser/workspace/csharp-programming-languages/ProgrammingLanguages.csproj]

Thanks for the tip. I need to learn to use the PreformattedText command in the palette to post code. Hopefully this will be clearer.

#10) PrettyPrintAll()
Your code inside the method works OK… I pasted it into my console and it printed out all of the lines from the Languages list as intended. You mentioned that you get Error CS0103 when you call the method with the “languages” argument. Maybe this is the source of the problem.

Perhaps the method is hidden or embedded inside of another function like an “if” statement or perhaps there are a couple of stray braces left over from another statement? Can you verify that both the method and the callout are contained within the set of braces in Main()?

static void Main()
{
}

Also, both the method and the method callout should be in the Program.cs file (and not in Languages.cs). Sometimes I am not sure if a particular batch of code should be entered into Program.cs or into the associated files.

#11) PrintAll()
Your PrintAll() method also functions OK when I paste it into my console. I am able to print out the data containing the NAME and the YEAR of the oldest languages from the YEAR 1990. The data is Selected using the LINQ Method syntax (2 statements) and then that file is used as the argument when calling the method. Do you also get the Error CS0103 when you call the method?

Yes i do bro, most likely i did something wrong somewhere else.
So what did you decide to do after this project?

Did you try to do the Build Web Apps with ASP.NET?

https://www.codecademy.com/learn/paths/build-web-apps-with-asp-net

It worked when moved to main, but i think this is the incorrect way to do it. The reason why the public modifier is not needed when the methods is moved to main is because when they are in main we dont access them from somewhere else so they dont need an access modifier.

Most likely there is another way to print them but i am to afraid to ask in stackoverflow the people there can be pretty harsh to beginners.

edit:

I asked in stackoverflow and got the solution (Oh my oh my i feel really stupid now lol).
With the methods in Language.cs you would use this syntax to call the method PrettyPrintAll:

var langScripts = languages

        .Where(language => language.Name.Contains("Script"));

        Language.PrettyPrintAll(langScripts);

To print out PrintAll:

var stringLanguage = languages.Select(languagePrint => $"{languagePrint.Year} {languagePrint.Name} {languagePrint.ChiefDeveloper}");

Language.PrintAll(stringLanguage );

Now you can have the methods in Language.cs with the public modifier.

I am in the process of wrapping up the “Console Game” project and attempting to decipher the code that capture key strokes so I can make my own changes. Stuck on that.

You solved the mystery! No doubt I need to go back and move those methods over into Games.cs. That also resolves the Error related to the PUBLIC modifier. That was kind of bothering me. Makes sense now I suppose.

I’m also still learning how to navigate around the Visual Studio.

Cool

Let me know if you need help, or advice.

Very good; thanks! For now I’ll start messing around with the basic Console.Readkey() and hopefully move forward from there.

Confirming that I moved the methods over to Languages.cs as you suggested and that everything prints out after calling them in Program.cs using the syntax that you entered above.

At first, it threw Error CS0246: “The namespace “IEnumerable” could not be found.” This is resolved by adding this USING statement to Languages.cs:

using System.Collections.Generic;

(It is already in Program.cs)
Just a heads-up for the benefit of anyone who may get the same error.

to use the public modifier, you have to define the method inside of program.cs but OUTSIDE of main (right after the main closing curly brace). that’s what i did and it worked for me.

you can see it here in the method section of the course:

https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-method-calls-and-input/exercises/define-a-method

to use the public modifier, you have to define the method inside of program.cs but OUTSIDE of main (right after the main closing curly brace). that’s what i did and it worked for me.

you can see it here in the method section of the course:

https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-method-calls-and-input/exercises/define-a-method

For the #11 You will also need to add the Select operator to the objects on number 3, 4 and 5 to have what they asked printed out to the console or you will just end up having: ProgrammingLanguages.Language printed.

I got my code sorted up until step 11.

My code looks like this for it:

public static void PrintAll(IEnumerable<Object> sequence) {
      foreach (Object o in sequence) {
        Console.WriteLine(o);
      }
    }

And in the main method I called it with:

PrintAll(languages);

All it’s spitting out is a bunch of text saying “ProgrammingLanguages.Language”.

I’m not sure if this is right and what they want.
The rest of my functions and loops work fine.

Is there anyone who knows what to do there? Been puzzling me for works since finishing C#.