Question : Cold Fusion and DOT NET Class Library Structures (written in VB.NET Visual Studio 2008)

I have build a DOT NEt Class Library that returns arrays, collection, list, etc.  of a structure that I defined within.  If I include the DLL in a DOT NET application,  I can make arrays and collection and list variables and for example use
for each mystruct as MyStructure in ReturnedArray
  etc.

I can return a datatable from the component to a cfc component and return it to a cfc page.  But I do not seem to be able to use an array of my structures to pass some information back to the cfc page.

I have included an example class library with similar behavior and a client app that uses it.

Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
'The Class


Option Explicit On

Public Class SharedRecordDefinitions

    Public Structure RecordLayout
        Dim FName As String
        Dim LName As String

        Sub New(ByVal FN As String, ByVal LN As String)
            Me.FName = FN
            Me.LName = LN
        End Sub

    End Structure

    Private m_RecordA() As RecordLayout = { _
                                          New RecordLayout("H", "B"), _
                                          New RecordLayout("R", "B"), _
                                          New RecordLayout("B", "B") _
                                          }

    Private m_RecordB() As RecordLayout = { _
                                          New RecordLayout("H", "B"), _
                                          New RecordLayout("R", "B"), _
                                          New RecordLayout("B", "B") _
                                          }

    Private m_RecordC() As RecordLayout = { _
                                          New RecordLayout("H", "B"), _
                                          New RecordLayout("R", "B"), _
                                          New RecordLayout("B", "B") _
                                          }

    Public ReadOnly Property RecordA() As RecordLayout()
        Get
            Return m_RecordA
        End Get
    End Property

    Public ReadOnly Property RecordB() As RecordLayout()
        Get
            Return m_RecordB
        End Get
    End Property

    Public ReadOnly Property RecordC() As RecordLayout()
        Get
            Return m_RecordC
        End Get
    End Property

End Class


'A Simple Client in VB.NET

Imports SharedRecordDefinitions.SharedRecordDefinitions

Module frmMain
    Private m_recAllFormats As SharedRecordDefinitions.SharedRecordDefinitions = New SharedRecordDefinitions.SharedRecordDefinitions

    Sub Main()
        Dim reca() As SharedRecordDefinitions.SharedRecordDefinitions.RecordLayout = m_recAllFormats.RecordA
        Dim recb() As SharedRecordDefinitions.SharedRecordDefinitions.RecordLayout = m_recAllFormats.RecordB
        Dim recc() As SharedRecordDefinitions.SharedRecordDefinitions.RecordLayout = m_recAllFormats.RecordC

        For Each o As RecordLayout In m_recAllFormats.RecordA
            Debug.Print(o.FName & " : " & o.LName)
        Next

    End Sub

End Module

Answer : Cold Fusion and DOT NET Class Library Structures (written in VB.NET Visual Studio 2008)

Working with .NET objects from CF is a little different than using them from w/in .NET.  Properties are a good example.  To access pubic "properties" you'll need to use the syntax:

   Get_propertyName()
   Set_propertyName(value)
   (See: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=dotNet_04.html)

So to access the "RecordA" property use the following, which will return an array.

   <!--- assuming the DLL is in the current directory --->
   <cfset path = ExpandPath("SharedRecordDefinitions.dll")>
   <cfset obj = createObject("dotnet", "SharedRecordDefinitions.SharedRecordDefinitions", path)>
   <!--- recordA will now contain an array of objects --->
   <cfset recordA = obj.get_recordA()>

VB.NET arrays (like "recordA") should be converted transparently. So you'll be able to iterate through its elements using the "array" attribute of cfloop.  Note, this example doesn't do anything except dump the public methods of each element ...

   <cfloop array="#obj.get_recordA()#" index="elem">
      <cfdump var="#elem#">
  </cfloop>

Keep in mind VB.NET "structures" aren't the same as CF structures.  So they'll be converted into objects, meaning you'll need to use the same technique, ie Get_propertyName(), to access the contents of your structures:

    <cfloop array="#obj.get_recordA()#" index="elem">
        <!--- use the Get_ methods to display the values ...--->
      <cfoutput>
      FName = #elem.get_FName()#
      LName = #elem.get_LName()#<br>
      </cfoutput>
    </cfloop>

Since the "structures" are not interchangable w/CF structures, keep in mind you won't be able to use them everywhere you can use a CF structure. For example, you *can't* use the "collection" attribute of cfloop to iterate through the structure keys -OR- use associative array notation to access the values as you *can* with regular CF structures:

<!--- {} is a shortcut for structNew() --->
<cfset myStruct = {} >
<cfset myStruct.FName = "Bob">
<cfset myStruct.LName = "Smith">
<cfloop collection="#myStruct#" item="key">
    <cfoutput>
      key = #key# value=#myStruct[key]#<br>
    </cfoutput>
</cfloop>
Random Solutions  
 
programming4us programming4us