|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Rank newbie question - Class or Function?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. > Or even nicer instead of a shared class in VBNet a module in the format of a > 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. > 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 Ligthert [MVP] wrote:
Show quoteHide quote > \\\ Why on earth would you want to write the above functions into a module (or a > '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 > /// 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... -- (O)enone 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 Nagy wrote:
> Its great that you can point out the flaws in other people's answers I don't think there was anything else particularly to answer on this thread, > without actually providing one yourself. 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. I have no problem with a shared method. I use lots of shared methods. I have > There is no state involved. 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 That looks absolutely fine to me, but that's not the code that I was > 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 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, I thought I was providing useful input. If you re-read my message, I don't > as opposed to just writing randomly. The OP would actually get > something out of it. All you seemed to do was create confusion. 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. -- (O)enone
VB Code question
At least one object must implement IComparable How to create multiple threads? Parsing Files with Regular Expressions Suppressing redraw of PictureBox after moving? Dynamically adding in User Controls Using ListView's VirtualMode inverse color DataGridTableStyle Updating DataTable bound to a DataGridView |
|||||||||||||||||||||||