Click here to Skip to main content
15,913,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set default value DateTime.Now in my custom Property using the following code:
VB
<DefaultValue(GetType(DateTime), "Now")>

But, it sets a blank value. Please, help me.
Thanks in advance.
Posted
Updated 19-Dec-11 7:37am
v2

Rather than use "Now", you should use something like DateTime.Now.ToString.
 
Share this answer
 
The DefaultValueAttribute does not actually set the property value but just informs the editor what default value has been set in code. Hopefully the example will clarify the concept.

VB
Imports System.ComponentModel

Public Class MyClass
    Private Const InitialValue As Int32 = 37
    Private pvalue As Int32

    Public Sub New()
        pvalue = InitialValue
    End Sub

    <DefaultValue(InitialValue)> _
    Public Property MyProperty() As Int32
        Get
            Return pvalue
        End Get
        Set
            pvalue = value
        End Set
    End Property
End Class


Additionally note that an attribute specifies a constant value so it is not possible to say that a default value is the very variable DateTime.Now.

Alan
 
Share this answer
 
Hi fiend,

Suppose your textbox id is


<asp:TextBox ID="txtDates" runat="server"></asp:TextBox>

call the below code in Page Load will get what u want



txtDates.Text = System.DateTime.Now.ToString("dd/MM/yyyy");


The Format you can change according to your need I am using dd/MM/yyyy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900