开发者

VB Classes Best Practice - give all properties values?

Sorry if this is a bit random, but is it good practice to give all fields of a class a value when the class i开发者_Go百科s instanciated? I'm just wondering if its better practice to have a constuctor that takes no parameters and gives all the fields default values, or whether fields that have values should be assigned and others left alone until required?

I hope that makes sense, Becky


I don't know if it makes a performance difference or not, but any fields for which you have explicit default values I personally prefer to assign them in the declarations, as so:

Public Class MyClass
    Private pIsDirty As Boolean = False
    Private pDated as Date = Now()
End Class

Keep in mind most "simple" types like boolean, integer, etc. auto-default and don't NEED to be initialized, but I show that here as example and sometimes for clarity you want it anyway. Additionally since any classes I write are all for internal use (we don't sell any code objects for public use) I can be assured to the consumer of my classes. So I generally just write a minimal constructor (if a non-default one is needed) that only takes the primary fields, and spin up any additional values with the new With syntax in VB as so:

Dim myObj = New SomeClass() With { .Prop1 = "value", .Prop2 = Now() }


Your class' constructor should accept enough parameters to be in an usable state.

You can get the same functionality you seem to be looking for by using Optional Parameters in your constructor.

That way you can set by name just the properties that you have to, and leave the rest with default values until you need to change them.

Sub Notify(ByVal Company As String, Optional ByVal Office As String = "QJZ")
   If Office = "QJZ" Then
      Debug.WriteLine("Office not supplied -- notifying Headquarters")
      Office = "Headquarters"
   End If
   ' Code to notify headquarters or specified office.
End Sub

Remember that optional parameters must be after all non optional parameters.


Ideal practice is to have an object in a usable state as soon as the constructor returns. This reduces errors whereby a partially 'ready' object is inadvertently used.


It depends. How do you intend to use the class? What is the purpose of the class? (i.e. is it an entity class for database modeling or some other class)

I always make my entity classes with nullable properties that are all null when the class is instanced with a constructor that takes no parameter. Then when I call .Load I know all properties reflect the database.

Adding a constructor that calls .Load and assigns all of the properties known values from the database would be a feasible route also.

If you are not referring to an entity modeling class then it really depends on your usage of the class.

My personal preference is to assign all properties a known value (from constructor parameters) and therefore the class is in a known - neutral state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