Solved

Extend vb.Net Class to have New Property

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Very nice!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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:…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now