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
418 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SqlServer no dupes 25 37
Checking a checkbox based on SQL DataReader boolean value in ASP.NET(VB) 2 32
VB.NET 2008 Winforms Signing 13 31
checkbox to hide entire section 10 41
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

840 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