Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Passing a default property to a new class?


Reply
 
Thread Tools Display Modes
  #1  
Old 07-04-2012, 11:31 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,877
Default Passing a default property to a new class?


Maybe I am overthinking this...

I am making a new class that requires a lot of input information.
Some of the properties need to be set to a default value, however I do not want to hard code them into the class.

What do you think would be the most efficient way to populate these properties?

I've been thinking of making a new class to hold all the default props and pass them as one object into the new class as a single argument but it seems kind of clunky.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #2  
Old 07-04-2012, 01:17 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

That's actually one of the more convenient ways to go about it. If you do it that way, you can have several pre-configured instances of the configuration class to make it easier on yourself. Consider this approach using an imaginary BackgroundWorker class since it has 2 Boolean properties you might want to configure:
Code:
Class BackgroundWorkerConfiguration
    Public Property ReportsProgress As Boolean
    Public Property SupportsCancellation As Boolean

    Public Shared ReadOnly Property WithProgressAndCancellation As BackgroundWorkerConfiguration
        Get
            Dim value As New BackgroundWorkerConfiguration()
            value.ReportsProgress = True
            value.SupportsCancellation = True
            Return value
        End Get
    End Property
End Class

' somewhere in code:
Dim worker As New BackgroundWorker(BackgroundWorkerConfiguration.WithProgressAndCancellation)
An alternative is using a factory pattern. Make a class with methods that does the icky configuration for you:
Code:
Class BackgroundWorkerFactory

    Public Shared Function WithCancellation() As BackgroundWorker
        Dim value As New BackgroundWorker()
        value.SupportsCancellation = True
        Return value
    End Function
...
Either way, you end up having to do the icky typing work /somewhere/, but at least it's limited to one place.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-04-2012, 03:26 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,877
Default

My understanding of the backgroundworker is fuzzy at best so your example poses more questions than answers for me.

This is what I was thinking.
Code:
Public Class Form1() Private oCarDefaults as New VehicleDefaults(4,4,false) '... 'Elsewhere in an event : oCarDefaults = New VehicleDefaults(6,2,True) Then create a slew based on these defaults. End Class Public Class Vehicle Public Property Defaults = Nothing Public Property VehicleColor as color Public Property License as string = "" '... Public Sub New(defauits as vehicledefaults, vehiclecolor as color, license as string, ...) Defaults = defaults VehicleColor = vehiclecolor License = license '... End Sub End Class Public Class VehicleDefaults ' This class is created only once. ' Defaults are changed rarely if at all. Public Cylinders as integer = 0 Public Seats as integer = 0 Public Convertable as boolean = false '... Public Sub New(cylanders as integer, seats as integer, convertable as boolean) ' Set Props End Sub End Class

The idea is to reduce the number of parameters and the number of times you have to set them.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->