Relationship Has-in C #?

3

I tried to make a relationship in C # and I'm not getting it and I do not even know if it's recommended to do it.

Here is my error code:

  

System.NullReferenceException Object reference not set to an instance of an object.

class Program
{
    static void Main(string[] args)
    {
        Cliente cliente = new Cliente();
        cliente.endereco.rua = "guaranesia";

        Console.WriteLine(cliente.endereco.rua);
        Console.ReadKey();
    }
}

class Cliente
{
    public string name{get; set;}
    public string idade { get; set; }
    public Endereco endereco;
}

class Endereco
{
    public string rua { get; set; }
    public string bairro { get; set; }
}
    
asked by anonymous 06.02.2015 / 00:15

3 answers

3

I would do so:

using System;

public class Program {
    public static void Main(string[] args) {
        Cliente cliente = new Cliente();
        cliente.endereco.rua = "guaranesia";
        Console.WriteLine(cliente.endereco.rua);
    }
}

class Cliente {
    public string name{get; set;}
    public string idade { get; set; }
    public Endereco endereco = new Endereco();
}

class Endereco {
    public string rua { get; set; }
    public string bairro { get; set; }
}

See working on dotNetFiddle .

Make the class work for you. Encapsulate the implementation. Make the consumer cleaner and do not make him aware of how he should use the parts of the class. You used the tag that shows that you understand that you should use this form, you now need to implement this. You should also consider making the field endereco as a property.

Actually in a real class you most likely should have builders . Something like this:

using System;

public class Program {
    public static void Main(string[] args) {
        var cliente = new Cliente();
        cliente.Endereco.Rua = "guaranesia";
        Console.WriteLine(cliente.Endereco.Rua);
        var cliente2 = new Cliente("João", "20", "Rua Torta", "Centro");
        Console.WriteLine(cliente2.Endereco.Rua);
        var cliente3 = new Cliente() {
            Nome = "João", 
            Idade = "20",
            Endereco.Rua = "Rua Torta",
            Endereco.Bairro = "Centro"
        };
        Console.WriteLine(cliente3.Endereco.Rua);
    }
}

class Cliente {
    public string nome {get; set;}
    public string idade {get; set;}
    public Endereco Endereco {get; set;}
    public Cliente() {
        Endereco = new Endereco();
    }
    public Cliente(string nome, string idade, string rua, string bairro) {
        Nome = nome;
        Idade = idade;
        Endereco = new Endereco(rua, bairro);
    }
}

class Endereco {
    public string Rua {get; set;}
    public string Bairro {get; set;}
    //Note que não é preciso criar um construtor Endereco() já que ele não fará nada extra
    //O compilador criará um para você
    public Endereco(string rua, string bairro) {
        Rua = rua;
        Bairro = bairro;
    }
}
    
06.02.2015 / 01:03
2

You have to initialize the endereco property before you can use it.

The best place to do it is in the builder.

class Cliente
{
    public string Name { get; set; }
    public string Idade { get; set; }
    public Endereco Endereco { get; set; }

    public Cliente()
    {
        Endereco = new Endereco();
    }
}

Two notes:

  • In C #, the convention is to use PascalCase for property names
  • Almost never publics should be exposed! In the above code, replace the field with a property (note {get; set;} ).

To read: Jon Skeet - Why Properties Matter

    
06.02.2015 / 12:40
1

You have to instantiate address first:

static void Main(string[] args)
{
    var cliente = new Cliente();
    cliente.endereco = new Endereco();
    cliente.endereco.rua = "guaranesia";

    Console.WriteLine(cliente.endereco.rua);
    Console.ReadKey();
}
    
06.02.2015 / 00:22