Rank newbie question - Class or Function?

  • Thread starter Thread starter Tony Van
  • Start date Start date
T

Tony Van

A philosophical question I guess....

In VB6, I used a function to take
city, state and zipcode fields and combine
them with a comma, correct formatting
and allowances for null or blank entries.

In VB.NET, its it permissible to still use
a function, or should I make a class to
do this?

Any thoughts.
 
A function (method) is fine for this.
Perhaps a static method that is publicly available would be ideal so
that you can call it at any time in your application.

However if you have a single class that contains these 3 items, it
could easily have a method that will return those items formatted. So
just add the method to the existing class (in this case, not using a
static method). I personally don't see any point to writing an entire
class to handle one specific function, but as your app grows, it may be
more reasonable to do so.

Note: Even a static method needs to sit in a class somewhere, whether
its an existing class or a new one created by you. In VB, static
methods use the "shared" keyword.
 
Note: Even a static method needs to sit in a class somewhere, whether
its an existing class or a new one created by you. In VB, static
methods use the "shared" keyword.

Or even nicer instead of a shared class in VBNet a module in the format of a
class. Here a sample where the function is real something without sense.

\\\
'To show how to use
Public Class Form1
Private Sub Form1_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim LengthOfString As Integer = MyNameFunctions.giveLength("Cor")
End Sub
End Class

'The module
Public Module MyNameFunctions
Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Function giveLength _
(ByVal theString As String) As Integer
Return theString.Length
End Function
End Module
///

This is the same as a shared class, but in my opinion much nicer written. A
shared class is in fact a module, which don't exist in the C languages and
therefore there is constucted something as a static class, where the name
does not cover the cargo (I have myself this insight for some weeks/months
now).

Cor
 
Cor said:
\\\
'The module
Public Module MyNameFunctions
Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Function giveLength _
(ByVal theString As String) As Integer
Return theString.Length
End Function
End Module
///

Why on earth would you want to write the above functions into a module (or a
shared class/function for that matter)?

As soon as a module gets "state" (i.e., it has some storage, the mName
variable in this case) then you immediately open yourself up to potential
problems. What if you called another function that also used the functions
in this module and set the Name property to something it wanted to work
against? Or what if you grew this module and then ended up using it within a
web project running in IIS? In both cases the module would suddenly stop
working, as the value would be unexpectedly changed by other code.

As soon as something has state, it should be moved into a class. The
procedures can then create their own instance of the class and be quite
happy that nothing else will unexpectedly interfere with the data stored
within.

Perhaps this was just being given as an example, but I'm very aware of how
easy it is to start building a project on an example like this, particularly
for a new coder, and the above code could lead to a whole lot of rework
later on when the project grew in complexity...
 
Its great that you can point out the flaws in other people's answers
without actually providing one yourself.

I am not quite sure why you thought the shared method wouldn't work.
There is no state involved. In fact, this example has no dependancies
at all and is perfect even for a unit test.

Public shared function FormatLocation(byval City as string, byval State
as string, byval ZipCode as string) as String

Dim location as string = City.Trim() & ", " & State.ToUpper().Trim()
& " " & ZipCode
Return location
end function


I think it would be better if you provided input where it was useful,
as opposed to just writing randomly. The OP would actually get
something out of it. All you seemed to do was create confusion.
 
Steven said:
Its great that you can point out the flaws in other people's answers
without actually providing one yourself.

I don't think there was anything else particularly to answer on this thread,
the original question had been addressed.

But the example provided was one that I considered "dangerous" (on a very
minor scale). I've spent a heck of a lot of time over the last two years
working out the best ways of writing code that needs to run in various
different environments (WinForms, IIS) and if I'd known which techniques to
use and to avoid at the beginning then I'd have found the whole experience
much more straightforward.

My only intention when pointing out the flaw in Cor's example procedure was
to help the OP to try to avoid a way of writing code that could cause him a
lot of difficult-to-recreate problems down the line, resulting in a
potentially large amount of wasted time tracking them down. Would you be
happy including the module that was provided into your project? As a class
it would have been fine, but as a module it is not.

I've provided my fair share of answers to this newsgroup by the way, not to
anything like the extent of others such as Herfried and Cor or course, but
I'm hardly just sitting here criticising.
I am not quite sure why you thought the shared method wouldn't work.
There is no state involved.

I have no problem with a shared method. I use lots of shared methods. I have
a problem with a module that has a module-level variable. In that case,
state most definitely was involved. As I said in my previous post, any call
to another function that also tried to use the module to store its own name,
or use of this in a multi-threaded environment such as IIS, would both cause
problems that would be unexpected and hard to track down.
In fact, this example has no dependancies
at all and is perfect even for a unit test.
Public shared function FormatLocation(byval City as string, byval
State as string, byval ZipCode as string) as String

Dim location as string = City.Trim() & ", " & State.ToUpper().Trim()
& " " & ZipCode
Return location
end function

That looks absolutely fine to me, but that's not the code that I was
responding to. I have no problem at all with your example above which is, as
you say, stateless.
I think it would be better if you provided input where it was useful,
as opposed to just writing randomly. The OP would actually get
something out of it. All you seemed to do was create confusion.

I thought I was providing useful input. If you re-read my message, I don't
think the tone was particularly aggressive or confrontational, unlike your
own. I'd hate to think I confused anyone, but I'm as certain as I can be
that the OP would get confused if his code started acting strangely after
he'd been building upon it for months.
 
Back
Top