✅ I'm having issues with "MostrarEstatisticasPorLocal/Sensor/Tipo" in Menu.cs

it was suposed to give me the information that is on the "Leitura" list, but for some reason, it just starts the first switch case without returning anything out of it. there's more issues I got but in a conversation I might be able to recall them. Thanks https://github.com/JoValadares/Issue/tree/main/TrabalhoFinal_23-24
GitHub
Issue/TrabalhoFinal_23-24 at main · JoValadares/Issue
Contribute to JoValadares/Issue development by creating an account on GitHub.
107 Replies
leowest
leowest3w ago
it reaches line 31 u type in an option or are u saying its not asking for an option and just returns nothing
Curlyfry210
Curlyfry2103w ago
it asks for an option, I tell him the option, and it just goes back to "Exibir" the menu method
leowest
leowest3w ago
which option u tell it
Curlyfry210
Curlyfry2103w ago
what do you mean?
leowest
leowest3w ago
what is the option ur trying to reach
Curlyfry210
Curlyfry2103w ago
when I do the first option, it works, still working on it, but it works for the time being, but the 2nd option has 3 more options below it, those are the ones not returning anything sorry I switched them, it's the 2nd option that has 3 more below
leowest
leowest3w ago
ok so you're doing 2, then u want o select an option and it just goes back to the main menu?
Curlyfry210
Curlyfry2103w ago
yea, without showing the data that I asked for, which in the would be the average, minimum and maximum number of the data input
Curlyfry210
Curlyfry2103w ago
yes it's most likely that
leowest
leowest3w ago
public static List <Leitura> leituras { get; set; }

public DadosAmbientais()
{
leituras = new List<Leitura>();
}

public static void AdicionarLeitura(Leitura leitura)
{
List<Leitura> leituras;
leituras = new List<Leitura>();
leituras.Add(leitura);
}
public static List <Leitura> leituras { get; set; }

public DadosAmbientais()
{
leituras = new List<Leitura>();
}

public static void AdicionarLeitura(Leitura leitura)
{
List<Leitura> leituras;
leituras = new List<Leitura>();
leituras.Add(leitura);
}
you're creating a local variable leituras and updating it locally so once it goes out of the scope its gone you're not using the property leituras which is subsequently used by your other methods on DadosAmbientais.cs so it makes sense that when u do DadosAmbientais.leituras its empty
Curlyfry210
Curlyfry2103w ago
so how would I fix that?
leowest
leowest3w ago
by using the property to store the data
Curlyfry210
Curlyfry2103w ago
but I have been using the property have I not?
leowest
leowest3w ago
look at the code above specifically inside AdicionarLeitura and explain to me what it does
Curlyfry210
Curlyfry2103w ago
I don't know how to explain it very well as english is not my language, but wouldn't that be like creating a new list and adding information into it? or is it because of the static?
leowest
leowest3w ago
// here you are declaring a local variable named leituras
List<Leitura> leituras;
// now you are intializing that local variable
leituras = new List<Leitura>();
// now you're adding items to that local variable
leituras.Add(leitura);
// here you are declaring a local variable named leituras
List<Leitura> leituras;
// now you are intializing that local variable
leituras = new List<Leitura>();
// now you're adding items to that local variable
leituras.Add(leitura);
do you see the problem?
Curlyfry210
Curlyfry2103w ago
it's local?
leowest
leowest3w ago
what else? it being local is 1 of the problems
Curlyfry210
Curlyfry2103w ago
I can't see what's the other
leowest
leowest3w ago
you're not using your property at all the static is also bad and should not be needed its an instance you're initializing.
Curlyfry210
Curlyfry2103w ago
what do you mean I'm not using my property?
leowest
leowest3w ago
do you understand what a local variable is?
Curlyfry210
Curlyfry2103w ago
I am genuinely confused, I'm on my first year of C# so hence why I'm not really understanding of this I would guess it's something only the same class can mess with
leowest
leowest3w ago
no its something that exists within a block i.e.:
{
// inside here
}
{
// inside here
}
Curlyfry210
Curlyfry2103w ago
so how would I call it if not inside a method "block"
leowest
leowest3w ago
so we have
// namespace
namespace Something
{
// class
public class Another
{
// property
public string Name {get;set}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}
}
}
// namespace
namespace Something
{
// class
public class Another
{
// property
public string Name {get;set}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}
}
}
now imagine this
// namespace
namespace Something
{
// class
public class Another
{
// property
public string Name {get;set}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
string Name = name;
}
}
}
// namespace
namespace Something
{
// class
public class Another
{
// property
public string Name {get;set}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
string Name = name;
}
}
}
what would this print
var another = new Another();
another.Add("Jose");
another.Print();
var another = new Another();
another.Add("Jose");
another.Print();
Curlyfry210
Curlyfry2103w ago
I think it would yea
leowest
leowest3w ago
would what?
Curlyfry210
Curlyfry2103w ago
José
leowest
leowest3w ago
u think Print() would print Jose? it does not
Curlyfry210
Curlyfry2103w ago
yea I think so
leowest
leowest3w ago
because we never give the property Name the value Jose we are giving it to a local variable we added inside Add
public void Add(string name) // imagine name is Jose
{
Name = name;
}
public void Add(string name) // imagine name is Jose
{
Name = name;
}
this would have used the property
MODiX
MODiX3w ago
leowest
REPL Result: Success
var another = new Another();
another.Add("Jose");
another.Print();

