Solved

method that returns a class

Posted on 2004-08-31
3
164 Views
Last Modified: 2010-04-23
not sure how to explain and this might be too vague, but- i am working on a project with another person and am not sure how to use what they wrote since i'm unfamiliar with this level of code. could someone please help explain how i would use a method that is returning a class?  i need to pass a value to the method and am unsure how to do that since it returns a class. i also need to take the results of the method and assign some of the data to labels on a form. if someone has a site with further information, maybe that would help also?
#Region "GetPermit"

      Friend Function GetPermit(ByVal permitId As Integer) As PSI.Common.Permit
            Dim cn As SqlConnection = Me.CreateConnection()
            cn.Open()
            Dim retval As Permit = Me.GetPermitImpl(permitId, cn)
            cn.Close()
            Return retval
      End Function
Private Function GetPermitImpl(ByVal permitId As Integer, ByVal cn As System.Data.SqlClient.SqlConnection) As PSI.Common.Permit
            Dim cmd As New SqlCommand("spPermit", cn)
            Dim dt As New DataTable
            Dim p As Permit
            Dim imgr As New InspectionMgr

            'Permit
            With cmd
                  .CommandType = CommandType.StoredProcedure
                  .Parameters.Add("@PermitId", SqlDbType.Int).Value = permitId
                  .Parameters.Add("@Mode", SqlDbType.VarChar).Value = "SELECT"
            End With

            Dim da As New SqlDataAdapter(cmd)
            da.Fill(dt)

            If dt.Rows.Count = 0 Then
                  cn.Close()
                  Return New Permit
            End If

            p = New Permit(dt.Rows(0))

            'Applicant, Property and Municipality
            p.Applicant = Me.GetApplicant(p.ApplicantIdLink)
            p.Applicant.[Property] = Me.GetProperty(p.Applicant.PropertyIdLink)
            p.Applicant.[Property].Municipality = Me.GetMunicipality(p.Applicant.[Property].MunicipalityIdLink)

            'PermitContractor relationships
            cmd = New SqlCommand("spPermitContractor", cn)
            dt = New DataTable

            With cmd
                  .CommandType = CommandType.StoredProcedure
                  .Parameters.Add("@PermitIdLink", SqlDbType.Int).Value = p.PermitId
                  .Parameters.Add("@Mode", SqlDbType.VarChar).Value = "SELECTBYPERMIT"
            End With

            da = New SqlDataAdapter(cmd)
            da.Fill(dt)

            'Inspections and Contractors
            For i As Integer = 0 To dt.Rows.Count - 1
                  Dim c As New PermitContractor(dt.Rows(i))
                  Dim dt2 As DataTable
                  c.Contractor = Me.GetContractorImpl(c.ContractorIdLink, cn)
                  c.ContractorType = Me.GetContractorType(c.ContractorTypeIdLink, cn)

                  dt2 = imgr.GetInspections(c.PermitContractorId, cn)

                  For ii As Integer = 0 To dt2.Rows.Count - 1
                        Dim ins As New Inspection(dt2.Rows(ii))
                        ins.InspectionType = imgr.GetInspectionType(ins.InspectionTypeIdLink, cn)
                        c.Inspections.Add(ins)
                  Next

                  p.PermitContractors.Add(c)
            Next

            Return p
      End Function
#End Region
0
Comment
Question by:coletteck8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
imu79 earned 400 total points
ID: 11945744
just define an instance of the class PSI.Common.Permit as:

Dim oPermit as PSI.Common.Permit

or

Dim oPermit as Permit
(since that's how its defined in your methods as well)

you just use the methods as you would do for value types such as Integer, String, etc.

oPermit = GetPermit(lPermitID)

Then you can make use of the properties, etc of the Permit object returned to you to do whatever you want to with your labels on the form, etc

As far as using methods is concerned, you can use methods that return an instance of a class just like any other method.

hope this helps..
Imran.
0
 

Author Comment

by:coletteck8
ID: 11953699
i think i need to rephrase my question due to new info. i have a main form, and if you click on a button(showdialog) another form opens with a datagrid. i use the following code in the datagrid form(form2) to get the value of the index when a user clicks a row
  Dim pt = New Point(e.X, e.Y)
        Dim IntID As Integer
        Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)
        If hti.Type = DataGrid.HitTestType.Cell Then
            DataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)
            DataGrid1.Select(hti.Row)
            IntID = CType(RTrim(DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString), Integer)

I need to pass the value above (IntID) into the method in my first question, however, i would like the return to be usable in form1 so that i can populate label boxes with the return. I'm not at all sure how to do this.
0
 

Author Comment

by:coletteck8
ID: 11953865
in addition, the method above in my first question is in a separate class(called PermitMgr) from the two forms.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with adding DLL file in Windows project 20 47
vb.net winforms sizing/resolution? 4 55
"Emulate" TAB key when press Enter Key 3 68
MYSQL responding very slow 3 48
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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