Solved

How to Write a New Address sub-Class that Uses a DataReader and Can be Passed to a class in VB.NET

Posted on 2011-02-15
6
403 Views
Last Modified: 2012-05-11
I am attempting to write my attached code in a method that allows one to reuse the code to only call the address and related fields once and have a header class inherit the sub-address classes. I am a little confused on how to write the sub-address class to also have a DataReader on it so that it can be used when the original class is used to read in data from a SQL Server Database.
Namespace PO

    Public Class EDIAckHeader

        Dim strRecordType As String
        Dim strCustomerNumber As String
        Dim strChangeFlag As String
        Dim strOrderNumber As String
        Dim strOrderDate As String
        Dim strShipVia As String
        Dim strCustomerPONumber As String
        Dim strCustomerPOReleaseNumber As String
        Dim strPODate As String
        Dim strSystemDate As String
        Dim strSystemTime As String
        Dim strGSSenderID As String
        Dim strTD5DNSNumber As String
        Dim strTradingPartnerName As String
        Dim strDocument_TransactionID As String
        Dim strReceiverID As String
        Dim strSenderID As String
        Dim strSupplierName As String
        Dim strSupplierAddress1 As String
        Dim strSupplierAddress2 As String
        Dim strSupplierCity As String
        Dim strSupplierState As String
        Dim strSupplierZipCode As String
        Dim strSupplierCountry As String
        Dim strSupplierIDQualifier As String
        Dim strShipperName As String
        Dim strShipperAddress1 As String
        Dim strShipperAddress2 As String
        Dim strShipperCity As String
        Dim strShipperState As String
        Dim strShipperZipCode As String
        Dim strShipperCountry As String
        Dim strShipperParterID As String
        Dim strShipperIDQualifier As String
        Dim strReceiverName As String
        Dim strReceiverAddress1 As String
        Dim strReceiverAddress2 As String
        Dim strReceiverCity As String
        Dim strReceiverState As String
        Dim strReceiverCountry As String
        Dim strReceiverParterID As String
        Dim strReceiverIDQualifier As String

        Private Sub New()
            ' Instantiate the output variables that will be used by the class
            strRecordType = ""
            strCustomerNumber = ""
            strChangeFlag = ""
            strOrderNumber = ""
            strOrderDate = ""
            strShipVia = ""
            strCustomerPONumber = ""
            strCustomerPOReleaseNumber = ""
            strPODate = ""
            strSystemDate = ""
            strSystemTime = ""
            strGSSenderID = ""
            strTD5DNSNumber = ""
            strTradingPartnerName = ""
            strDocument_TransactionID = ""
            strReceiverID = ""
            strSenderID = ""
            strSupplierName = ""
            strSupplierAddress1 = ""
            strSupplierAddress2 = ""
            strSupplierCity = ""
            strSupplierState = ""
            strSupplierZipCode = ""
            strSupplierCountry = ""
            strSupplierIDQualifier = ""
            strShipperName = ""
            strShipperAddress1 = ""
            strShipperAddress2 = ""
            strShipperCity = ""
            strShipperState = ""
            strShipperZipCode = ""
            strShipperCountry = ""
            strShipperParterID = ""
            strShipperIDQualifier = ""
            strReceiverName = ""
            strReceiverAddress1 = ""
            strReceiverAddress2 = ""
            strReceiverCity = ""
            strReceiverState = ""
            strReceiverCountry = ""
            strReceiverParterID = ""
            strReceiverIDQualifier = ""
        End Sub
    End Class
End Namespace

Open in new window

0
Comment
Question by:thenthorn1010
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 500 total points
ID: 34899032
This is a big question, really. I'll try to help you get started.

Notes:

1. You can simplify default values by assigning them inline with the declaration. This will make your code easier to read (less clutter), and ensure you don't miss any.
2. Assigning "" seems questionable to me. I suspect you'll either find that these values will end up being assigned by your "reader", or you should run each property through a helper method if you really need to know if it's empty. What about IsNothing, "    " and ""?
3. A parent class doesn't inherit children. A child is based on a parent, and thus inherits it. In your case I think you mean "has" or "contains" ... the header has addresses.
4. Create a separate class called Address. Add the required fields to it. Then add as many properties to the header as you need, each of type Address.
5. This is basically an entity. You would not embed any knowledge of persistence in an entity. That would be a terrible thing to do. You want your data layer to know how to read SQL data (for instance) and instantiate it as entities. In the simplest of cases, you'd query the database, create a new entity instance and then populate the properties from the datarow directly.
0
 

Author Comment

by:thenthorn1010
ID: 34899105
Thank you for helping me get started on the process. I have split the one class into the attached two classes to this message posting. My next question is how should the data reader be setup so that it reads in default "Nothing" values to start off with before it passes through the code that will read in the data from the SQL database?
EDIAckHeader.vb
EDIAckAddress.vb
0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 500 total points
ID: 34899287
If you're going to read data into an object and populate every property, why would you want to first initialize them to nothing?

Your entity class is already initializing the values in the constructor, so you'd then be setting them three times ... once in the constructor, then before the reader populates them and finally with the real data.

What am I missing?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:thenthorn1010
ID: 34899398
This is the first time that I have used a DataReader in VB.NET, as I am used to binding the data fields together. SInce this is being done in a class, I am attempting to use a DataReader for the first time. I thought that you still needed to initialize the properties that you have from the entity and the child-class in the constructor. If the DataReader is already setting them equal to IsNothing, than you are correct that nothing needs to be done with the variables in the constructor.

If the constructor does not need to initialize the values, since the DataReader is going to do it, how would you go about declaring the EDIAckHeader class, which was attached to the previous posting, using the DataReader to fill in all of the properties that are inside of the class along with the EDIAckAddress class, wihch is the child class to the EDIAckHeader class?
0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 500 total points
ID: 34899485
It sounds as if you expect the data reader to be able to create entities for you. It can't. It's up to you to instantiate a new header object for each data reader row returned, populate the properties one at a time out of the reader and then add the header to a collection. This assumes there is more than one.

There are other ways to skin this, like Linq to SQL and Entity Framework, which try to take a lot of low level coding out of the picture.

This example uses a helper, but the idea is the same:

http://www.pcreview.co.uk/forums/customer-objects-datareader-bindinglist-customer-t2354461.html

The code gets the data and then creates business objects from it.
0
 

Author Comment

by:thenthorn1010
ID: 34899512
Thanks for the help. All of the info you have provided has given me the direction I needed in order to finish the classes and get the data. Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now