public class Another
{
// property
public string Name {get;set;}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
string Name = name;
}
}
var another = new Another();
another.Add("Jose");
another.Print();

public class Another
{
// property
public string Name {get;set;}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
string Name = name;
}
}
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 511.528ms | Execution: 35.913ms | React with ❌ to remove this embed.
MODiX
MODiX3w ago
leowest
REPL Result: Success
var another = new Another();
another.Add("Jose");
another.Print();

public class Another
{
// property
public string Name {get;set;}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
Name = name;
}
}
var another = new Another();
another.Add("Jose");
another.Print();

public class Another
{
// property
public string Name {get;set;}
// field
private bool _isWorking;

// methods
public void Print()
{
Console.WriteLine(Name);
}

public void Add(string name) // imagine name is Jose
{
Name = name;
}
}
Console Output
Jose
Jose
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 503.540ms | Execution: 34.926ms | React with ❌ to remove this embed.
leowest
leowest3w ago
you see the difference on both code one print jose, the other does not
Curlyfry210
Curlyfry2103w ago
I do see it
leowest
leowest3w ago
that is the impact of scopes and acessibility in c#
Curlyfry210
Curlyfry2103w ago
so I gotta remove "leituras = new..." from here?
leowest
leowest3w ago
u have to remove the first 2 lines the first line declares the local variable u dont want that and the 2nd line initializes it to empty why would u want to make it empty every time u add new items to it and last but not least u need to remove static from the property https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/understanding-scope-and-accessibility-modifiers this is very important to understand
Curlyfry210
Curlyfry2103w ago
I removed all static from all the methods in DadosAmbientais, but still it all goes rogue when I take away the static from Adicionar
leowest
leowest3w ago
I dont know what goes rogue is, if u have an error say the error
Curlyfry210
Curlyfry2103w ago
No description
leowest
leowest3w ago
yes those errors are in a separate file did u have some requiriment where u need SistemaMonitorizacaoAmbiental to inherit from DadosAmbientais because right now ur doing anything but that
Curlyfry210
Curlyfry2103w ago
yea but from what my professor told me, there's no need to be connected, since DadosAmbientais.cs is just a helping tool to connect SistemaMonitorizacaoAmbiental.cs and Leitura.cs
leowest
leowest3w ago
do u have written what he asked?
Curlyfry210
Curlyfry2103w ago
No description
Curlyfry210
Curlyfry2103w ago
this would be there connection
leowest
leowest3w ago
ok so remove this : DadosAmbientais and again remove anything static in SistemaMonitorizacaoAmbiental.cs static is used in very specific scenarios
Curlyfry210
Curlyfry2103w ago
anything?
leowest
leowest3w ago
not in the way you're using for instances every static in that file
Curlyfry210
Curlyfry2103w ago
are you talking about methods or everything? cuz in this image texts says "anything underlined must be static"
leowest
leowest3w ago
in the file SistemaMonitorizacaoAmbiental.cs remove the word "static" anywhere u see it
Curlyfry210
Curlyfry2103w ago
leowest
leowest3w ago
sure those u can leave
Curlyfry210
Curlyfry2103w ago
that's the only one I have in my SistemaMonitorizacaoAmbiental
leowest
leowest3w ago
ok good because the previous file was excessively using it
Curlyfry210
Curlyfry2103w ago
yea I can see that now but now there is no connection
leowest
leowest3w ago
there is connection
Curlyfry210
Curlyfry2103w ago
I used the static and the : DadosAmbientais because VS2022 told me to, since I was so confused with everything being wrong
leowest
leowest3w ago
that is why I told u to watch the video above
Curlyfry210
Curlyfry2103w ago
how so, still got alot of erros, even more than before
leowest
leowest3w ago
so u have a better understanding of scope and acessibility while I explain the rest
Curlyfry210
Curlyfry2103w ago
I was watching it but came back in hopes of understand better will continue now
leowest
leowest3w ago
so in your SistemaMonitorizacaoAmbiental class the connection here would be private DadosAmbientais dadosAmbientais; That is how you access the instance of DadosAmbientais from it so why do u have the errors? because you're not using it
DadosAmbientais.AdicionarLeitura(leitura);
DadosAmbientais.AdicionarLeitura(leitura);
You're acessing the static class directly not the instance u have declared
dadosAmbientais.AdicionarLeitura(leitura);
dadosAmbientais.AdicionarLeitura(leitura);
Curlyfry210
Curlyfry2103w ago
holy shit that such a simple thing and it somehow makes so much sense I created a bridge and I was trying to barge through the back door
leowest
leowest3w ago
more like u created a blackhole and were trying to patch all over it to remove the holes :catlaugh: that's why access modifier, scopes etc are important to understand so u know ur acessing the right thing working the right context etc I would also make totalSensores private not public same way u did to Sensor
Curlyfry210
Curlyfry2103w ago
also, I did do a little bit of cheating while doing this, could you briefly explain what "Distinct()"; does? or send a video about it?
MODiX
MODiX3w ago
leowest
REPL Result: Success
var letters = new List<string> { "a", "b", "c", "a", "c" };
letters.Distinct()
var letters = new List<string> { "a", "b", "c", "a", "c" };
letters.Distinct()
Result: DistinctIterator<string>
[
"a",
"b",
"c"
]
[
"a",
"b",
"c"
]
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 309.039ms | Execution: 66.825ms | React with ❌ to remove this embed.
Curlyfry210
Curlyfry2103w ago
can I get one more push on this? why is it when I try to create both types of "Sensor" so "SensorTemperatura" and "SensorHumidade", it only creates Humidade, or at least only displays humidade
Curlyfry210
Curlyfry2103w ago
example, here I created three type 1 sensores, which should be "Temperatura", but instead they're shown as "Humidade"
No description
leowest
leowest3w ago
mmmm string tipo = tipoSensor == 0 ? "Temperatura" : "Humidade"; https://github.com/JoValadares/Issue/blob/main/TrabalhoFinal_23-24/TrabalhoFinal_23-24/Menu.cs#L110C16-L110C76 this one should be easy for u to spot
Curlyfry210
Curlyfry2103w ago
I don't see it
leowest
leowest3w ago
what are the options
Curlyfry210
Curlyfry2103w ago
the options of it being wrong in my opinion? tipo is not being reached by the method
leowest
leowest3w ago
what are the option numbers
Curlyfry210
Curlyfry2103w ago
1-Temperatura 2- Humidade
leowest
leowest3w ago
ok and how would u compare if tipo was temperatura?
Curlyfry210
Curlyfry2103w ago
1 == Temperatura
leowest
leowest3w ago
and what are u not doing in your code
Curlyfry210
Curlyfry2103w ago
saying which of the option is which?
leowest
leowest3w ago
No description
leowest
leowest3w ago
look closer and think of what we just talked about its not a tricky question
Curlyfry210
Curlyfry2103w ago
attibuiting a number to its specific option is the one answer that comes to head
leowest
leowest3w ago
No description
No description
Curlyfry210
Curlyfry2103w ago
it was "tipoSensor ==1" before but there was a bug going on cuz of it and when I changed it it worked
leowest
leowest3w ago
well you input 1 or 2 that means u will never create a sensor of type temperature because 0 will never be 1
Curlyfry210
Curlyfry2103w ago
oh I get it
leowest
leowest3w ago
int tipoSensor = 0;
while (tipoSensor != 1 && tipoSensor != 2)
{
Console.WriteLine("Tipo de Sensor (1-Temperatura, 2-Humidade): ");
tipoSensor = int.Parse(Console.ReadLine());
}
int tipoSensor = 0;
while (tipoSensor != 1 && tipoSensor != 2)
{
Console.WriteLine("Tipo de Sensor (1-Temperatura, 2-Humidade): ");
tipoSensor = int.Parse(Console.ReadLine());
}
Curlyfry210
Curlyfry2103w ago
but that would conflict with what's already in the method
leowest
leowest3w ago
conflict with what that is literally saying ask the user for the type until he types a valid number
leowest
leowest3w ago
leowest
leowest3w ago
in this case valid number being 1 or 2
Curlyfry210
Curlyfry2103w ago
but wouldn't making the 0 a 1 here solve the issue? cuz my program doesn't have all of that
leowest
leowest3w ago
yes it will my point is ur not handling invalid scenarios and for those it will always create a humidity sensor that is how that ternary works if its not 1 go to right hand limiting the user to pick only 1 or 2 is one way, the other would be using a switch with 3 cases the 3rd case being an error and returning and not creating the sensor or if,elseif,else which u prefer
Curlyfry210
Curlyfry2103w ago
would that be inside the for?
leowest
leowest3w ago
it would replace the 2 lines u have before tipo yes
Curlyfry210
Curlyfry2103w ago
but here what would I put on "tipo"
No description
Curlyfry210
Curlyfry2103w ago
tipoSensor instead?
leowest
leowest3w ago
I didnt change those 2 lines
replace the 2 lines u have before tipo yes
before tipo
Curlyfry210
Curlyfry2103w ago
oh I was replacing these two lines I still have alot of issues but I'l ltry to solve them by myself for now, thanks for all the help tho, sorry for bothering aswell
leowest
leowest3w ago
not a bother but the parts that were not working are working now correct?
Curlyfry210
Curlyfry2103w ago
yea, the ones I did make an issue
leowest
leowest3w ago
ok 🙂 well feel to ask more or $close up to u
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered
Curlyfry210
Curlyfry2103w ago
if I really can't seem to grasp the error I will, thanks again!