Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Access Class programming

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
Can anyone explain please..

If I have a Class like below
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Categories
#Region "Declarations"
	Private _CategoryID As Integer
	Private _CategoryName As String
	Private _Description As String	
#End Region
#Region "Properties"
	Public Property CategoryID() As Integer
		Get
			Return _CategoryID
		End Get
		Set(ByVal Value as Integer)
			_CategoryID = Value
		End Set
	End Property
	Public Property CategoryName() As String
		Get
			Return _CategoryName
		End Get
		Set(ByVal Value as String)
			_CategoryName = Value
		End Set
	End Property
	Public Property Description() As String
		Get
			Return _Description
		End Get
		Set(ByVal Value as String)
			_Description = Value
		End Set
	End Property

#End Region
#Region "NewClass"
Public Sub New(	ByVal _CategoryID As Integer, _
		ByVal _CategoryName As String, _
		ByVal _Description As String)
			CategoryID = _CategoryID
			CategoryName = _CategoryName
			Description = _Description
			
End Sub


'...............
'...............
'...............
#End Region
End Class

When I create a new instance of the class I have to use like
Code:
Dim newcls As New Categories(1, "Cars", "Car Description")

What is the difference if I declare the New Instance in the Class without any "ByVal" clause?
Code:
Public Sub New()
	CategoryID = _CategoryID
	CategoryName = _CategoryName
	Description = _Description	
End Sub
declaring the vaiables where I instaniate a new class like
Code:
    Dim newcls As New Categories()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim CName As String = "Cars"
        With newcls
            .CategoryName = CName
        End With

        MessageBox.Show(newcls.CategoryName)
    End Sub
In this approach I don't have to give all the data..
Which one is correct?
I am missing something here..

Any place where I can read more on this?



________________________________________________________
Zameer Abdulla
Help to find Missing people
 
First of all naming a class should always be singular so category and not categories. Except if you make it a collectionclass.

This

Code:
Public Sub New()
    CategoryID = _CategoryID
    CategoryName = _CategoryName
    Description = _Description    
End Sub

Will not really work since you are putting in the values from the local/private variables which are empty in the beginning.

As seen here

Code:
Private _CategoryID As Integer
    Private _CategoryName As String
    Private _Description As String

Better would it be to write this.

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  Categoryid = ""
  categoryname = ""
  description = ""
end sub
#end region

This way you have an empty constructor and a normal constructor with all the fields which should be the normal way of working, this is the minimum.

The following will also work and is shorter.

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  me.new("","","")
end sub
#end region

You could of course write more then two constructors.

like so

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  me.new("","","")
end sub

category and not categories. Except if you make it a collectionclass.

This

[code]
Public Sub New()
    CategoryID = _CategoryID
    CategoryName = _CategoryName
    Description = _Description    
End Sub

Will not really work since you are putting in the values from the local/private variables which are empty in the beginning.

As seen here

Code:
Private _CategoryID As Integer
    Private _CategoryName As String
    Private _Description As String

Better would it be to write this.

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  Categoryid = ""
  categoryname = ""
  description = ""
end sub
#end region

This way you have an empty constructor and a normal constructor with all the fields which should be the normal way of working, this is the minimum.

The following will also work and is shorter.

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  me.new("","","")
end sub
#end region

You could of course write more then two constructors.

like so

Code:
#Region "NewClass"
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

public sub new()
  me.new("","","")
end sub

Public Sub New(ByVal _CategoryName As String)
  me.new("",_categoryname,"")            
End Sub
#end region

I hope that clears things up

BTW the variabels used int the constructor with the same name as the private members are not the same.




Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Thanks Chrissie..
>>First of all naming a class should always be singular so category and not categories. Except if you make it a collectionclass.

Actually I am working on a Class generator that will produce data access classes from a selected Access.mdb. So the Class Name is taken as the selected table/Query Name.

>>.You could of course write more then two constructors.

So when you create more constructors don't you need a "Shared" or "Overridable" key word ? I know "Sub New()" won't allow these key words. How the app recognize it or how will I call them ?

>>BTW the variabels used int the constructor with the same name as the private members are not the same.

Can you please explain ?

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
<< Actually I am working on a Class generator that will produce data access classes from a selected Access.mdb. So the Class Name is taken as the selected table/Query Name.

Ok, not good but if you can live with it then I can.

<< So when you create more constructors don't you need a "Shared" or "Overridable" key word ? I know "Sub New()" won't allow these key words. How the app recognize it or how will I call them ?

They get overloaded there is an overloads keyword but you don't have to type it. As long as the parameter-signature is different it knows that you mean overloading. Overloading is The same method with a different parameter-signature. I'm sure you have seen that thousands of times in the framework. Like in dim _con as new sqlconnection(...) you have different option there of different constructors.

So this won't work

Code:
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

Public Sub New(    ByVal _CategoryID2 As Integer, _
        ByVal _CategoryName2 As String, _
        ByVal _Description2 As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

<< Can you please explain ?

I had the expression that yuo think that the members used here

Code:
#Region "Declarations"
    Private _CategoryID As Integer
    Private _CategoryName As String
    Private _Description As String    
#End Region

are the same as the ones with the same name that you use here.

Code:
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

try this

Code:
#Region "Declarations"
    Private _CategoryID As Integer = 10
    Private _CategoryName As String = "string1"
    Private _Description As String = "description"
#End Region

with this constructor

Code:
Public Sub New()
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

So
Code:
dim _categories as new Categories()
What do you think the values for categories.categoryname will be?

and then with this constructor

Code:
Public Sub New(    ByVal _CategoryID2 As Integer, _
        ByVal _CategoryName2 As String, _
        ByVal _Description2 As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

So
Code:
dim _categories as new Categories(20,"","test")
What do you think the values categories.categoryname will be?

and then with this constructor

Code:
Public Sub New(    ByVal _CategoryID As Integer, _
        ByVal _CategoryName As String, _
        ByVal _Description As String)
            CategoryID = _CategoryID
            CategoryName = _CategoryName
            Description = _Description
            
End Sub

So
Code:
dim _categories as new Categories(20,"","test")
What do you think the values categories.categoryname will be?

Do you understand?




Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
>>They get overloaded there is an overloads keyword but you don't have to type it.

This is what I was confused. It is clear now. Thanks.



________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top