C Visual Code I am gettin an error in my code it says The na
C# Visual Code
I am gettin an error in my code it says \"The name \"Courses\" does not exist in the current context\". Can someone help find whats wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CGAsn1.cgClasses
{
class Course
{
int CourseID;
string CourseCode;
string Title;
int CreditHour;
string DeptCode;
public Course(int id, string code, string t, int h, string d)
{
CourseID = id;
CourseCode = code;
Title = t;
CreditHour = h;
DeptCode = d;
}
//Override the Tostring method
public override string ToString()
{
string CourseDesc =
String.Format(\"\\t{0} \\t{1} \\t{2} \\t{3} \\t{4}\", CourseID, CourseCode, Title, CreditHour, DeptCode);
return CourseDesc;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(\"Asn1 Part1 Task2: Testing the Course Class\ \");
Console.WriteLine(\"These courses are:\ \");
Course c1 = new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\");
Console.WriteLine(c1.ToString());
{
Course c2 = new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\");
Console.WriteLine(c2.ToString());
{
//==============================================================
// Part 2 starts here
// ==========================================================
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part 2 starts here\ \");
List<Course> Courses = new List<Course>()
{
new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\"),
new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\"),
new Course(3, \"INFS3000\", \"Intro to Programming\", 3, \"INFS\"),
new Course(4, \"INFS3700\", \"Database System\", 4, \"INFS\"),
new Course(5, \"INFS4010\", \"System Analysis\", 6, \"IFNS\"),
new Course(6, \"MKTG1000\", \"Intro to Marketing\", 3, \"MKTG\"),
new Course(7, \"MKTG2000\", \"Transportation Theory\", 4, \"MKTG\"),
};
Console.WriteLine(\"Part 2 Task2\");
Console.WriteLine(\"The courses in the List are:\");
foreach (Course h in Courses)
{
Console.WriteLine(h.ToString());
}
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part2 Task3\");
Console.WriteLine(\"Create a sublist of courses for DeptCode MKTG from Courses:\");
// Run a \"LINQ Where\" query for DeptCode \"MKTG\" on Courses and store the result in a List object.
// Name the sublist as MktgCourse
}
List<Course> SortedCoursesByFluent = Courses
.Where(x => x.DeptCode)
.GroupBy(x => x.MKTG)
.ToList();
Console.WriteLine(\"\\The course in MKTG are\");
foreach (Course h in SortedCoursesByFluent)
{
Console.WriteLine(h.ToString());
}
Console.ReadLine();
}
}
}
}
Name the sublist as MktgCourses The objective of this task is to run a LINQ query with Where method for extracting the course objects contained in Courses list for all courses offered by De \"MKTG\", and save the results in a sub list named MktgCourses. Do not assume that you already know the MKTG couses. You must run the appropriate LINQ query. Do not try to do it in some other ways. After you have run the query and saved the result in another list (say, MktgCourses), enter the following code Display the courses in the MktgCourses list: Console Write Line courses in the MktgCourses are Now use a foreach loop to display the courses in the MktgCourses.Solution
Explanation : This type of error occurs when you use a Variable outside the scope of its definition.
In your code, please find the two comments that have added.
1. // COMMENT 1
2.// COMMENT 2
These are the two curly brackets that needs to be commented.
Edited code :
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CGAsn1.cgClasses
{
class Course
{
int CourseID;
string CourseCode;
string Title;
int CreditHour;
string DeptCode;
public Course(int id, string code, string t, int h, string d)
{
CourseID = id;
CourseCode = code;
Title = t;
CreditHour = h;
DeptCode = d;
}
//Override the Tostring method
public override string ToString()
{
string CourseDesc =
String.Format(\"t{0} t{1} t{2} t{3} t{4}\", CourseID, CourseCode, Title, CreditHour, DeptCode);
return CourseDesc;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(\"Asn1 Part1 Task2: Testing the Course Classn\");
Console.WriteLine(\"These courses are:n\");
Course c1 = new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\");
Console.WriteLine(c1.ToString());
{
Course c2 = new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\");
Console.WriteLine(c2.ToString());
// COMMENT 1 {
//==============================================================
// Part 2 starts here
// ==========================================================
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part 2 starts heren\");
List<Course> Courses = new List<Course>()
{
new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\"),
new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\"),
new Course(3, \"INFS3000\", \"Intro to Programming\", 3, \"INFS\"),
new Course(4, \"INFS3700\", \"Database System\", 4, \"INFS\"),
new Course(5, \"INFS4010\", \"System Analysis\", 6, \"IFNS\"),
new Course(6, \"MKTG1000\", \"Intro to Marketing\", 3, \"MKTG\"),
new Course(7, \"MKTG2000\", \"Transportation Theory\", 4, \"MKTG\"),
};
Console.WriteLine(\"Part 2 Task2\");
Console.WriteLine(\"The courses in the List are:\");
foreach (Course h in Courses)
{
Console.WriteLine(h.ToString());
}
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part2 Task3\");
Console.WriteLine(\"Create a sublist of courses for DeptCode MKTG from Courses:\");
// Run a \"LINQ Where\" query for DeptCode \"MKTG\" on Courses and store the result in a List object.
// Name the sublist as MktgCourse
// COMMENT 2 }
List<Course> SortedCoursesByFluent = Courses
.Where(x => x.DeptCode)
.GroupBy(x => x.MKTG)
.ToList();
Console.WriteLine(\"The course in MKTG are\");
foreach (Course h in SortedCoursesByFluent)
{
Console.WriteLine(h.ToString());
}
Console.ReadLine();
}
}
}
}




