![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| I have a class within my objects layer that contains various properties relating to a client; forename, surname, address etc. I have several other client pages that contain different client information, which comes from different related db tables. In total there must be about maybe 60-70 different properties, possibly more. Would it be better to have all these properties in one class or split them between classes depending on what data is required on a page? Does it make any difference to the efficiency of the code? I'm just thinking on each page you'd be creating a new instance of a class with several properties that aren't being used on that page. |
| Sponsored Links |
|
#2
| ||||
| ||||
| I'm thinking it would be inefficient to create multiple classes like that. One thing I noticed from looking over your code is that you are not using constructors. You can have multiple constructors to perform initialization of your class and would look like below: Code: Public Sub New()
End Sub
Public Sub New(ByVal firstname As String, ByVal middlename As String, ByVal lastname As String)
Me.FirstName = firstname
Me.MiddleName = middlename
Me.LastName = lastname
End Sub
Public Sub New(ByVal lastname As String, ByVal email As String)
Me.LastName = lastname
Me.Email = email
End Sub
Code: Dim myclass As New MyClass("Richy", "", "Rich")
Code: myclass.FirstName = "Richy" myclass.MiddleName = "" myclass.LastName = "Rich"
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.Join our Folding team: DeveloperBarn Folding |
|
#3
| ||||
| ||||
| OK. Makes sense. So you have constructors within your BOL layer that are called from your DAL layer. How do you then deal with NULL values? At the moment by explicitly setting the value of each property, I can use:- Code: if not isdbnull(rs("middlename")) then client.middlename= rs("middlename")
Code: Public Sub New(ByVal middlename as String)
Me.middlename = middlename
end sub
Private _middlename as String = String.Empty
Public Property middlename() as String
Get
Return _middlename
End Get
Set(ByVal value As String)
_middlename = value
End Set
End Property
Code: Dim client as new ClientBOL(rs("middlename"))
|
|
#4
| ||||
| ||||
| Yeah, just tried it for a couple of properties and it gave an error when converting a NULL value... Hmmm....Is it best to alter the DB to not store NULL values or to handle the object creation differently? It worked fine without the constructors and explicity defining each property. I've always understand that it's better to use NULL as it's more "definite" than blank or empty values. I can't see the point in dealing with the NULL values in the SQL code as you may as well not store them as NULL in the first place. What do you think is the best way to go? |
|
#5
| ||||
| ||||
| Quote:
![]() Quote:
Personally, I do NOT like using NULL values. First off, they are more of a pain than they are helpful. Secondly, what does NULL mean? It's really not descriptive of why a value wasn't entered. The examples in the above article explain this. If you do choose to use NULL in your database, then you can allow your properties you use Nullable data types as I have shown you earlier. In the case of strings, which donot have a Nullable Type, you just have to do some validation before passing the value to the object. |
|
#6
| ||||
| ||||
| OK, assuming I keep using NULL, is there an advantage to using a constructor? From your previous post, you said that it wouldn't be a good idea to create new classes for each client page with different information. So, if the ClientBOL class contains all the properties anyway, creating an instance of this class is still going to give me some redundant properties on each page isn't it? Irrespective of whether I use a constructor or not. I just imagine that a class with 60-70 properties is going to be more resource intensive than one containing 20-30 properties. Or is each class compiled when the App is loaded, irrespective of whether you create an instance of it on the page or not? Does that make sense? |
|
#7
| ||||
| ||||
| Constructors help initialize member variables. Take a look at the following: VB.NET Language - Lesson 12: Class Construction and Destruction Pay particular attention to the sections labeled Class Construction and Class Deconstruction. Considering most experienced programmers go about this the way I have been showing you, I doubt there are any performance issues. Especially since .Net automatically performs garbage collection. Regardless, if you do experience performance issues, maybe you need to redesign your class. Why do you have so many properties? Perhaps I misunderstood your question about creating multiple classes. I have apps that have multiple classes. Some of these classes use other classes as properties. For example, an application for an address book might have the following classes: Contact and Address With these two classes, the Address class could be used as a property of the Contact class. This breaks down the amount of properties for each class. Is this what you were considering? |
|
#8
| ||||
| ||||
| Basically, the App contains details of our clients. As there's alot of information, it's split between various pages. So, the first page contains name details, address, phone numbers etc. The second page contains some other details, the third page some more details etc. So, if you were to view Page 2, for example, you wouldn't need to know the address details, so it seems a waste of resource to create an instance of a class that holds all the properties of a client for each page. Does that make more sense? |
|
#9
| ||||
| ||||
| Absolutely! In this case, I would definitely have this broken into multiple classes: User and Address, with address also containing the phone numbers. You could also have address as a property of the User class as in my example above, if you needed to. |
|
#10
| ||||
| ||||
| So, would something like this be right? Code: Public Class ClientDetails
Private _forename as String = String.Empty
Public Property forename as String
Get
Return _forename
End Get
Set(ByVal value As String)
_forename = value
End Set
End Property
Private _AddressDetails As AddressDetails
Public Property AddressDetails as AddressDetails
Get
Return _AddressDetails
End Get
Set(ByVal value as AddressDetails)
_AddressDetails = value
End Set
End Property
End Class
Public Class AddressDetails
Private _add1 As String = string.empty
Public Property add1 as String
Get
Return _add1
End Get
Set(ByVal value as String)
_add1=value
End Set
End Property
End Class
Code: Private ClientDetails as ClientDetails
ClientDetails.AddressDetails.add1 = rs("add1")
.
.
.
.
txt_add1.text = ClientDetails.AddressDetails.add1
|
![]() |
|
| Bookmarks |
| Tags |
| class, constructors, properties |
| Thread Tools | |
| Display Modes | |
|
|