Results 1 to 7 of 7

Thread: Storing data in classes.

  1. #1

    Thread Starter
    Hyperactive Member capsulecorpjx's Avatar
    Join Date
    May 2005
    Location
    Renton, WA
    Posts
    288

    Storing data in classes.

    Is the below code a good way to store data in classes?

    I have propetie get set classes, and a constructor to quickly set them all.

    The only concern I have is that the byval in the constructor class and the byval in the set() properties, might be inefficient because the parameter is copied twice? Should I use ByRef? Or is that too dangerous?

    Code:
    'Config data for an Environment.
    Public Class Environment
        Private msEnvName As String
        Private msRegionCode As String
        Private msDBName As String
        Private msDBAddress As String
        Private msDBRegion As String
    
        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
            End Set
        End Property
    
        Public Property RegionCode() As String
            Get
                Return msRegionCode
            End Get
            Set(ByVal value As String)
                msRegionCode = value
            End Set
        End Property
    
        Public Property DBName() As String
            Get
                Return msDBName
            End Get
            Set(ByVal value As String)
                msDBName = value
            End Set
        End Property
    
        Public Property DBAddress() As String
            Get
                Return msDBAddress
            End Get
            Set(ByVal value As String)
                msDBAddress = value
            End Set
        End Property
    
        Public Property DBRegion() As String
            Get
                Return msDBRegion
            End Get
            Set(ByVal value As String)
                msDBRegion = value
            End Set
        End Property
    End Class
    "I like to run on treadmills, because at least I know I'm getting nowhere."
    - Me

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Public Class Environment
    2.  
    3.     Public Property EnvironmentName As String
    4.     Public Property RegionCode As String
    5.     Public Property DBName As String
    6.     Public Property DBAddress As String
    7.     Public Property DBRegion As String
    8.  
    9.     Public Sub New(environmentName As String, regionCode As String, dbName As String, dbAddress As String, dbRegion As String)
    10.         _EnvironmentName = environmentName
    11.         _RegionCode = regionCode
    12.         _DBName = dbName
    13.         _DBAddress = dbAddress
    14.         _DBRegion = dbRegion
    15.     End Sub
    16.  
    17. End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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.
    My usual boring signature: Nothing

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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:
    1. '
    2. 'Config data for an Environment.
    3. Public Class Environment
    4.     Implements System.ComponentModel.INotifyPropertyChanged
    5.  
    6.     Private msEnvName As String
    7.     Private msRegionCode As String
    8.     Private msDBName As String
    9.     Private msDBAddress As String
    10.     Private msDBRegion As String
    11.  
    12.     Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    13.  
    14.     Protected Overridable Sub OnPropertyChanged(ByVal e As System.ComponentModel.PropertyChangedEventArgs)
    15.         RaiseEvent PropertyChanged(Me, e)
    16.     End Sub
    17.  
    18.  
    19.     Public Sub New(ByVal lsEnvName As String, ByVal lsRegionCode As String, ByVal lsDBName As String, ByVal lsDBAddress As String, ByVal lsDBRegion As String)
    20.         Me.EnvName = lsEnvName
    21.         Me.RegionCode = lsRegionCode
    22.         Me.DBName = lsDBName
    23.         Me.DBAddress = lsDBAddress
    24.         Me.DBRegion = lsDBRegion
    25.     End Sub
    26.  
    27.     Public Property EnvName() As String
    28.         Get
    29.             Return msEnvName
    30.         End Get
    31.         Set(ByVal value As String)
    32.             msEnvName = value
    33.             OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("EnvName"))
    34.         End Set
    35.     End Property
    36.  
    37.     Public Property RegionCode() As String
    38.         Get
    39.             Return msRegionCode
    40.         End Get
    41.         Set(ByVal value As String)
    42.             msRegionCode = value
    43.             OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("RegionCode"))
    44.         End Set
    45.     End Property
    46.  
    47.     Public Property DBName() As String
    48.         Get
    49.             Return msDBName
    50.         End Get
    51.         Set(ByVal value As String)
    52.             msDBName = value
    53.             OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBName"))
    54.         End Set
    55.     End Property
    56.  
    57.     Public Property DBAddress() As String
    58.         Get
    59.             Return msDBAddress
    60.         End Get
    61.         Set(ByVal value As String)
    62.             msDBAddress = value
    63.             OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBAddress"))
    64.         End Set
    65.  
    66.     End Property
    67.  
    68.     Public Property DBRegion() As String
    69.         Get
    70.             Return msDBRegion
    71.         End Get
    72.         Set(ByVal value As String)
    73.             msDBRegion = value
    74.             OnPropertyChanged(New System.ComponentModel.PropertyChangedEventArgs("DBRegion"))
    75.         End Set
    76.     End Property
    77.  
    78. 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.
    Last edited by Niya; Apr 25th, 2012 at 02:51 PM. Reason: Minor edit.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Imports System.ComponentModel
    2. Imports System.Linq.Expressions
    3. Imports System.Reflection
    4.  
    5. Public MustInherit Class BindingBaseClass
    6.     Implements INotifyPropertyChanged
    7.  
    8.     Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    9.  
    10.     Public Sub NotifyPropertyChanged(name As String)
    11.         RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    12.     End Sub
    13.  
    14.     Public Sub NotifyPropertyChanged(Of T)(propertyExpression As Expression(Of Func(Of T)))
    15.         Dim propertyName As String = GetPropertyName(propertyExpression)
    16.         NotifyPropertyChanged(propertyName)
    17.     End Sub
    18.  
    19.     Private Function GetPropertyName(Of T)(propExpression As Expression(Of Func(Of T))) As String
    20.         If propExpression Is Nothing Then
    21.             Throw New ArgumentNullException("propExpression")
    22.         End If
    23.  
    24.         Dim mbrExpression As MemberExpression = TryCast(propExpression.Body, MemberExpression)
    25.         If mbrExpression Is Nothing Then
    26.             Throw New ArgumentException("The expression is not a member access expression.", "propExpression")
    27.         End If
    28.  
    29.         Dim prop As PropertyInfo = TryCast(mbrExpression.Member, PropertyInfo)
    30.         If prop Is Nothing Then
    31.             Throw New ArgumentException("The member access expression does not access a property.", "propExpression")
    32.         End If
    33.  
    34.         If Not prop.DeclaringType.IsAssignableFrom(Me.[GetType]()) Then
    35.             Throw New ArgumentException("The referenced property belongs to a different type.", "propExpression")
    36.         End If
    37.  
    38.         Dim getMethod As MethodInfo = prop.GetGetMethod(True)
    39.         If getMethod Is Nothing Then
    40.             Throw New ArgumentException("The referenced property does not have a get method.", "propExpression")
    41.         End If
    42.  
    43.         If getMethod.IsStatic Then
    44.             Throw New ArgumentException("The referenced property is a static property.", "propExpression")
    45.         End If
    46.  
    47.         Return mbrExpression.Member.Name
    48.     End Function
    49. End Class

    Here is a sample class inheriting from the base class and adding a setter guard clause.

    vb.net Code:
    1. Public Class Environment
    2.     Inherits BindingBaseClass
    3.  
    4.     Private _envName As String
    5.     Public Property EnvName As String
    6.         Get
    7.             Return _envName
    8.         End Get
    9.         Set(value As String)
    10.             If _envName = value Then Return
    11.             _envName = value
    12.             NotifyPropertyChanged(Function() EnvName)
    13.         End Set
    14.     End Property
    15.  
    16.     Private _regionCode As String
    17.     Public Property RegionCode As String
    18.         Get
    19.             Return _regionCode
    20.         End Get
    21.         Set(value As String)
    22.             If _regionCode = value Then Return
    23.             _regionCode = value
    24.             NotifyPropertyChanged(Function() RegionCode)
    25.         End Set
    26.     End Property
    27.  
    28.     Private _dbName As String
    29.     Public Property DBName As String
    30.         Get
    31.             Return _dbName
    32.         End Get
    33.         Set(value As String)
    34.             If _dbName = value Then Return
    35.             _dbName = value
    36.             NotifyPropertyChanged(Function() DBName)
    37.         End Set
    38.     End Property
    39.  
    40.     Private _dbAddress As String
    41.     Public Property DBAddress As String
    42.         Get
    43.             Return _dbAddress
    44.         End Get
    45.         Set(value As String)
    46.             If _dbAddress = value Then Return
    47.             _dbAddress = value
    48.             NotifyPropertyChanged(Function() DBAddress)
    49.         End Set
    50.     End Property
    51.  
    52.     Private _dbRegion As String
    53.     Public Property DBRegion As String
    54.         Get
    55.             Return _dbRegion
    56.         End Get
    57.         Set(value As String)
    58.             If _dbRegion = value Then Return
    59.             _dbRegion = value
    60.             NotifyPropertyChanged(Function() DBRegion)
    61.         End Set
    62.     End Property
    63.  
    64.     Public Sub New(lsEnvName As String, lsRegionCode As String, lsDBName As String, lsDBAddress As String, lsDBRegion As String)
    65.         _envName = lsEnvName
    66.         _regionCode = lsRegionCode
    67.         _dbName = lsDBName
    68.         _dbAddress = lsDBAddress
    69.         _dbRegion = lsDBRegion
    70.     End Sub
    71.  
    72. End Class
    Last edited by MattP; Apr 25th, 2012 at 03:57 PM.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Storing data in classes.

    That is really amazing . 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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width