Public Function Question

lorena

Centurion
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I have a little function that just checks to see whether a database field is a date or null and I wanted to be able to make it accessible from all the forms on the site it is attached to - so I did this:
Visual Basic:
Public Class ShortDateClass
  Public Function showShortDate(ByVal aDate As Object)
    If Not IsDBNull(aDate) Then
      Return aDate.ToShortDateString
    End If
  End Function
End Class
My question is this: how do I access this function from within a datagrid, for example? This is the code I used but it doesn't work:
Visual Basic:
<asp:TemplateColumn HeaderText="Sched Complete Date">
<ItemTemplate>
<asp:Label ID="lblSchedCompDate" Runat="server"    text='<%#ShortDateClass.showShortDate(DataBinder.Eval(Container.DataItem,"sched_comp_date"))%>'/>
</ItemTemplate>
</asp:TemplateColumn>
help?
 
Trying making it a shared (static) function.

You can then access it with ClassName.FunctionName without having to create an instance of the class first.
 
Where do I actually put the code? Do I create a separate Class Module or put it in the Global.asax file?
Would this be the correct code?
Visual Basic:
Public Class ShortDateClass
  Public Shared Function showShortDate(ByVal aDate As Object)
    If Not IsDBNull(aDate) Then
      Return aDate.ToShortDateString
    End If
  End Function
End Class
 
If it's in the same namespace it should not matter. If it's in a different namespace then you need to call it with MyNamespace.MyClass.MyFunction
 
Back
Top