C# functions and classes help
say i have
class temp {
private List<temp3> aList = new List<temp3>();
public List<temp3> getAList()
{
return this.aList;
}
public temp() {
}
}
class temp3 {
public temp3() {}
}
now i have an other class
class temp2 {
private temp t = new temp();
t.aList.Add(new temp3());
}
will
t.getAList.Add(new temp3());
really add temp3 to the aList in temp c开发者_StackOverflowlass?
No.
The line should be:
t.getAList().Add(new temp3());
EDIT after reading the comments: put that line in a method.
temp.aList
is private, so no that wouldn't work. What you would want to do is add a property to the temp
class:
public List<temp3> AList
{
get {return aList;}
set {aList = value;}
}
And then use it as t.AList.Add(new temp3())
And as Akram Shahda point out in the comments, you have to create a method in the class for it. You can't use statements like that directly in the class.
aList
is private you cant reach it this way t.aList.Add(new temp3());
.
you should be using the get method getAList()
like this t.getAList.Add(new temp3());
below code does what you want to do
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Project4
{
class temp {
private List<temp3> aList = new List<temp3>();
public List<temp3> getAList
{
get{return aList;}
set{aList = value;}
}
public temp() {
}
}
class temp3 {
public temp3() {}
}
class temp2 {
public static void Method()
{
temp t = new temp();
t.getAList.Add(new temp3());
}
}
}
精彩评论