Solved

Extend vb.Net Class to have New Property

Posted on 2013-05-22
3
397 Views
Last Modified: 2013-05-22
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

0
Comment
Question by:pamsauto
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39188391
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.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39188425
An example of an extension method:

Definition
Public Module MyExtensions

    <System.Runtime.CompilerServices.Extension()>
    Public Function WeekOfMonth(source As DateTime) As Integer
        Dim DayNo As Integer = 0
        Dim DateNo As Integer = 0
        Dim StartingDay As Integer = 0
        Dim PayPerEndDate As Date = source
        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

        Return week
    End Function
End Module

Open in new window


Usage
Dim intWeek as integer = DateTime.Now.WeekOfMonth()

Open in new window

0
 

Author Closing Comment

by:pamsauto
ID: 39188435
Very nice!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question