Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: ismaelson en 24 Marzo 2021, 13:51 pm



Título: convertir implícitamente una lista c#
Publicado por: ismaelson en 24 Marzo 2021, 13:51 pm
buenas estoy aprendiendo c# aqui el motivo de mi pregunta estoy haciendo un ejercicio de herencias  que trata de hacer una tienda con ciertos articulos y que se puedan comprar pero mi problema es que no consigo llamar a una lista la e intentado llamar de distintas formas pero no lo consigo siempre me dice lo mismo que necesita una conversion implicita, pero por mas que miro en la documentacion no encuentro algo parecido, dejo el codigo para que puedan echarle un ojo

necesitaria saber como hago esa conversion implicita y asi poder aprender de ello para que no me vuelva a ocurrir gracias, un saludo!


Código
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using static ejerciciopracticotema3.Program;
  6.  
  7. namespace ejerciciopracticotema3
  8. {
  9.    public class Program
  10.    {
  11.  
  12.        public DefinicionGeneral[] inventarioTienda;
  13.  
  14.        public float aPagar
  15.        {
  16.            get; set;
  17.        }
  18.        public float dinero
  19.        {
  20.            get; set;
  21.        }
  22.  
  23.  
  24.  
  25.  
  26.        public class Naranjas : DefinicionGeneral, IAPeso
  27.        {
  28.            public float precioPorKg
  29.            {
  30.              get; set;
  31.            }
  32.            public bool aPeso
  33.            {
  34.                get; set;
  35.            }
  36.  
  37.  
  38.            public void SetVarNaranjas()
  39.            {
  40.  
  41.                precioPorKg = 0.75f;
  42.                aPeso = true;
  43.                SetNameId("Naranjas", precioPorKg,0.75f);
  44.  
  45.            }
  46.            public Naranjas(float x, bool b) { precioPorKg = x; aPeso = b; }
  47.  
  48.        }
  49.  
  50.        public class Berenjenas : DefinicionGeneral, IAPeso
  51.        {
  52.  
  53.  
  54.            public float precioPorKg
  55.            {
  56.                get; set;
  57.            }
  58.            public bool aPeso
  59.            {
  60.                get; set;
  61.            }
  62.  
  63.            public void SetVarBerenjenas()
  64.            {
  65.                precioPorKg = 0.85f;
  66.                aPeso = true;
  67.                SetNameId("Berenjenas", precioPorKg, 0.85f);
  68.            }
  69.  
  70.            public Berenjenas(float x, bool b) { precioPorKg = x; aPeso = b; }
  71.  
  72.        }
  73.  
  74.        public class Pelota : DefinicionGeneral,IUnitario
  75.        {
  76.            public float precioPorItem
  77.            {
  78.  
  79.                get; set;
  80.  
  81.            }
  82.            public int cantidad
  83.            {
  84.  
  85.                get; set;
  86.  
  87.  
  88.            }
  89.            public void SetVarPelota()
  90.            {
  91.                precioPorItem = 1.60f;
  92.                cantidad = 1;
  93.                SetNameId("Pelota", 1, 1.60f);
  94.  
  95.            }
  96.  
  97.            public Pelota(float x, int c) { precioPorItem = x; cantidad = c; }
  98.  
  99.        }
  100.  
  101.        public class Yatekomo : DefinicionGeneral, IUnitario
  102.        {
  103.  
  104.            public float precioPorItem
  105.            {
  106.                get; set;
  107.            }
  108.  
  109.            public int cantidad
  110.            {
  111.                get; set;
  112.            }
  113.  
  114.            public void SetVarYatekomo()
  115.            {
  116.                precioPorItem = 2.20f;
  117.                cantidad = 1;
  118.                SetNameId("Yatekomo", 1, 2.20f);
  119.  
  120.            }
  121.           public Yatekomo(float x, int c) { precioPorItem = x; cantidad = c; }
  122.        }
  123.  
  124.  
  125.         static void Main(string[] args)
  126.         {
  127.            Program p = new Program();
  128.  
  129.  
  130.            //p.inventarioTienda =
  131.            p.inventarioTienda = Inventario.InventarioTienda(); // aqui me tira el error aunque imagino que sera por algo que esta mal en la lista
  132.  
  133.  
  134.            Console.WriteLine("Bienvenidos a tiendas paqui");
  135.            Console.WriteLine(" ");
  136.            Console.WriteLine("tenemos todo esto disponible en tienda: ");
  137.            foreach(DefinicionGeneral dG in p.inventarioTienda)
  138.            {
  139.                Console.WriteLine( dG.name + " Tiene un precio de "+ dG.precio);
  140.            }
  141.  
  142.            Console.WriteLine(" ");
  143.            Console.WriteLine("Que cantidad de dinero tienes disponible? ");
  144.            p.dinero = int.Parse(Console.ReadLine());
  145.            Console.WriteLine("Perfecto tienes disponible para gastar: " + p.dinero + " Euros");
  146.  
  147.            Console.WriteLine(" ");
  148.            Console.WriteLine("elige que Kg deseas comprar: ");
  149.  
  150.  
  151.  
  152.        }
  153.    }
  154.    public static class Inventario
  155.    {
  156.  
  157.  
  158.        public static List<DefinicionGeneral> inventarioDeLaTienda;
  159.  
  160.  
  161.  
  162.        public static List<DefinicionGeneral> InventarioTienda() // esta es la lista que tengo que llamar
  163.        {
  164.  
  165.            return   new List<DefinicionGeneral>()
  166.             {
  167.                new Naranjas(0.75f, true),
  168.                new Berenjenas(0.85f, true),
  169.                new Pelota(1.60f, 1),
  170.                new Yatekomo(2.20f, 1),
  171.             };
  172.  
  173.  
  174.        }
  175.  
  176.  
  177.    }
  178.  
  179.    public abstract class DefinicionGeneral
  180.    {
  181.  
  182.  
  183.  
  184.        public string name { get; set; } //nombre del producto
  185.  
  186.        public float iD { get; set; } // id del producto
  187.        public float precio { get; set; }
  188.  
  189.        public virtual void SetNameId(string s, float i, float ip)
  190.        {
  191.  
  192.  
  193.            name = s;
  194.            iD = i;
  195.            precio = ip;
  196.  
  197.        }
  198.  
  199.    }
  200.    interface IAPeso// interfaz que define si el objeto se pesa o no
  201.    {
  202.  
  203.        public float precioPorKg
  204.        {
  205.            get; set;
  206.        }
  207.  
  208.        public bool aPeso
  209.        {
  210.            get; set;
  211.        }
  212.  
  213.    }
  214.    interface IUnitario
  215.    {
  216.        public float precioPorItem
  217.        {
  218.            get; set;
  219.        }
  220.  
  221.        public int cantidad
  222.        {
  223.            get; set;
  224.        }
  225.    }
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. }


Título: Re: convertir implícitamente una lista c#
Publicado por: K-YreX en 24 Marzo 2021, 20:14 pm
Tu problema se puede resumir en:
Código
  1. int[] arrayNumeros;
  2. List<int> listaNumeros;
  3. arrayNumeros = listaNumeros; // Error!! Se esta asignando una List<int> a un int[]
  4. arrayNumeros = listaNumeros.ToArray(); // OK
  5. listaNumeros = arrayNumeros; // Error!! Se esta asignando un int[] a una List<int>
  6. listaNumeros = arrayNumeros.ToList(); // OK