设置变量 - 构造函数/获取/设置 C#

发布于 2024-12-07 23:47:48 字数 1094 浏览 1 评论 0原文

我有一段时间没有编程了,所以我忘记了一些东西。

我有一个“Kunde”课程。有一些变量:

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;
    }
}

}

我的问题是..我有一个带有一些文本字段的winform,但不是每个字段都必须填充数据..

应该对每个变量进行获取/设置以便能够从另一个变量设置变量类 - 或者每个选项的构造函数?

最好的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等风来 2024-12-14 23:47:48

只需为每个成员提供一个 Get 和一个可选的 Set 访问器。

您必须选择某种形式的数据绑定+验证才能在您的表单中工作。但是 Customer 类有自己的设计和逻辑。

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.

审判长 2024-12-14 23:47:48

在 C# 4.0 中,您可以在调用构造函数时指定属性值。

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

为每个字段创建 get/set 访问器,然后您可以指定要按上述方式设置的属性。

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.

行雁书 2024-12-14 23:47:48

您最好仅保留默认构造函数,并为需要读取或设置的每个数据创建公共属性。

您可以在构造函数中保留参数 - 但仅限于那些确实必须为每个 Kunde-n 填充的参数。

如果您计划将 Kunde 对象直接绑定到某些 BindingSource 并在某种网格/列表和/或树视图中显示它们,您还可以考虑实现一些相关接口:System.ComponentModel.IdataErrorInfo; System.ComponentModel.INotifyPropertyChanged;

您可以考虑将 Attribute-s 应用于您的公共属性 - 例如 System.ComponentModel.DisplayNameAttribute; - 它可以定义 DataGrid 中标头的固定名称,或者它可能针对不同语言进行本地化

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

固执像三岁 2024-12-14 23:47:48

公共字符串地址{获取;私人套装; } 等,并且您有一个自动变量,该变量在类内部之外是只读的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文