Classes

G

Greg

I have the following class as such.

Class EmployeeClass
Private m_FirstName as string
Public Property FirstName () as String
Get
Return m_FirstName
End Get
Set(ByVal Value as String)
m_FirstName = Value
End Set
End Class

In my code I want to setup an array containing this class object. So I do
the following.

dim objEmployee(2) as EmployeeClass
objEmployee(0) = New EmployeeClass
objEmployee(0).FirstName = "Greg"
objEmployee(1) = New EmployeeClass
objEmployee(1).FirstName = "John"

Now, I have two employees in this EmployeeClass object and I am going to
pass it to a function for the Employee's in the object to be inserted into my
database. So, I do it like this.

Function InsertEmployee(byval objEmployee as EmployeeClass) as boolean
' Step through each Employee record and append to database.
End Function

I would pass my EmployeeClass object like this, InsertEmployee(objEmployee).

Now, I want to do a few things inside this funciton.

1. How can I find out how many Employee records exist.
2. How can Determine which properties exist in this class. For example, in
my case I have the FirstName property, but if I did not know what it was, how
could I step through each property.
3. And, lastly, as I determine each property above, how could I determine
what type of property each is.

Maybe once I understand how to do this I might be able to do what I want.

What I have done in the past is create one single save rountine that can
save data to all of my tables through one single utility. Maybe if I can get
this part worked out, i might be able to build one common Save utility.

Thanks.
 
S

Scott M.

See responses inline....


Greg said:
I have the following class as such.

Class EmployeeClass
Private m_FirstName as string
Public Property FirstName () as String
Get
Return m_FirstName
End Get
Set(ByVal Value as String)
m_FirstName = Value
End Set

FYI - you are missing an "End Property" line right here.
End Class

In my code I want to setup an array containing this class object. So I do
the following.

In VB .NET, when you delcare arrays, the number you pass is NOT meant to
represent the size of the array. It is meant to indicate the highest index
you'll need for your array. So, your declaration:
Dim objEmployee(2), would mean that you'd get an array big enough to hold up
to index 2 - that means 3 Employee classes. In your example below, you
should say: Dim objEmployee(1) if you only want to store 2 employee classes.
dim objEmployee(2) as EmployeeClass
objEmployee(0) = New EmployeeClass
objEmployee(0).FirstName = "Greg"
objEmployee(1) = New EmployeeClass
objEmployee(1).FirstName = "John"

Now, I have two employees in this EmployeeClass object and I am going to
pass it to a function for the Employee's in the object to be inserted into
my
database. So, I do it like this.

Function InsertEmployee(byval objEmployee as EmployeeClass) as boolean
' Step through each Employee record and append to database.
End Function

I would pass my EmployeeClass object like this,
InsertEmployee(objEmployee).

Now, I want to do a few things inside this funciton.

1. How can I find out how many Employee records exist.

