People, good evening, I have this piece of code that, in summary, with an array of size 2, that condition works great, but if I give the "real estate" array more than 3 in size, then I get the error- what I want to do is verify that a new key is not equal to a previously entered one "Check()" takes care of that as additional data, the error pops up (for example with 4 spaces) when I add a new key in index 1. I am not using get, set, this, is that why? I am new to POO at most I have invested 48hrs. Labels(); is nothing more than WritLine's saying that the key already exists, 1 ReadKey() and 1 Clear();
Class CApartamento
{
public string Clave;
...;
public CApartamento[] inmobiliaria;
Console.Write("Cuantos Registros?: ")
tam = Convert.ToInt32(Console.ReadLine());
inmobiliaria = new CApartamento[tam];
for(int i=0, j=1; inmobiliaria.Length; i++)
{
inmobiliaria[i] = new CAparamento();
Console.Write("CLAVE [A-###]: ");
//inmobiliaria[i].clave = Console.ReadLine();
temporary_key = Console.ReadLine();
if (j == i)
{
do
{
if(Check(inmobiliaria, temporary_key))
{
///me lo recalca aca arriba tambien(leer
///mas abajo xD)
Rotulos();
existe = true;
Console.WriteLine(" .------------------.");
Console.WriteLine(" + INGRESANDO DATOS +");
Console.WriteLine(" .------------------.");
Console.WriteLine("\n");
Console.Write("CLAVE [A-###]: ");
temporary_key = Console.ReadLine();
}
else
{
j++;
existe = false;
}
} while (existe != false);
inmobiliaria[i] = temporary_key;
}
else
{
inmobiliaria[i] = temporary_key;
}
}
}
private bool Check(CApartamento[] inmobiliaria, string temporary_key)
{
bool existe = true;
for(int i=0; i<inmobiliaria.Length;)
{
if(inmobiliaria[i].clave != null && inmobiliaria[i].clave.CompareTo(temporary_key) == 0)
{
///aca se genera un error
///cuando quiere buscar en más de 2 posiciones, y me
///lo recalca arriba el error
existe = true;
break;
}
else
{
existe = false;
}
}
return existe;
}
I add a functional code; I have edited it in a project changing certain things in your code. I have added as a class
CApartamento
, which will serve as a model to organize the data. Your example has several bugs that have already been mentioned in other answers, as well as in my comment on your question. One of the main ones, which I think you're asking your question about, is that when you initialize the arrayinmobiliaria
and send it to your functionCheck
, you don't check if the position has already been initialized: If your array contains 5 values, but you're at position 0, the following 1, 2, 3 and 4 have not been initialized yet so it will give you an error. However, and as you mention that you are learning, you could take a look at the use of lists , which in your case, would be the option that I would choose.Your code may be generating the error, because you have instantiated the variable
inmobiliaria
with a capacity, and you have not assigned values for each of its components, when you refer to one of its components and it is null, it throws the error, but also your methodCheck
contains another obvious error and that is that you declare and assign a valuetrue
to the variableexiste
and then within the blockif
you assign a new value to ittrue
as a result, your methodCheck
will always returntrue
whatever the result of the conditional isif
. With the following code, none of those errors will occur.The fault may be in the first three lines of code:
What you read from the console you already try to save in real estate[i].key and I think it should be like this:
After having the data entered in temporary_key is when you check if said element already exists in your real estate[] array. Please note Eduardo Reyes's corrections in the Check function.
I hope I have been of help although you have only shown part of the code, we do not know where that i comes from, nor its value nor what function the conditional j==i has.
Okay; understanding a little more what you want I think it's a matter of organization. I would do it with List< T > and I would pose it like this:
Then in your program body or method in charge we put:
The fact of using List allows us to perform operations such as dynamically adding or deleting elements from the list, ordering them... at the same time that it allows us to simplify the code and add readability to it.