A
Andy B.
Is there anywhere I can go to read the best guidelines (if any) on when to
use classes or modules?
use classes or modules?
Andy B. said:Is there anywhere I can go to read the best guidelines (if any) on when to
use classes or modules?
So, for example of a shared method would be something like:
Public class NewsManager
Private DBContext as new EternityRecordsEntities()
public Shared Function GetNewsArticle(NewsID as Guid) as ObjectQuery(Of
NewsArticles)
return DBContext.NewsArticles.Where("ID = @NewsID", New
ObjectParameter("NewsID", NewsID))
End Function
End class
Then later in some code for databind as an example:
NewsDataList.DataSource = NewsManager.GetNewsArticle(NewsID)
NewsDataList.DataBind()
Is this right? And if for some reason I have instance methods mixed up in a
class with shared methods, then what do I do?
Andy B. said:Thanks. This explains things a lot better. Now the question would be what
guidelines do you use when developing shared methods or instance methods?
Well, take a class called Car. It would make sense that this class would
have an instance method called Color, because that varies per instance.
It would make sense that it may have a shared method called Wheels,
because all cars have the same number of wheels (for sake of
argument...). Basically it boils down to me whether you expect a
different value per instance or not.
Michael D. Ober said:MichaelC,
Given your apparent attitude towards VB.NET in the thread you started on
Linq killing VB, I think your advice to never use a module is way off
track.
Andy,
Use modules for globally useful functions and subs that don't require
saved state between calls. Yes, you can use a Static variable inside the
function, but if you are saving state between calls, chances are you're
really looking at an object. In C#, you have Namespaces and Classes as
the primary scoping control. In VB, you have Namespaces, Modules, and
Classes. A module is a hybrid between a namespace and a static class with
all it's methods shared by default. You can reference functions in
modules using either just the function name or "module.function" syntax.
The latter is useful in that other dotNET languages can call your function
(in a dll) using the module.function syntax. If you have functions in
your module that aren't for public consumption, make the Private, just
like you would for a class.
I disagree with your assertion to "never" use modules. There may well be aMichael C said:It's pretty simple, never use modules. If you need somewhere to put
general purpose functions then put them as shared functions in a class.
The reason you should not use modules is that their methods become global
which is something we should be trying to get away from in dot net. Shared
functions in a class are pretty much the same thing but you need to
qualify them with the class name.
Michael
Andy B. said:Hi.
The methods being used fit the following guidelines.
1. They wont be seen or maintained by anyone else but me.
2. They are for the applications use only.
3. They all will do some sort of database operation. The current set of
methods I'm working on now relate to the news feature of the application.
Other ones will deal with Events, mailing lists, settings and so on.
Would shared methods be better or modules?
Shared methods. In some *very* rare cases it could be argued that you
should
use a module but for something like accessing the database these should be
in a shared function in a class.
Is there anywhere I can go to read the best guidelines (if any) on
when to use classes or modules?
Cor Ligthert said:Why *should* whatever (beside the main) be in a shared function of a
class.
By instance a fill is simple a sub not a function.
Because there is not any need to do it shared, as it can be instanced as
well.
It is OOP in managed code you know.
An object exist as long as it is used automaticly which is managed by the
managed code.
(I get the idea that you are still programming in VB6 style)
Harry Strybos said:I disagree with your assertion to "never" use modules. There may well be a
case where you wish to put in place some class library specific
functionality that you do not want exposed publically.
Rory Becker said:Apologies if it's already been mentioned elsewhere in this thread. ( I
can't find a reference but I only had a cursory look)
It is actually a pre-requisite that Extension Methods be contained in
Modules, and whilst some people object to their use, it remains a fact
that you simply must use Modules if you want to use them.
I am so glad that you are active in this newsgroup.I didn't realise that, the more I know about VB the less I like.
Cor Ligthert said:Michael,
I am so glad that you are active in this newsgroup.
The more you write, the more I (and probably others) see the advantages of
VB