Every array (regardless of what's in it) has a "Length" property, which
returns a 1-based count of how many items are in the array.
2. How can Determine which properties exist in this class. For example, in
my case I have the FirstName property, but if I did not know what it was,
how
could I step through each property.

You'd need to use the classes in System.Reflection to iterate the class
members.
3. And, lastly, as I determine each property above, how could I determine
what type of property each is.

Same answer as #2.
Maybe once I understand how to do this I might be able to do what I want.

What I have done in the past is create one single save rountine that can
save data to all of my tables through one single utility. Maybe if I can
get
this part worked out, i might be able to build one common Save utility.

Perhaps, but because .NET is all about object (classes), what would be more
likely (and probably more appropriate) is to build a base class to take care
of your basic data access and then build sub-classes using inheritance to
extend that base class for whatever scenario you find yourself in.

You could also look into Generics as a possible solution to this problem.

As a last FYI, when although you can certainly store your objects in an
array, you may want to look at the various .NET collection classes (both
Generic and non-Generic) as collections offer more robust methods for
working with objects, rather than primitive types.
 
C

Cor Ligthert[MVP]

Greg,

I would have expected this question from Scott as well so I had to read is
complete answer.

Why do you not use the standard predefined classes for DataBase handling in
AdoNet or in Linq?

(Or it should be to find out all that is in that you don't have to do
yourself)

Cor
 
S

Scott M.

Why would you have expected that question from me as I have been using and
teaching .NET since the early beta days?
 
C

Cor Ligthert[MVP]

Scott,
Why would you have expected that question from me as I have been using and
teaching .NET since the early beta days?
Because I know that and your answers become better and better, as you had
written it, then I would not have done it.

Cor
 
S

Scott M.

I don't understand what that means Cor.


Cor Ligthert said:
Scott,

Because I know that and your answers become better and better, as you had
written it, then I would not have done it.

Cor
 
C

Cor Ligthert[MVP]

,
I don't understand what that means Cor.
Instead of creating your own classes you can use DataSets or DBML classes.

In my idea much easier, I thought that I had seen such an answer once from
you, therefore I have checked it before I wrote my message in your answer if
you had done that and than I would not have given an answer. That does not
mean that your answer is wrong.

Cor
 
C

Cor Ligthert[MVP]

Retry,

Scott, don't think it was meant to discourage you, it was totaly opposite
from that.

I have checked if you had given the answer that I wanted to give already.

Cor
 
P

Phill W.

Greg said:
Class EmployeeClass
Private m_FirstName as string
Public Property FirstName () as String
Get
Set(ByVal Value as String)
Function InsertEmployee(byval objEmployee as EmployeeClass) as boolean
' Step through each Employee record and append to database.
End Function

I would suggest that a better way would be to add a method to the
Employee class that saves it's own contents to the database.
1. How can I find out how many Employee records exist.

I assume you need to know the "next" Employee Number to allocate to this
new Employee? I would recommend a "last number" table to hold the last
ID allocated. Whenever you add a new Employee, increment this value.

Sub NewExployee()
"update last_emp_no set emp_no = emp_no + 1;"
"select emp_no from last_emp_no"
"insert into employees( emp_no, ... _
) values (" & emp_no & ", ... )"
2. How can Determine which properties exist in this class. For example, in
my case I have the FirstName property, but if I did not know what it was, how
could I step through each property.

This is another good reason for putting the Save logic /into/ the
Employee Class. This kind of "generic" property harvesting is (a)
fiddly, (b) likely to cause you headaches later on, especially if you
start working with "cleverer" data types, and (c) IMHO, better avoided,
by working with a /known/ quantity, i.e. the Employee /class/ knows how
to save (and reload) /itself/; nobody else needs to know what's inside
it.
3. And, lastly, as I determine each property above, how could I determine
what type of property each is.

You don't need to or want to; see (2)
What I have done in the past is create one single save rountine that can
save data to all of my tables through one single utility. Maybe if I can get
this part worked out, i might be able to build one common Save utility.

I would suggest that that your "utility" will now be reduced to "helper
functions" for, say, constructing the SQL needed to do the updates and
that these functions will be called from the Save routine in each class
(e.g. a Manager [object] might have more (/less?) information to Save
than a "regular Joe" Employee [object]).

class Employee
public overridable Sub Save( _
ByVal saver as SomeObjectThatAllowsSaving _
)
Dim helper as new SqlHelper( "update", "EmployeeTable" )

helper.Add( "FirstName", m_FirstName )
. . .

saver.Save( helper.GetConstructedSql() )

end sub
end class

HTH,
Phill W.
 
M

Michel Posseth [MCP]

Well let me try to explain this comunication disturbance between you :)

I would have expected this question from Scott as well so I had to read is
complete answer.
Why do you not use the standard predefined classes for DataBase handling in
AdoNet or in Linq?
(Or it should be to find out all that is in that you don't have to do
yourself)

<Modified by michel :)>

Greg,

Why do you not use the standard predefined classes for DataBase handling in
Ado.Net or in Linq? ( or do you want to write all what is in these classes
provided )

I would have expected this question from Scott

Cor

</Modified by michel :)>

Maybe it is because i am also Dutch and i inmediatly understood what Cor
meant to say , however i also saw how it could be wrong interpreted , as
if Cor expected the same question as the TS asked from Scott .


Hope that it is clear now where the fog between you two was :)

Michel Posseth
 

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