2. Articles - проблем с извикването на ToString()
Моля за помощ отностно извикването на ToString() метода. Изписва следното: "Redundant 'ToString()' Call" (44 ред). В лекцията методът работи, но не и при мен, че дори и да използвам отделна променлива преди да печатам резултата. Може отново проблема да е заради MonoDevelop.
2. Articles
Create a class Article with the following properties:
Title – a string
Content – a string
Author – a string
The class should have a constructor and the following methods:
Edit (new content) – change the old content with the new one
ChangeAuthor (new author) – change the author
Rename (new title) – change the title of the article
Override the ToString method – print the article in the following format:
{title} - {content}: {autor};
Write a program that reads an article in the following format "{title}, {content}, {author}". On the next line,
you will receive a number n, representing the number of commands, which will follow after it. On the next n lines,
you will be receiving the following commands: "Edit: {new content}"; "ChangeAuthor: {new author}";
"Rename: {new title}". At the end, print the final state of the article.
Input
some title, some content, some author
3
Edit: better content
ChangeAuthor: better author
Rename: better title
Output
better title - better content: better author
using System;
using System.Linq;
using System.Collections.Generic;
namespace ObjectsAndClasses
{
class MainClass
{
public static void Main()
{
string[] input = Console.ReadLine().Split(", ".ToCharArray()).ToArray();
int n = int.Parse(Console.ReadLine());
Article article = new Article();
article.Title = input[0];
article.Content = input[1];
article.Author = input[2];
for (int i = 0; i < n; i++)
{
string[] command = Console.ReadLine().Split(": ".ToCharArray()).ToArray();
if (command[0] == "Edit")
{
article.Edit(command[1]);
}
else if (command[0] == "ChangeAuthor")
{
article.ChangeAuthor(command[1]);
}
else if (command[0] == "Rename")
{
article.Rename(command[1]);
}
}
Console.WriteLine(article.ToString());
}
public class Article
{
public object Title { get; set; }
public object Content { get; set; }
public object Author { get; set; }
public void Edit(string newContent)
{
Content = newContent;
}
public void ChangeAuthor(string newAuthor)
{
Author = newAuthor;
}
public void Rename(string newTitle)
{
Title = newTitle;
}
public override string ToString()
{
return $"{Title} - {Content}: {Author}";
}
}
}
}
Hello Axiomatik,
thank you for your quick responce.
I'am worry, because on my MonoDevelop does not work: "StringSplitOptions.RemoveEmptyEntries" and "Console.WriteLine(article.ToString());". ToString() metods is with white blue color.
At least I know the logic is correct. Thank you!
All the best!
Elena
If you are running VS for Mac, then there have been some recent updates (around 2 weeks ago) for VS which don't allow running the program below Mac OS 13 anymore. If you are using an older version of Mac OS you should at least update to Mac OS 13 or 14 (High Sierra, Mojave), but not 15 (Catalana) if your Mac is older than 2018. After updating your Mac OS, try updating VS and everything should be working fine.
Best,
Hi Axiomatik ,
I'am running Monodevelop on Ubunto. I have updated a month ago, but I will try again. Thanks for your advice!
I think I will run VS on Windows 10 soon for C# Advanced module.