In: Computer Science
Start by creating a new Visual Studio solution, but chose a “class library .Net Core” as the project type.
It will create a file and a class definition in that file. Rename the file to be ToDo.cs and accept the suggestion which will also rename that class to be
Class Todo
{
}
In that class, create
- a string property called Title
- an int property called Priority
- a bool property called Complete
Also, define one Constructor that takes in one string and one int.
In the constructor, use the passed in string to set the Title property and the passed in int to set the Priority property.
Also, set the Complete property to be false.
You are done with that class definition.
Now click on the Solution and add a new project, a .Net Core console project.
In the Main method,
[1] Define an array of type ToDo and size 3. The compiler will complain it does not know what a ToDo is. You need to add a reference from the Console Project to the Class Library. (see any of those 3 descriptions I listed above for how to do this. Don’t try going further until the compiler is happy with your array definition.)
[2] Ask the user to enter a ToDo title (like ”wash the car” or “buy groceries”
And also ask them for a priority which should be a 1, 2, or a 3.
Create a new ToDo object by calling the constructor and passing in these 2 user values.
Add this new ToDo object into your array at [0].
Repeat that code 2 more times so that the array has 3 ToDo objects in it.
Now build a for loop that writes out one line for each array element, using the 3 properties that each item has.
Your output should look something like
Wash the car Priorty:2 Completed: false
Buy groceries: Priorty 3 Completed: false
Do Prog120 Homework: Priority 1 Completed: false
Make sure the “false” comes from the property, don’t just hard code it in your for loop.
//Code in visual Studio using c#
=> Create a .net core console project => todo_project1
=> Create following code after renaming class Program to ToDo
using System;
namespace todo_project1
{
public class ToDo
{
public string Title { get; set; }
public int Priority { get; set; }
public bool Complete { get; set; }
//Constructor
public ToDo(string title, int priority)
{
Title = title;
Priority = priority;
Complete = false;
}
public static void Main(string[]args)
{
}
}
}
//Now go to Solution Explorer and Add another Solution => todo_project2
=> Add the project dependencies
=> right click on second project => Add=> Project Reference
//Now Write the following code
using System;
using todo_project1;
namespace todo_project2
{
class Program
{
static void Main(string[] args)
{
// Define an array of type ToDo and size 3
ToDo[] toDos = new ToDo[3];
for (int i = 0; i < toDos.Length; i++)
{
Console.Write("Enter a ToDo title: ");
string title = Console.ReadLine();
Console.Write("Enter the priority: ");
int priority = int.Parse(Console.ReadLine());
ToDo todo = new ToDo(title, priority);
toDos[i] = todo;
Console.WriteLine();
}
//output
for (int i = 0; i < toDos.Length; i++)
{
Console.WriteLine("{0} Priority: {1} Completed: {2}", toDos[i].Title, toDos[i].Priority, toDos[i].Complete);
}
//pause
Console.WriteLine("Enter any key to terminate....");
Console.ReadLine();
}
}
}
//Right Click => Select Property
// Output
//If you need any help regarding this solution...... please leave a comment..... thanks