2
I was wondering how I can reuse the object I put into an array list because I can’t compare it to anything, and how I compare an object inside it to another, in this college program the goal was for the user to put the number of ways he wanted and put his characteristics so that I could print everything, and I’m having difficulty precisely in printing because I can’t compare the square object or rectangle or circle with Listaformas[x], as I normally did with an array, nor can I remove any object from Listaformas to be able to work on it.
static void Main(string[] args)
{
ArrayList ListaFormas = new ArrayList();
Quadrado quadrado = new Quadrado();
Retangulo retangulo = new Retangulo();
Circulo circulo = new Circulo();
Console.WriteLine("Quantas Formas deseja criar?(quadrado,retangulo ou circulo:");
int quantidade = int.Parse(Console.ReadLine());
for(int x = 0; x < quantidade; x++)
{
Console.WriteLine("Qual forma deseja criar?:");
string nome = Console.ReadLine();
if(nome == "quadrado" || nome == "Quadrado")
{
Console.WriteLine("Entre com o Lado do quadrado:");
double lado = double.Parse(Console.ReadLine());
quadrado.Lado = lado;
ListaFormas.Add(quadrado);
}
else if(nome =="retangulo" || nome == "Retangulo")
{
Console.WriteLine("Entre com o primeiro lado:");
double lado1 = double.Parse(Console.ReadLine());
Console.WriteLine("Entre com o segundo lado:");
double lado2 = double.Parse(Console.ReadLine());
retangulo.Lado1 = lado1;
retangulo.Lado2 = lado2;
ListaFormas.Add(retangulo);
}
else if(nome == "circulo" || nome == "Circulo")
{
Console.WriteLine("Entre com o raio do circulo:");
double raio = double.Parse(Console.ReadLine());
circulo.Raio = raio;
ListaFormas.Add(circulo);
}
else
{
Console.WriteLine("Erro opcao invalida");
}
}
}
}
}
Compare what? Without a clear definition of the problem there is no way to find a solution. Only this section already see several problems. You can put the definitions of the 3 classes of forms to help us improve your code?
– Maniero
@Maniero "(...)because I can’t compare the square or rectangular object or circle with Listaformas[x], as I normally did with an array, nor can I remove from Listaformas any object to be able to work on it.(...)".
– LP. Gonçalves
@LP.Gonçalves that doesn’t mean anything.
– Maniero
See in the documentation the methods and properties that class Arraylist makes available.
– ramaral
@Maniero if I didn’t get it wrong, I believe he’d like to know if
ListaFormas[x]
is aQuadrado
,Retangulo
orCirculo
. I might be wrong.– LP. Gonçalves
@LP.Gonçalves may be, but nothing indicates clearly that this is it.
– Maniero
and I even want to store the objects in an arraylist and compare them later to see if it is square rectangle or circle, then print the information of each one
– Arthur Henrique
@Arthurhenrique you want a solution based on your question or you want to learn to do it the right way?
– Maniero