[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Extend vb.Net Class to have New Property

Posted on 2013-05-22
3
Medium Priority
?
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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