C
C#2mo ago
maiker

Basic stuff: c#

so i am new to programming i only learnt the basics, so please don't laugh at this, but i don't quite understand using classes in other classes(not program.cs i mean other classes). for example: internal class WritingTool { private string name; private string metrial; private double price; private bool isUsed; } internal class PencilCase { ..... } how can i use the WritingTool class and its properties in PencilCase and how it works. and i don't mean parent child classes.
4 Replies
maiker
maikerOP2mo ago
i mean not as a whole for example creating arr of it like private WritingTool[] writingTools; but how do i use it
Dinosaure
Dinosaure2mo ago
Can I ask you what you mean by "using" the class inside the other? Because there are multiple use cases, and they have different outcomes. If you just want to use the class as just an embedded class, you can do so:
internal class PencilCase
{
internal class WritingTool
{
public string name; //fields have to be public to be accessible outside
private string metrial;
private double price;
private bool isUsed;

}

private WritingTool MyWritingTool = new();
public string ExposeThatName() => MyWritingTool.name;
}
internal class PencilCase
{
internal class WritingTool
{
public string name; //fields have to be public to be accessible outside
private string metrial;
private double price;
private bool isUsed;

}

private WritingTool MyWritingTool = new();
public string ExposeThatName() => MyWritingTool.name;
}
Or if you just want to have an array of this WritingTools in PencilCase:
internal class WritingTool
{
private string name;
private string metrial;
private double price;
private bool isUsed;

}
internal class PencilCase
{
private WritingTool[] WritingTools = [];
}
internal class WritingTool
{
private string name;
private string metrial;
private double price;
private bool isUsed;

}
internal class PencilCase
{
private WritingTool[] WritingTools = [];
}
This would setup your WritingTool array.
cap5lut
cap5lut2mo ago
classes in other classes (or types in other types in general) have the advantage that you can access the private members from the "outer" class. a good example would be something like implementing the IEnumerable<T> interface
public class MyWeirdCollection : IEnumerable<int>
{
private readonly int[] _data = [0, 1, 2, 3, 4];
private readonly int _multiplier = 5;

public Enumerator GetEnumerator() => new(this);

public struct Enumerator(MyWeirdCollection collection)
{
private int _next = 0;
public int Current => collection._data[_next - 1] * collection._multiplier;
public bool MoveNext() => (++_next) < collection._data;
}
}
public class MyWeirdCollection : IEnumerable<int>
{
private readonly int[] _data = [0, 1, 2, 3, 4];
private readonly int _multiplier = 5;

public Enumerator GetEnumerator() => new(this);

public struct Enumerator(MyWeirdCollection collection)
{
private int _next = 0;
public int Current => collection._data[_next - 1] * collection._multiplier;
public bool MoveNext() => (++_next) < collection._data;
}
}
(i left out a lot of the actual stuff needed to implement the interface correctly, but its to showcase) so here, instead of having to pass the _data and _multiplier directly to the Enumerator's constructor, you can simply pass the reference to the collection's instance and still use its private members without having so much bloat
maiker
maikerOP2mo ago
yep
Want results from more Discord servers?
Add your server