✅ 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
Angius2w ago
Sure The two classes can expose a public property And you can access it from their instances
MODiX
MODiX2w 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
Curlyfry2102w 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
Angius2w 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
Curlyfry2102w ago
would it be a bother if I sent the example where I'm having this issue?
Angius
Angius2w ago
Sure, go ahead
Curlyfry210
Curlyfry2102w ago
it's in another irl language, but it showed to be no bother to another of the helpers here ok
Curlyfry210
Curlyfry2102w 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
Angius2w 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
Curlyfry2102w ago
a foreach u say? I really don't know what LINQ is
Angius
Angius2w ago
For example, yes
Curlyfry210
Curlyfry2102w ago
but then how'd do the foreach? what would I search in Nosensor
Angius
Angius2w ago
Sensores
foreach (nosensor in noSensores)
foreach (sensor in nosensor.sensores)
...
foreach (nosensor in noSensores)
foreach (sensor in nosensor.sensores)
...
basically
Curlyfry210
Curlyfry2102w ago
so right now I have it like this
No description
Curlyfry210
Curlyfry2102w 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
Angius2w ago
Assuming sensorId is actually an instance of Sensor, and not its ID like it says Get that data from there
Curlyfry210
Curlyfry2102w 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
Angius2w ago
Well, if there are three sensores...
Curlyfry210
Curlyfry2102w 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
Angius2w 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
Curlyfry2102w ago
I see, so the problem was that the console write line was inside the foreach not that the foreach itself was wrong
Angius
Angius2w 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
Curlyfry2102w 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
Curlyfry2102w ago
No description
Angius
Angius2w 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
Curlyfry2102w ago
that's where they are read from if that's what you're asking yea
Curlyfry210
Curlyfry2102w ago
would this not be enough?
No description
Angius
Angius2w ago
Is it?
Curlyfry210
Curlyfry2102w ago
apparently not, since I can't seem to reach even estadoAtivo, which is a Sensor variable
Angius
Angius2w ago
Does this not work, from the MonstrarEstaticadasPorSensor() method? It should be able to list all sensors from all nosensors
Curlyfry210
Curlyfry2102w 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
Angius2w ago
Then get more
Curlyfry210
Curlyfry2102w ago
that's the issue, how do I call them to the method?
Curlyfry210
Curlyfry2102w ago
No description
Angius
Angius2w 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
Curlyfry2102w ago
yea but I'm not asking the user for nosensores, I'm asking the user for the sensor
Angius
Angius2w ago
But sensors are inside of nosensors
Curlyfry210
Curlyfry2102w ago
yes
Angius
Angius2w ago
This class has a list of nosensors Each nosensor has a list of sensors
Curlyfry210
Curlyfry2102w ago
yes
Angius
Angius2w 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
Curlyfry2102w ago
I can definitely see it working I just can't implement it never mind I got it working