我在这里读到这个问题:
Is there a way to override the empty constructor in a class generated by LINQtoSQL?
通常我的构造函数看起来像:
public User(String username, String password, String email, DateTime birthday, Char gender)
{
this.Id = Guid.NewGuid();
this.DateCreated = this.DateModified = DateTime.Now;
this.Username = username;
this.Password = password;
this.Email = email;
this.Birthday = birthday;
this.Gender = gender;
}
但是,正如在该问题中所读到的,您希望使用部分方法OnCreated()来分配值而不是覆盖默认构造函数。好的,所以我明白了:
partial void OnCreated()
{
this.Id = Guid.NewGuid();
this.DateCreated = this.DateModified = DateTime.Now;
this.Username = username;
this.Password = password;
this.Email = email;
this.Birthday = birthday;
this.Gender = gender;
}
然而,这给了我两个错误:
Partial Methods must be declared private.
Partial Methods must have empty method bodies.
好吧我将其更改为Private Sub OnCreated()
以删除这两个错误。但是我仍然坚持......我如何通过普通的自定义构造函数传递它的值?
另外我在VB中这样做(因为我知道大多数人知道/更喜欢C#),所以会对此产生影响吗?
答案 0 :(得分:1)
您无法将值传递到OnCreated
。您链接的问题涉及覆盖默认构造函数的行为。听起来你想要使用这样的参数化构造函数:
Public Sub New(username as String, password as String, email as String, birthday as DateTime, gender as Char)
User.New()
Me.Id = Guid.NewGuid()
Me.DateCreated = this.DateModified = DateTime.Now
Me.Username = username
Me.Password = password
Me.Email = email
Me.Birthday = birthday
Me.Gender = gender
End Sub
您想创建这样的新用户:
Dim u as User = new User()
或者像这样:
Dim u as User = new User("Name", "Password", "Email", etc)
答案 1 :(得分:0)
使用VB时,不应将实现标记为Partial
,而应将其标记为Private
。请看以下示例:
Private Sub OnCreated()
' Your code here'
End Sub