Results 1 to 3 of 3

Thread: Looking for Guidance on Classes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Looking for Guidance on Classes

    I have done a fair amount of VB.NET programming over the past 4 years, mostly in conjunction with SQL Server using SQLClient class. I have never used classes in my programming. A few times I used Structures when I needed to pass multiple attributes of an entity to a function or two. And I have used XML fragments in a similar way, using XML to group togther a bunch of info and pass the XML to a sub or function.

    Classes seem to be such a fundamental aspect of VB programming yet for some reason they are intimidating. I understand the role of Classes and why to use them, but i am curious how people use classes in conjunction with SQL Server. Can anyone point me to some examples of tutorials?

    For example, do developers use functions that read a bunch of data from SQL, assign values to an object's properties, and then return that object for other uses?

    Maybe this should be moved to th beginner forum.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Looking for Guidance on Classes

    You'd probably get more eyes in the .NET forum, but considering that you explicitly asked about classes and SQL, your choice of forums is understandable.

    For example, do developers use functions that read a bunch of data from SQL, assign values to an object's properties, and then return that object for other uses?
    The answer to this is: Yes! That is a common way to do things. In fact, you have done this yourself, since you claim to have done a fair amount of VB.NET programming. Consider that pretty nearly everything in .NET is a class, and you will realize that you have used quite a number of classes. Every form is a class, as you may have noticed, since the first line in any form code file is always

    Public Class <your form name here>

    You have also used a bunch of other DB related classes. Perhaps you have used Connection objects, Command objects, DataReaders, DataAdapters, DataSets, DataTables, or some such. Each is a class. Each has properties and methods that you can work with, and each encapsulates certain data and functionality that you can make use of throughout your program. Therefore, you have used lots of classes.

    One fairly common technique is to have a class that populates a dataset with a series of datatables. These datatables might be pick lists, they might be views of the database, or something else. The class that holds the dataset then has a series of methods that other parts of the program can call to get values from pick lists, get filtered views of datatables, update records in some of the tables, and so forth.

    Another technique that is used is to have an object that encapsulates roughly one record out of a database table. It might have properties for all the fields in that record, and it might have other methods and functionality that enfoce rules about how those fields can be changed. For instance, you might have a rule that fieldA > fieldB, such that if the user tries to put a value into fieldA that is smaller than the value in fieldB, the change is denied. Once you have such a class designed, you could create a datareader to read all the records from the DB table that the class encapsulates, and for each record returned by the datareader, you could create a new instance of that class to hold the information from that record. The program could then work with those instances for altering them, querying them, editing them, and so forth, then at a later date they could all be pushed back to the database.

    So, the short answer is YES, and the long answer is....well.....long.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: Looking for Guidance on Classes

    Another technique that is used is to have an object that encapsulates roughly one record out of a database table.
    This was exactly the info I was after and is a confirmation of the direction I am going. Thank you! It seemed to make sense to build a class around different sets of data rather than a series of independent variables.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width