Solved

Extend vb.Net Class to have New Property

Posted on 2013-05-22
3
383 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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