vb.net 2005 - OO question...

B

Brad Pears

(keep in mind I am very new to OO design and development etc...)

I have an SQL Server 2000 db table that holds system values. There is only
ONE row in this table. I have created a class specifically for this table.
When a user instantiates the "System_Data" object, I would like that
object's properties to be populated with the values from the SQL Server
"System" table. So, when writing the following code elsewhere in my
project...

dim oSys as new System_Data
msgbox ("Contract Number: " & osys.NextContractNo & " has been created.")

the msgbox should return the next contract number that was stored in the SQL
table.

In order to do this, in the "Sub New" for my System_Data class, I call a
stored procedure that returns all of the fields in that table as output
variables. My next question is this.... Once I have run the stored
procedure, the very next thing I want to do is to assign the values to the
specific properties of my class from the output parameters returned by my
stored procedure. How do I actually assign the values to the properties
without doing something like...

dim osys as New SystemData
osys.NextContractNo =
Convert.ToInt32(objCommand.Partameters("@iNextContractNo"))

Becasue, if I try the above code, as soon as the dim statement is hit, my
"sub new" will run AGAIN and I will be in an infinite loop...

Can someone maybe explain a better way to do this type of thing OR maybe THE
way to do this type of thing?? Maybe I should NEVER be assigning values to a
newly instantiated object at all in the "sub new" procedure for a class?

Help!

Thanks, Brad
 
G

Guest

Brad,

You can assign the values to the object's properties in the sub new
constructor. For example:

Me.NextContractNo =
Convert.ToInt32(objCommand.Partameters("@iNextContractNo"))

Kerry Moorman
 
C

Cor Ligthert [MVP]

Brad,

Is there any reason that you use the classic OO design instead of a dataset
which is completely OOP, however a dataset is as well relational in the way
of the SQL database design. It will save you a lot of work.

If you generate the Dataset using an XSD from your original SQL database
table, than you get all the information from that the SQL datatables in your
dataset, complete with all properties, most methods, and some extra methods
as by instance checking for Null in Null allowed fields.

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Brad said:
(keep in mind I am very new to OO design and development etc...)

I have an SQL Server 2000 db table that holds system values. There is only
ONE row in this table. I have created a class specifically for this table.
When a user instantiates the "System_Data" object, I would like that
object's properties to be populated with the values from the SQL Server
"System" table. So, when writing the following code elsewhere in my
project...

dim oSys as new System_Data
msgbox ("Contract Number: " & osys.NextContractNo & " has been created.")

the msgbox should return the next contract number that was stored in the SQL
table.

In order to do this, in the "Sub New" for my System_Data class, I call a
stored procedure that returns all of the fields in that table as output
variables. My next question is this.... Once I have run the stored
procedure, the very next thing I want to do is to assign the values to the
specific properties of my class from the output parameters returned by my
stored procedure. How do I actually assign the values to the properties
without doing something like...

dim osys as New SystemData
osys.NextContractNo =
Convert.ToInt32(objCommand.Partameters("@iNextContractNo"))

Becasue, if I try the above code, as soon as the dim statement is hit, my
"sub new" will run AGAIN and I will be in an infinite loop...

You don't have to create the object in the constructor, that has already
been done. You are already inside the object, and you use the Me keyword
to access it:

Me.NextContractNo =
Convert.ToInt32(objCommand.Partameters("@iNextContractNo").Value)
Can someone maybe explain a better way to do this type of thing OR maybe THE
way to do this type of thing?? Maybe I should NEVER be assigning values to a
newly instantiated object at all in the "sub new" procedure for a class?

Yes, you should. Normally, the constructor initialises all the members
that need to be initialised.
 
B

Brad Pears

ahh yes... makes perfect sense.. I am already in the object so just
access teh properties using the me keyword... Works like a top!! Thanks for
that!

Brad
 
B

Brad Pears

Well, what I did do was to get rid of the sql output parameters (I was
getting 10 of them) and instead used a dataset which made my sotred
procedure much easier and much less vb coding too!! So I used part of what
you said. I am basically designing a data class for each table (in some
cases it's a related group of tables) and then using my own methods to
add/update/delete etc... Within those classes I am using the dataset object
to do my thing...

Thanks for your comments!

Brad
 
B

Brad Pears

Thanks for your help. Makes perfect sense. I am already in the object so I
just used the "me" keyword as you indicated and voila!! Also thanks for the
reinforcements on what I thought to be true as far as what should or
shouldn;t be done in an object instantiation... I guess it dfepends on what
the object is as far as what you do. In this case, I wanted all of the
system variables available as soon as the system_data class was
instantiated...

Thanks, Brad
Thanks, Brad
 

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