Inheritance VB.NET

G

Graham Blandford

Hi guys,

Wonder if anyone can help a newbie in VB.net.

I've done a substantial amount of work in VB6 and to promote code use, I
lent some methodology from some other OOP RAD tools - but I'm having a
probem figuring out an approach in VB.Net...

In VB6, I would, for each database table, create a 'class module', which
contained all of the necessary database functions I required.

So, e.g.

I would have a class module called 'tcProvinceState', which would handle all
of my database IO for the ProvinceState table.

In each class module, I would have functions such as 'DoSelect',
'DoSelectRow', 'InsertRow', 'UpdateRow' and 'DeleteRow'.


Here are some examples of the type of thing I currently do;

Public Function Do_Select(pSearchString As String) As ADODB.Recordset

' Select all records from the table

sSQL = Do_Select_Body(pSearchString) 'Get the body

sSQL = sSQL & " ORDER BY PS.PROVINCE_STATE_NAME"

Set Do_Select = clsSQL.Perform_SQL(sSQL)

End Function

Private Function Do_Select_Body(ByRef pSearchString As String) As String

' Get the select body

sSQL = "SELECT"
sSQL = sSQL & " PS.ID"
sSQL = sSQL & ", PS.CODE"
sSQL = sSQL & ", PS.PROVINCE_STATE_NAME"
sSQL = sSQL & ", PS.COUNTRY"
sSQL = sSQL & ", PS.GMT_ZONE"
sSQL = sSQL & ", PS.TAX_GROUP_ID"
sSQL = sSQL & ", TG.CODE AS TAX_GROUP_CODE"
sSQL = sSQL & ", TG.DESCRIPTION AS TAX_GROUP_DESCRIPTION"
sSQL = sSQL & ", PS.FREIGHT_ADJUSTMENT"
sSQL = sSQL & ", PS.RECORD_CREATED"
sSQL = sSQL & ", PS.RECORD_UPDATED"
sSQL = sSQL & ", PS.USER_ID"
sSQL = sSQL & ", EL.USER_NAME AS EMPLOYEE_USER_NAME"
sSQL = sSQL & ", EL.FIRST_NAME AS EMPLOYEE_FIRST_NAME"
sSQL = sSQL & ", EL.LAST_NAME AS EMPLOYEE_LAST_NAME"
sSQL = sSQL & " FROM (("
sSQL = sSQL & "PROVINCE_STATE AS PS"
sSQL = sSQL & " LEFT JOIN EMPLOYEE_LOGIN AS EL ON PS.USER_ID = EL.ID)"
sSQL = sSQL & " LEFT JOIN TAX_GROUP AS TG ON PS.TAX_GROUP_ID = TG.ID)"
sSQL = sSQL & " WHERE 1=1"

Build_Search pSearchString

Do_Select_Body = sSQL

End Function

Public Function Insert_Row(ByRef rsRow As ADODB.Recordset, pID As Long) As
Boolean

' Insert row into the table;

Dim sErrors As String
Dim sDate As Variant
sDate = Format(Now(), "d-mmm-yyyy")

' Use Stored Procedure (SQL Server 2000)

' Initialize the input & output parameters;

Set cStoredProcedure = New ADODB.Command
Set cParam = New ADODB.Parameter

With cStoredProcedure

