Re: Storing data in classes.
Seems pretty boilerplate to me. What framework are you using? If it's 4.0 you can auto implement your properties.
Your code will be condensed down to this:
vb.net Code:
Public Class Environment
Public Property EnvironmentName As String
Public Property RegionCode As String
Public Property DBName As String
Public Property DBAddress As String
Public Property DBRegion As String
Public Sub New(environmentName As String, regionCode As String, dbName As String, dbAddress As String, dbRegion As String)
_EnvironmentName = environmentName
_RegionCode = regionCode
_DBName = dbName
_DBAddress = dbAddress
_DBRegion = dbRegion
End Sub
End Class
Re: Storing data in classes.
Your arguments may not be getting copied twice. If the argument is a reference type, then you are only getting the reference anyways. While that reference IS being copied, there is nothing you can do about that, and it is about the fastest possible way to shift data anyways. Having said that, I would add that strings, while being reference types, don't always act as reference types.
In any case, I wouldn't worry about it. You won't gain a thing by switching to ByRef.
Re: Storing data in classes.
I'm going to contradict MattP here and say that you may want to go with the boilerplate code, but only if you are a fan of data binding. Your class looks like a row you'd get from a database table and sometimes you may want to show a list of them in a DataGridView and have them editable. In such a case you would want to implement the INotifyPropertyChange interface which requires that you raise the PropertyChanged event everytime you set one of the properties so what ever control you bind your list to can update its display, so keep that in mind. If you have no intention of using your class with any form of data binding that involves the data being changed while its bound then feel free to ignore this post. :)
Re: Storing data in classes.
This is how I implement classes that represent data that I may want to use in data binding:-
vbnet Code:
'
'Config data for an Environment.
Public Class Environment
Implements System.ComponentModel.INotifyPropertyChanged
Private msEnvName As String
Private msRegionCode As String
Private msDBName As String
Private msDBAddress As String
Private msDBRegion As String
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(ByVal e As System.ComponentModel.PropertyChangedEventArgs)
RaiseEvent PropertyChanged(Me, e)
End Sub
Public Sub New(ByVal lsEnvName As String, ByVal lsRegionCode As String, ByVal lsDBName As String, ByVal lsDBAddress As String, ByVal lsDBRegion As String)
Me.EnvName = lsEnvName
Me.RegionCode = lsRegionCode
Me.DBName = lsDBName
Me.DBAddress = lsDBAddress
Me.DBRegion = lsDBRegion
End Sub
Public Property EnvName() As String
Get
Return msEnvName
End Get
Set(ByVal value As String)
msEnvName = value
OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("EnvName"))
End Set
End Property
Public Property RegionCode() As String
Get
Return msRegionCode
End Get
Set(ByVal value As String)
msRegionCode = value
OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("RegionCode"))
End Set
End Property
Public Property DBName() As String
Get
Return msDBName
End Get
Set(ByVal value As String)
msDBName = value
OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBName"))
End Set
End Property
Public Property DBAddress() As String
Get
Return msDBAddress
End Get
Set(ByVal value As String)
msDBAddress = value
OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBAddress"))
End Set
End Property
Public Property DBRegion() As String
Get
Return msDBRegion
End Get
Set(ByVal value As String)
msDBRegion = value
OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBRegion"))
End Set
End Property
End Class
Come to think of it your class looks like some kind of config for your app so you may not want to do this but I'll leave it here for future reference.
Re: Storing data in classes.
The original post contained boilerplate code that could be condensed to auto implemented properties. If he needs to change it in the future all he needs to do is hit enter and type in 'Get' to convert it from an auto implemented property to explicit implementation.
-----
Here's the BaseClass that I use to implement INotifyPropertyChanged. It overloads the NotifyPropertyChanged method to take an Expression(Of Func(Of T)) so you're not passing in magic strings.
vb.net Code:
Imports System.ComponentModel
Imports System.Linq.Expressions
Imports System.Reflection
Public MustInherit Class BindingBaseClass
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub NotifyPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Public Sub NotifyPropertyChanged(Of T)(propertyExpression As Expression(Of Func(Of T)))
Dim propertyName As String = GetPropertyName(propertyExpression)
NotifyPropertyChanged(propertyName)
End Sub
Private Function GetPropertyName(Of T)(propExpression As Expression(Of Func(Of T))) As String
If propExpression Is Nothing Then
Throw New ArgumentNullException("propExpression")
End If
Dim mbrExpression As MemberExpression = TryCast(propExpression.Body, MemberExpression)
If mbrExpression Is Nothing Then
Throw New ArgumentException("The expression is not a member access expression.", "propExpression")
End If
Dim prop As PropertyInfo = TryCast(mbrExpression.Member, PropertyInfo)
If prop Is Nothing Then
Throw New ArgumentException("The member access expression does not access a property.", "propExpression")
End If
If Not prop.DeclaringType.IsAssignableFrom(Me.[GetType]()) Then
Throw New ArgumentException("The referenced property belongs to a different type.", "propExpression")
End If
Dim getMethod As MethodInfo = prop.GetGetMethod(True)
If getMethod Is Nothing Then
Throw New ArgumentException("The referenced property does not have a get method.", "propExpression")
End If
If getMethod.IsStatic Then
Throw New ArgumentException("The referenced property is a static property.", "propExpression")
End If
Return mbrExpression.Member.Name
End Function
End Class
Here is a sample class inheriting from the base class and adding a setter guard clause.
vb.net Code:
Public Class Environment
Inherits BindingBaseClass
Private _envName As String
Public Property EnvName As String
Get
Return _envName
End Get
Set(value As String)
If _envName = value Then Return
_envName = value
NotifyPropertyChanged(Function() EnvName)
End Set
End Property
Private _regionCode As String
Public Property RegionCode As String
Get
Return _regionCode
End Get
Set(value As String)
If _regionCode = value Then Return
_regionCode = value
NotifyPropertyChanged(Function() RegionCode)
End Set
End Property
Private _dbName As String
Public Property DBName As String
Get
Return _dbName
End Get
Set(value As String)
If _dbName = value Then Return
_dbName = value
NotifyPropertyChanged(Function() DBName)
End Set
End Property
Private _dbAddress As String
Public Property DBAddress As String
Get
Return _dbAddress
End Get
Set(value As String)
If _dbAddress = value Then Return
_dbAddress = value
NotifyPropertyChanged(Function() DBAddress)
End Set
End Property
Private _dbRegion As String
Public Property DBRegion As String
Get
Return _dbRegion
End Get
Set(value As String)
If _dbRegion = value Then Return
_dbRegion = value
NotifyPropertyChanged(Function() DBRegion)
End Set
End Property
Public Sub New(lsEnvName As String, lsRegionCode As String, lsDBName As String, lsDBAddress As String, lsDBRegion As String)
_envName = lsEnvName
_regionCode = lsRegionCode
_dbName = lsDBName
_dbAddress = lsDBAddress
_dbRegion = lsDBRegion
End Sub
End Class
Re: Storing data in classes.
That is really amazing :D. Id give you rep twice if I could for that godly piece of code but alas I cant give even once until the next 24 hours.
Can't get over how clever that is.