Home > Back-end >  How to pass data to list?
How to pass data to list?

Time:09-29

I'm sorry for the beginner question. I'm having a hard time figuring out why I can't pass data taken from the Console.Readline and pass it to my List.

I'm trying to get better at coding, but I have a tendency to overcomplicate things a bit.

Is what I'm doing here completely dumb?

using System;
using System.Collections.Generic;

namespace BB1
{
    public class Client
    {
        public string Telefon { get; set; }
        public string Navn { get; set; }
        public string Addresse { get; set; }
        public int Postnummer { get; set; }
        public string By { get; set; }
        public string Email { get; set; }

        List<string> client = new List<string>();

        public void AddToList()
        {

            client.Add("Navn"   Navn   "Telefon"   Telefon);
        }
        public static void Main(string[] args)
        {



            var client = new Client();
            int tl;

            Console.WriteLine("Indtast dit navn her");
            client.Navn = Console.ReadLine();

            Console.Write("Indtast dit TLF nummer: ");
            client.Telefon = Console.ReadLine();

            bool success = int.TryParse(client.Telefon, out tl);

            while (!success)

            {
                Console.WriteLine("Du har indtastet et ugyldigt nummer");
                client.Telefon = Console.ReadLine();
                success = int.TryParse(client.Telefon, out tl);

            }

            Console.WriteLine("TEST to see if client list is updated");
            Console.ReadKey();
            Console.WriteLine(client);
            Console.ReadKey();
        }```


**NEW CODE**
    List<Client> client = new List<Client>();
    public override string ToString()
    { return "Client: "   Navn   " "   Telefon; }


    public static void Main(string[] args)
    {

        var client = new Client();


        int tl;

        Console.WriteLine("Indtast dit navn her");
        client.Navn = Console.ReadLine();

        Console.Write("Indtast dit TLF nummer: ");
        client.Telefon = client.Add(Console.ReadLine());```

It gives me the error 'Client' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'Client' could be found (are you missing a using directive or an assembly reference

How do I solve this (sorry I'm getting a bit tired, been sitting whith this all day).

CodePudding user response:

First add class Client in separated .cs file.

public class Client
{
    public string Telefon { get; set; }
    public string Navn { get; set; }
    public string Addresse { get; set; }
    public int Postnummer { get; set; }
    public string By { get; set; }
    public string Email { get; set; }

    public override string ToString()
    {
        return $"Navn: {Telefon}, Telefon: {Navn}, Addresse: {Addresse}, Postnummer: {Postnummer}, By: {By}, Email: {Email}";
    }
}

Then in Program.cs file add static client list List clients (see my code belowe) which you will fill after you enter client data.

class Program
{
    static List<Client> clients = new List<Client>();

    static void Main(string[] args)
    {
        var client = new Client();
        int tl;

        Console.WriteLine("Indtast dit navn her");
        client.Navn = Console.ReadLine();

        Console.Write("Indtast dit TLF nummer: ");
        client.Telefon = Console.ReadLine();

        bool success = int.TryParse(client.Telefon, out tl);

        while (!success)
        {
            Console.WriteLine("Du har indtastet et ugyldigt nummer");
            client.Telefon = Console.ReadLine();
            success = int.TryParse(client.Telefon, out tl);
        }

        clients.Add(client);

        Console.WriteLine("TEST to see if client list is updated");
        Console.WriteLine(client);

        Console.WriteLine("Print all clients in list");
        foreach (var c in clients)
        {
            Console.WriteLine(c.ToString());
        }

        Console.ReadLine();
    }
}

In your code you defined client list as string list, if you want to add string instead of client object inside client list, you just need add this line of code after you populate data in client object.

clients.Add($"Navn: {client.Navn}, Telefon: {client.Telefon}");

And dont forget to call client list as clients, if you want your code to work.

List<string> client = new List<string>();

CodePudding user response:

You should override the ToString() method in your Client class.

  • Related