Link to home
Start Free TrialLog in
Avatar of pamsauto
pamsauto

asked on

Extend vb.Net Class to have New Property

How do you add an new property to an existing .Net Class.   What I want ot do exactlyis add a property to the dateTime class.

The goal would be like this.  
 
Dim intWeek as integer = DateTime.Now.WeekOfMonth

Open in new window


Here is the code I use to get the week of the month.

 Dim DayNo As Integer = 0
                Dim DateNo As Integer = 0
                Dim StartingDay As Integer = 0
                Dim PayPerEndDate As Date = DateTime.Now
                Dim week As Integer = 1
                StartingDay = PayPerEndDate.AddDays(-(PayPerEndDate.Day - 1)).DayOfWeek
                DayNo = PayPerEndDate.DayOfWeek
                DateNo = PayPerEndDate.Day
                week = Fix(DateNo / 7)
                If DateNo Mod 7 > 0 Then
                    week += 1
                End If
                If StartingDay > DayNo Then
                    week += 1
                End If

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

How do you add an new property to an existing .Net Class.
You can't...at least not without subclassing it--if it's not sealed. It appears you want to add a member to the Now property, which is a DateTime struct. Structs cannot be inherited. You could add and extension method that would *appear* to be a member method, but in reality it's just a static (Shared in VB) method.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pamsauto
pamsauto

ASKER

Very nice!