C
C#6mo ago
Curlyfry210

✅ Variable calling from other classes

I've got a question, so imagine I've got 3 classes, and I was trying to make a method to show the information from the 1st class(Info.cs) to 2nd class(Menu.cs) through a 3rd class(MiddleClass.cs), would that be somehow possible?
43 Replies
Angius
Angius6mo ago
Sure The two classes can expose a public property And you can access it from their instances
MODiX
MODiX6mo ago
Angius
REPL Result: Success
class Info
{
public string Name { get; set; }
public void SomeMethod()
{
Name = "Bob";
}
}

class Menu
{
public int Count { get; set; }
public void SomeOtherMethod()
{
Count = 69;
}
}

var info = new Info();
var menu = new Menu();

info.SomeMethod();
menu.SomeOtherMethod();

Console.WriteLine($"Name is {info.Name} and count is {menu.Count}");
class Info
{
public string Name { get; set; }
public void SomeMethod()
{
Name = "Bob";
}
}

class Menu
{
public int Count { get; set; }
public void SomeOtherMethod()
{
Count = 69;
}
}

var info = new Info();
var menu = new Menu();

info.SomeMethod();
menu.SomeOtherMethod();

Console.WriteLine($"Name is {info.Name} and count is {menu.Count}");
Console Output
Name is Bob and count is 69
Name is Bob and count is 69
Compile: 526.359ms | Execution: 43.836ms | React with ❌ to remove this embed.
Curlyfry210
Curlyfry210OP6mo ago
I think I might have explained wrong here so my issue is I have 2 infos classes (examples only) and let's say you have alot of objects on info1.cs, and in info2.cs u got the information about those objetcs could you search on info1.cs and get the info2.cs connected to it through a method?
Angius
Angius6mo ago
Sure, you can pass instances of one class to a method And do whatever with them If you have a lot of objects, I assume it's a list or an array or some other collection In that case yes, you can search for the specific instance LINQ .Where() and/or .FirstOrDefault() will be useful here
Curlyfry210
Curlyfry210OP6mo ago
would it be a bother if I sent the example where I'm having this issue?
Angius
Angius6mo ago
Sure, go ahead
Curlyfry210
Curlyfry210OP6mo ago
it's in another irl language, but it showed to be no bother to another of the helpers here ok
Curlyfry210
Curlyfry210OP6mo ago
so I'm trying to get this method to show the localização of it's creator (NoSensor's location) and it's estadoAtivo (true or false, also in NoSensor) through Sensor.cs Id
No description
Angius
Angius6mo ago
SistemaMonitorizacaoAmbiental has the noSensores property that contains multiple NoSensors NoSensor has sensores, which is a list of Sensors That has estatoAdivo So you can loop over noSensores, then over sensores Or use LINQ
Curlyfry210
Curlyfry210OP6mo ago
a foreach u say? I really don't know what LINQ is
Angius
Angius6mo ago
For example, yes
Curlyfry210
Curlyfry210OP6mo ago
but then how'd do the foreach? what would I search in Nosensor
Angius
Angius6mo ago
Sensores
foreach (nosensor in noSensores)
foreach (sensor in nosensor.sensores)
...
foreach (nosensor in noSensores)
foreach (sensor in nosensor.sensores)
...
basically
Curlyfry210
Curlyfry210OP6mo ago
so right now I have it like this
No description
Curlyfry210
Curlyfry210OP6mo ago
and right now it's showing the id and value of the sensor I just searched how would I add a foreach here that'd make the localização and estadoAtivo of the NoSensor that this Sensor belongs to appear on the aftermath
Angius
Angius6mo ago
Assuming sensorId is actually an instance of Sensor, and not its ID like it says Get that data from there
Curlyfry210
Curlyfry210OP6mo ago
also for some reason when using the foreach here, instead of showing it one time, it'd show the same thing thrice and when I took it off it worked out
Angius
Angius6mo ago
Well, if there are three sensores...
Curlyfry210
Curlyfry210OP6mo ago
C# mixed in with a language you still have issues with can be such a hard achievement I'm sorry if I don't get it but what does that mean?
Angius
Angius6mo ago
If there are three elements in the sensores list, the loop will run three times, so it will print to console three times
Curlyfry210
Curlyfry210OP6mo ago
I see, so the problem was that the console write line was inside the foreach not that the foreach itself was wrong
Angius
Angius6mo ago
The problem was you expected to get a single sensor, seemingly But you got three At no point did you say "I want just one sensor"
Curlyfry210
Curlyfry210OP6mo ago
it was showing only one sensor, but repeating the same one 3 times I've been trying the foreach and still can't get a hold of how to do it in a way that it'd show these variables correspondent to the Id I'm searching for
Curlyfry210
Curlyfry210OP6mo ago
No description
Angius
Angius6mo ago
Don't select just the ID, get the entire sensor. Then, get those properties from that sensor Assuming dadosAmbientais.leituras is a list of sensors?
Curlyfry210
Curlyfry210OP6mo ago
that's where they are read from if that's what you're asking yea
Curlyfry210
Curlyfry210OP6mo ago
would this not be enough?
No description
Angius
Angius6mo ago
Is it?
Curlyfry210
Curlyfry210OP6mo ago
apparently not, since I can't seem to reach even estadoAtivo, which is a Sensor variable
Angius
Angius6mo ago
Does this not work, from the MonstrarEstaticadasPorSensor() method? It should be able to list all sensors from all nosensors
Curlyfry210
Curlyfry210OP6mo ago
the thing is that method should be able to go to a nosensor, and check if the Id that the user put in exists, and then show the information and for now it's working it's showing back the id and the ValorAtual of the sensor the user asks for but I want it to show more than just the id and valor
Angius
Angius6mo ago
Then get more
Curlyfry210
Curlyfry210OP6mo ago
that's the issue, how do I call them to the method?
Curlyfry210
Curlyfry210OP6mo ago
No description
Angius
Angius6mo ago
Get the nosensor the user requests From that nosensor get all sensors Right now your sensores is a list of just the IDs
Curlyfry210
Curlyfry210OP6mo ago
yea but I'm not asking the user for nosensores, I'm asking the user for the sensor
Angius
Angius6mo ago
But sensors are inside of nosensors
Curlyfry210
Curlyfry210OP6mo ago
yes
Angius
Angius6mo ago
This class has a list of nosensors Each nosensor has a list of sensors
Curlyfry210
Curlyfry210OP6mo ago
yes
Angius
Angius6mo ago
So you will need to get all the sensors from all the nosensors And get the one you need
Sensor? sensor = null;
foreach (var ns in nosensors)
{
foreach (var s in ns.Sensors)
{
if (s.Id == UserRequestedIdOrSomething)
{
sensor = s;
break;
}
}
}
Sensor? sensor = null;
foreach (var ns in nosensors)
{
foreach (var s in ns.Sensors)
{
if (s.Id == UserRequestedIdOrSomething)
{
sensor = s;
break;
}
}
}
Something like this Or with LINQ,
var sensor = nosensors
.SelectMany(ns => ns.Sensors)
.Where(s => sId == UserRequestedIdOrSomething)
.FirstOrDefault();
var sensor = nosensors
.SelectMany(ns => ns.Sensors)
.Where(s => sId == UserRequestedIdOrSomething)
.FirstOrDefault();
Curlyfry210
Curlyfry210OP6mo ago
I can definitely see it working I just can't implement it never mind I got it working
Want results from more Discord servers?
Add your server