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?

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thansk! well databinding - do you have a reference?
isen't databinding only for ASP.net?
databinding is for ASP.Net, WinForms and WPF as well
is data-binding really necessary ?
Mathias: when you copy the data from textbox to property then that too is a form of databinding.
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.

Comments

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

1 Comment

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!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.