newbie syntax/best practices question

  • Thread starter Thread starter MattB
  • Start date Start date
M

MattB

Hi. I have a asp.net/vb.net application I'm working on and I want to add
some shared functions for some frequently used code. I've put one in my
global.asax.vb file as that seemed like a good place to call from elsewhere
in the app. Is that an accepted practice? Then, to call it in another
aspx.vb file I stumbled across this way to do it:

dim oGbl as new global
myString = oGbl.MyFunction(arg1, arg2)

Is this a good way to do this? Should I do it another way (for efficency,
speed, etc). That particular function just Returns a string when called.

Also, I'd like to have another function with several properties I could
access, like this:

if oGbl.MyFunction.Success() then
myString = oGbl.MyFunction.RetString
end if

I'm not sure how to declare or assign those properties within the function
code (Success would be boolean)data. Any tips or examples? Thanks!
 
I'd put everything in a class, like this:

Public Class MyStuff
Private Sub New()
'Make sure no one instantiates it!
End Sub

Public Shared Function MyFunction(ByVal a As String, ByVal b As String)
As Boolean
'...
Return True
End Function
End Class

You can easily use it like this:

If MyStuff.MyFunction("a", "b") Then

End If
 
Thanks! That was very helpful. One slight twist here is I'd like to have two
return values for MyFunction for only calling it once. That is why I was
thinking of properties, so it might work like this:

If MyStuff.MyFunction("a", "b").Success() Then
MyString=MyStuff.MyFunction.RetString
End If

Any ideas? Thanks again!

Matt
 
The same mechanism could be used to create properties or functions that
return objects.
Public Class MyStuff
Private Sub New()
'Make sure no one instantiates it!
End Sub

Public Shared Function MyFunction(ByVal a As String, ByVal b As String)
As MyResult
'...
Return ...
End Function
End Class

If MyStuff.MyResult.MyProperty = True Then
....
End If

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Back
Top