Friday, March 9, 2012

Function like ToString

Hi

I want to deleveop a dateTime datatype for Arabic Calender.

how can i write a fucntion like ToString() function?

select ArabicDateTime::GetDate().ToWeek()

thanks every body

I don't quite understand what you are looking for. You can write your own User Defined Types (UDT's), and you can do it using the CLR. Look in BOL about UDT's for that. In that scenario, you would then just write a method on the UDT which would be whatever you wanted it to be.

Niels
|||Thank you nielsb

I write function ToString() like this

VB Code

Public Overrides Function ToString() As String
Return Me.Year & "/" & Me.Month & "/" & Me.Day
End Function


and i can call this Function in SQL Server :

SQL Code

select PersianDateTime::GetDate().ToString()



now i want to write a function named Toweeday() and call it in SQL server like this:

Code Snippet

select PersianDateTime::GetDate().Toweeday()



but i've problem. my Function Code is:


Code Snippet

public Overrides Function Toweeday() As String

Return Me.Yearname & " " & Me.Monthname & " " & Me.Dayname
End Function



VB.Net notify me to Delete Overrides Keyword. and i must use Shared keyword. and when i use Shared keyword then i cant use Me.Yearname.

thank you agin

|||

Mohsen,

Just remove the Overrides keyword and don't use Shared either. You only need Overrides when there is a method on the base class that you wish to replace the functionality of in your base class. Since Toweeday is not defined on the the base class of your class, you don't need to override it. Object is the base class for all classes, and it is where ToString is defined, which is why you need Overrides for ToString. Shared makes the method an aspect of the class not any particular instance of the class, so it cannot access instance fields. So, don't use that if you want to define a method which accesses instance fields.

If you would like to see an example of DateTime for other calendar systems, you might want to look at the CADateTime sample at http://www.codeplex.com/MSFTEngProdSamples/Wiki/View.aspx?title=SS2005%21Calendar-Aware%20Date%2fTime%20UDTs&referringTitle=Home.

|||thank you so much. my promlem solved.

i test it befor i make this topic but i catch this erorr

Msg 6573, Level 16, State 1, Line 0
Method, property or field 'ToWeekday' of class 'ArbicDateTime.ArabicDateTime' in assembly 'ArbicDateTime' is not static.

but now there isn't any problem. why?

No comments:

Post a Comment