calling custom class from app

Posted on 2005-05-04
Last Modified: 2010-04-23
I have created a custom class which parses an xml document and writes a converted text file to the same directory.

I created a Convert() subroutine in the class which does the actual reading and extraction. I call the subroutine from within the New() constructor since the class is rather simple. It's my (way too inexperienced) understanding that the New() sub gets called each time an instance of the class is created from within the calling application. Is this true?

It doesn't appear to be working however. Before I write a million msgboxes for debug, I thought I would ask if the way I am approaching this is fundamentally flawed in some way. I mean is, is it okay to call the sub which actually does the work from the New() constructor?

Question by:k3n51mm
    LVL 7

    Expert Comment

    Yes , you can do all that. But the purpose of constructors is to initialize the objects. It is all ways better to have it as a seperate method and call the method with the object of that class. IF you want this to be a singleton then the approach is different.

    What part is not working in your code. ?

    LVL 1

    Author Comment

    Here's the  new() sub. szSeddFile below is a global string. The extractA1() sub below is
    being called but does not create output.

    Also, what is a singleton?  

    Public Sub New(ByVal xmlFile As String)

            Dim rdrSEDD As XmlTextReader = New XmlTextReader(xmlFile)
            Dim rdrVal As New XmlValidatingReader(rdrSEDD)
            rdrVal.ValidationType = ValidationType.DTD
            'create event handler for events raised by validator
            AddHandler rdrVal.ValidationEventHandler, AddressOf valEvtHandler
            ' Check whether the document is valid or invalid.
            If isValid Then
                'if successful, perform extraction, write A1 file to disk.
                szSeddFile = seddFile
            End If

        End Sub
    LVL 4

    Expert Comment

    Without knowning what your class is call I'm thinking you are not creating the object from your class

    Dim myObject as New the_name_of_your_class
    Dim mytestData as string

    mytestData = myObject.Convert(Your data)
    LVL 1

    Author Comment

    Actually I have now changed the code. I have made the procedure in the class a boolean function, and it is now returning a value of true or false depending on whether execution completes or not. I believe that the parsing routine is failing and raising an exception.

    To hijack my own thread, I have the followiong question, now that the dynamics of the problem have changed somewhat:

    How do I trap an exception in an external .dll such as this custom class module, and return the exception to the calling application?

    LVL 8

    Expert Comment

    Use try catch in the form and throw an error in the Class.  Do not use any try catches in the class at all.  Not even for dataconnections.  The app will then handle all thrown errors.
    LVL 8

    Accepted Solution

    Throw New Exception(Message)
    : this is what the syntax will be in the class

    You may want to consider creating a new inherited exception class and throw that to the application with a custom signature for example with field in error and message for example.

    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

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    22 Experts available now in Live!

    Get 1:1 Help Now