1

i haven programmed in a while so i forgot something.

ive got a "Kunde" class. with some variables:

class Kunde
{
    private string _navn;
    private string _adresse;
    private int _postnr;
    private string _by;
    private int _telefonnr;
    private int _mobil;
    private string _email;
    private string _land;
    private string _oplysning;
    private int _kundenr;
    //Erhverv:
    private int _cvr;
    private string _firmanavn;
    private string _kontaktperson;

    //Tom konstruktør
    public Kunde()
    {
    }

    //privat
    public Kunde(string navn, string adresse, int postnr, string by, int telefonnr, int mobil, string email, string land, string oplysning, int kundenr)
    {
        _navn = navn;
        _adresse = adresse;
        _postnr = postnr;
        _by = by;
        _telefonnr = telefonnr;
        _mobil = mobil;
        _email = email;
        _land = land;
        _oplysning = oplysning;
        _kundenr = kundenr;
    }
}

}

my question is.. ive got a winform with some text fields, but not every field has to be filled with data..

should a make a get/set on every variable to be able to set the variable from another class - or a constructor for each option?

whats the best way to do this?

MMM
  • 311
  • 5
  • 14
  • 30

4 Answers4

2

Just provide a Get and optionally a Set accessor for each member.

You'll have to pick some form of DataBinding + Validation to work from your Form. But a Customer class has its own design and its own logic.

H H
  • 263,252
  • 30
  • 330
  • 514
2

In C# 4.0, you can specify values for properties when calling a constructor.

var kunde = new Kunde() 
{
    Navn = navn,
    Adresse = adresse,
    // all your properties
};

Create get/set accessors for each of your fields and then you can specify whichever properties you want to set as above.

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
1

You'd better keep default constructor only and create public property for each data you need to read or set.

You may keep your constructor with parameters - but only with those that are really mandatory to be filled for each of your Kunde-n.

If you plan to bind your Kunde object-s directly to some BindingSource and display them e.g. in some sort of grid/list and/or treeview you may also consider implementing some of the related interfaces: System.ComponentModel.IdataErrorInfo; System.ComponentModel.INotifyPropertyChanged;

and you may cosider apply Attribute-s to your public properties - such as System.ComponentModel.DisplayNameAttribute; - it can define fixed name of headers in the DataGrid orit might be localized for different languages

Community
  • 1
  • 1
rudolf_franek
  • 1,795
  • 3
  • 28
  • 41
  • i need to put my kunde objects in a list.. and then show the list in somekind of table yeah.. i might take a look at the displaynameattribute - Thanks! – MMM Oct 01 '11 at 13:36
1

public string Adresse { get; private set; } etc. and you have an automatic variable which is read-only except inside the class.

GeirGrusom
  • 999
  • 5
  • 18