La référence non définie à une instance d'objet.
Bonjour,
ça fait un bail que j'ai plus fait de C#, mais j'ai décidé de m'y remettre (à votre plus grand malheur :P)
J'ai deux classes :
- Network
- Server
Un network contient une "List<Server>", première question, est-ce que le type est bien choisi? :aie:
Seconde question, j'ai le code suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| private IList<Network> GetNetworks()
{
IList<Network> n = new List<Network>();
Network n0 = new Network("abc");
Server s = new Server("abc1");
MessageBox.Show(s.Name);
n0.Servers.Add(s);
Server s1 = new Server("abc2");
n0.Servers.Add(s1);
n.Add(n0);
Network n1 = new Network("def");
Server s2 = new Server("def1");
n1.Servers.Add(s2);
Server s3 = new Server("def2");
n1.Servers.Add(s3);
n.Add(n1);
return n;
} |
Qui me renvoit un :
Citation:
La référence d'objet n'est pas définie à une instance d'un objet.
Pourtant j'utilise bien new, ça vient de quoi? :)
:merci:
P.S : Server :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| class Server
{
/// <summary>
/// Variables
/// </summary>
string _name;
string _host;
string _port;
string _address;
string _addressType;
/// <summary>
/// Minimal constructor
/// </summary>
public Server(string name)
{
this._name = name;
}
/// <summary>
/// Maximal constructor
/// </summary>
public Server(string name, string host, string port, string address, string addressType)
{
this._name = name;
this._host = host;
this._port = port;
this._address = address;
this._addressType = addressType;
}
/// <summary>
/// Getter / Setter
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
private string Host
{
get { return _host; }
set { _host = value; }
}
public string Port
{
get { return _port; }
set { _port = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public string AddressType
{
get { return _addressType; }
set { _addressType = value; }
}
} |
Network :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Network
{
/// <summary>
/// Variables
/// </summary>
private string _name;
private IList<Server> _servers;
/// <summary>
/// Constructor
/// </summary>
public Network(string name)
{
this._name = name;
}
// Getter / Setter
public string Name
{
get { return _name; }
set { _name = value; }
}
public IList<Server> Servers
{
get { return _servers; }
set { _servers = value; }
}
} |