C
C#15mo ago
Luna

❔ Getting a value from an class in a list that is an attribute from another class

This code is an exmaple
class a{
.............
public List<b> listB { get; set; }
.............
}

public class b{
public int number { get; set; }
public string text { get; set; }
}
class a{
.............
public List<b> listB { get; set; }
.............
}

public class b{
public int number { get; set; }
public string text { get; set; }
}
I have an instance of class a, and I want to get the string text from 1 of the objects in the list List<b> listB how can I do that? I'm new to OOP and making this project to get better
5 Replies
Thinker
Thinker15mo ago
yourAObject.listB[index].text
Angius
Angius15mo ago
(also, it's not an attribute but a property) $structure
MODiX
MODiX15mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
For C# versions older than 10, see $StructureOld
Angius
Angius15mo ago
Knowing proper nomenclature helps search for answers faster
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.