Set cParam = .CreateParameter("@CODE", adVarChar, adParamInput, 4,
rsRow.Fields("CODE"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@PROVINCE_STATE_NAME", adVarChar,
adParamInput, 30, rsRow.Fields("PROVINCE_STATE_NAME"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@COUNTRY", adVarChar, adParamInput,
20, rsRow.Fields("COUNTRY"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@GMT_ZONE", adSmallInt, adParamInput,
5, rsRow.Fields("GMT_ZONE"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@TAX_GROUP_ID", adInteger,
adParamInput, 8, rsRow.Fields("TAX_GROUP_ID"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@FREIGHT_ADJUSTMENT", adCurrency,
adParamInput, 8, rsRow.Fields("FREIGHT_ADJUSTMENT"))
.Parameters.Append cParam

Set cParam = .CreateParameter("@USER_ID", adInteger, adParamInput,
8, glngUserID)
.Parameters.Append cParam

Set cParam = .CreateParameter("@ID", adInteger, adParamOutput, 8,
pID)
.Parameters.Append cParam


sSQL = "SP_PROVINCE_STATE_INSERT"
clsSQL.Perform_StoredProcedure sSQL, cStoredProcedure, sErrors

End With

' Check Errors
If Len(sErrors) Then
Display_Errors "Insert Failed", sErrors
Insert_Row = False
Exit Function
End If

' Read any output parameters
pID = cStoredProcedure.Parameters("@ID").Value
Insert_Row = True

End Function

With this method, creating a new table and its related 'class' is quite
simple.

Now, although this methodology doesn't have true inheritance, I get some
code re-use and it provided a very good platform for my DB applications.

However, I am now somewhat confused with regards to VB.Net.
..Net would appear to give me some native functionality for things such as
selects, inserts and deletes. I can see how inheritance may truly help me by
allowing me to create a 'master class', but I'm not sure how I would
implement this? - Should I be looking at writing similar code? or does .Net
offer me another option? Ideally I would like to create a visual class such
as a dataset - but from what I can see - this has to be copied from form to
form as required - not inherited.

Anyway, if anyone has any suggestioms, or can point me in the right
direction, I'd very much appreciate it.

All the best,
Graham
 
S

Sahil Malik

Graham,

There is a lot to communicate, but I'll try and be as concise and complete
as I can.

In .NET inheritance opens doors for you. You could create all generic helper
database functions in one class, and inherit other classes from this class;
hence all these helper functions are available in the descendants.
Similarly, if you wanted your datasets to have some common characteristic,
you could inherit them from the dataset class, and have each dataset inherit
from your class instead.

In .NET; just like VB6, you have been given the tools, and whether you
decide to build an empire state out of those, or shoot yourself in the
foot - is entirely your choice. But what .NET gives you is a full object
oriented language (s), that allow you to express your design and
architecture in a much much better way than flat VB6 ever did. (Actually
..NET gives you more than just that).

For a good database access architecture, or "data layer" architecture, while
there are scores of samples on the internet (there is one in my book too -
Chapter #11) - you might also want to check out Microsoft Data access
Application block.

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
 
C

Cor Ligthert

Graham,

Why would you not better first try to use the standard tools in VBNet before
you start with designing your architecture. There are much more tools and
some even done by a designer than in VB6.

When you know the tools the building will be mostly more robust.

Just my thouhgt,

Cor

"Graham Blandford"
 
G

Graham Blandford

Thanks Sahil,

That all makes sense. What I am trying to avoid is the huge learning curve
and years of time and resource involving myself in 'what is the best way' .
I dabbled in VB6 (4,5) for several years - following many authors
recommendations on how to create a 'commercial' application using the
generic tools/controls in the IDE... . resulting in some very slow
development. Then a couple of years ago, I was introduced to a very good
RAD - which was truly OOP - superclassing, inheritance, etc. The development
time was second-to-none - the downside was fewer people will invest in
something that isn't 'mainstream'..

So I adapted what I knew from the RAD to VB6 and came up with a nice
database application framework which, if I say so myself, works very well
and has a nice clean UI - AND I can turnout code at a high rate without
sacrificing quality.

With .Net I see many similarities with the RAD - even down to the
terminology. I have just finished the 1st phase of a small VB.NET project
using OleDB, and found a lot of repetition in my development. For each form
I would have to copy/paste connection/dataadaptor/dataset objects from an
existing form .. and then make adjustments to the select statements, or
maybe add a parameter or two.

I can certainly see the possibilities with .Net, but I am lacking in the
knowledge to determine whoch is the best approach - and let's face it, the
available books that offer to 'Teach yourself VB.Net....' or of this genre,
in my opinion, have no place on the booshelf, if you are considering serious
commercial applications.

Saying that, I have reached a section in 'Murach's' which deals with data
layers!! Go figure!

Anyway, thanks again for you input - it was appreciated.

Regards,
Graham
 
G

Graham Blandford

Cor,

Thanks for replying.
First let me say, I do appreciate your input - I have certainly benefited
from many of your posts, either in direct response or viewing others
problems.

It is not the tools that I have a problem with. VB.Net does certainly
intrigue me, and I am sure it will prove to be ideal for what I am trying to
achieve. What I am unsure of is the methodology to use, how to create
datalayers and truly inherited classes - where to start.

My personal experience is that the literature out on the bookstands is in
general, poor. Most are reworkings of previous versions. If you look at any
one of a dozen 'VB.Net' books currently available, you'll see that the
approach they take is almost identical to VB6?!! That's a little like buying
a race car to pick up the groceries.... Almost all will have a chapter or
two devoted to creating a 'customer entry' form and a 'find customer' form.
Then you proceed to write two pages of very specific code. What *I* am
looking for is how to write very non-specific code, inherit this, and build
my customer form and 'find' form from this base class.... wasn't there a
scripture about "teaching a man to fish...." ;-)


Many thanks again,
Graham
 
C

Cor Ligthert

Graham,

Next week on tuesday there is a chat about AdoNet and VBNet.

Most probably you can meet there Davix Sceppa and when he sees this maybe as
well William B. Vaughn. They both have written books about this.

http://msdn.microsoft.com/chats

Have a look for your self for the time, because of localization and daylight
zones it is mostly impossible to give a time here in this newsgroups.

For the rest I can only agree with you. In my opinion is it for a beginner
almost impossible to see when the documentation is about typed datasets or
about untyped datasets and more of that stuf.

Therefore was my advice to try it yourself first, because in the beginning I
could not find the start, the middle and the end.

It is possible to teach somebody fish with his bare hands, however when you
give him a net or an angle it will probably go easier.

The last part just my thought of course?

Cor
 
W

William \(Bill\) Vaughn

David writes very good books on ADO.NET and I think you'll find Rocky
Lhotka's book on Business Objects helpful. http://www.lhotka.net/

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
C

Cor Ligthert

Bill,

I seldom read books when I can get my information from Internet (and don't
say I should). However I hope somebody else tells that your book is as well
a very good book.
I thought that I saw people write that in this newsgroups.

:)

Cor
 
G

Graham Blandford

Thank you all.

I will be sure to join the online chat and have just placed an order for
Lhotka's Business Objects - looks like exactly what I need.

Best regards,
Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top