Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Structures

Status
Not open for further replies.

ByNary010101

Programmer
Nov 22, 2004
16
US
I am building an application using VB6 SP6 and have a question about using UDT's in functions. Listed below is some sample code outlining how I would like to use the UDT's


frmContact- Contains 4 textboxes (First Name, Last Name, Phone Number, E-mail Address) and a command button (Save)
Code:
General Declarations
  Private objContact as New clsContact

Private Sub cmdSave_Click
  Dim uContact as udtContact

  With uContact
    .FirstName = text1
    .LastName = text2
    .PhoneNo = text3
    .Email = text4
  End With

  If objContact.AddContact(uContact) = True Then
    Clear the form
  Else
    MsgBox "Data Not Valid"
  End If
End Sub

Contact- this is an ActiveX DLL component that encapsulates the business (clsContact) and data access (clsContactData) logic for the insertion

clsContact
Code:
Option Explicit
  Private objContactData as New clsContactData

Public Function AddContact(uContact as udtContact) as Boolean
  If objContactData.AddContactData(uContact) = True Then
    AddContact = True
  Else
    AddContact = False
  End If
End Function

clsContactData
Code:
Option Explicit

Public Function AddContactData(uContact as udtContact) as Boolean
  Declare the Connection and Command objects
  Execute the Insert Statement
  If Inserted Then
    AddContactData = True
  Else
    AddContactData = False
  End If
End Function

udtContact
Code:
  Public Type udtContact
    FirstName as String
    LastName as String
    PhoneNo as String
    Email as String
  End Type

First question is where should I declare udtContact so that I can use the UDT in both the Form and in DLL??

Second question is how do I pass the entire contents of the UDT to the various functions, for some reason this just isn't clicking with me??

I hope this makes sense. Thanks.
 
Check out this thread, which discusses the use of UDT's in Class modules:

thread222-720091

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Declare it in the ComDll (the class you are going to instanciate) at that point it should show up and you should be able to use it as a valid argument..

ie if you had a class called "stuff"
and a udt of

Public Type Employee
FirstName as string
LastName as string
End type

A function called AddEmployee

Public Function AddEmployee ( employee as employee)


THen using it you could do something like..

Dim x as Stuff
x.Employee.Firstname ="tim"
x.EMployee.Lastname = "tiny"
x.addemployee(x.employees)

Of course that would be 3 calls to the object...

It would be more effiecnt to have a funciton that takes all the values that you might store in the type as arguments, and then internally it adds them to the type if necessary..

i.e. Public Function AddEmployee(optional firstname as string, optional lastname as string)
Employee.firstname = firstname
employee.lastname = lastname

That would only incur 1 trip to set 2 properties and use the function... (rather than the 3 above)

HTH


Rob
 
Thanks for the responses. So, NoCoolHandle, if I were to use your suggestion of passing the values to the functions as arguments, and then place the values into the UDF would that more or less defeat the need for the UDF?? The reason that I want to be able to just pass the whole structure of the UDF to the Function is because my real application has multiple long functions that require up to 20 fields that need to be passed as arguments, so naturally just passing the UDF would decrease the amount of time I am coding
 
There are some problems with NoCoolHandle's code.

First, as stated in MSDN:
A user-defined type that appears within an object module can't be Public.

so this code:

Public Type Employee
FirstName as string
LastName as string
End type

would throw a compile error.

Second, this code:
Public Function AddEmployee(optional firstname as string, optional lastname as string)
Employee.firstname = firstname
employee.lastname = lastname

won't work either because because you can't access a UDT without first declaring a variable of that type. So in the Class code you would need a line like:

Dim uContact as udtContact

and then NoCoolHandle's code would be:

Public Function AddEmployee(optional firstname as string, optional lastname as string)
uContact.firstname = firstname
uContact.lastname = lastname


However, having said all of this, I would advocate paulbent's solution from Thread222-720091 and just make the elements of the UDT properties of the class and expose them with LET/GET statements.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Ok, I will take your (and paulbent's) advice and do it like that. Thanks again for your response
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top