A
A
ANYJT2021-11-24 17:00:34
C++ / C#
ANYJT, 2021-11-24 17:00:34

Error CS0029 implicitly convert string type to Consoleapp.programm.info?

I have error CS0029 implicitly convert string type to ConsoleApp.Program.info here is the code,
please help!

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {

            info name = new();
            Console.WriteLine("Hello , write your name.");
            name = Console.ReadLine();
            Console.WriteLine($"Name: {name} ");
            Console.WriteLine("Write name of your civilization");
            namecivil.info = Console.ReadLine();
            Console.WriteLine($"Civilization name - {namecivil} ");
        }
    class info
        {
            string name = "";
            string namecivil = "";
            int people = 0;
            int army = 0;
            double economy = 1.00;
            double money = 5.00;
        }     
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-24
@ANYJT

1. Replace private fields with public properties
2. Give normal names to everything.
Classes must be named with a capital letter.
Public properties must be capitalized.
3. Learn basic syntax.
How are you trying to write a value to the undeclared name variable?
Where should civilName be stored if you don't have a namecivil variable, and even more so if it doesn't have an info field?
4.

using System;


Console.WriteLine("Hello , write your name.");
var name = Console.ReadLine();
Console.WriteLine($"Name: {name} ");
Console.WriteLine("Write name of your civilization");
var civilzationName = Console.ReadLine();
Console.WriteLine($"Civilization name - {civilzationName} ");
var info = new Info
{
    Name = name,
    CivilizationName = civilzationName
};
Console.WriteLine(info);

record Info
{
    public string Name { get; init; } = "";
    public string CivilizationName { get; init; } = "";
    public int People { get; init; }
    public int Army { get; init; }
    public double Economy { get; init; } = 1.00;
    public double Money { get; init; } = 5.00;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question