-1

I have converted some code from VB6 to VB.NET, using the default CTRL-ALT-F10 where necessary.

I now get an error on the line:

For Each objChild In objItem.value(iSub)

saying that

Expression is of type 'Object', which is not a collection type.

Does any one have the correct conversion please?

Original VB code

Private Function ValidateAnItem(ByRef objItem As TDS.ItemField) As String
For iSub = 1 To objItem.Value.Count
  If objItem.FieldType = FieldTypeEmbed Then
      For Each objChild In objItem.Value(iSub)
          strReturn = ValidateAnItem(objChild, strAdministrator)
          If strReturn <> "" Then
             ValidateAnItem = strReturn
             Set objChild = Nothing
             Exit For
            Exit Function
         End If
     Next
 End If

Next

code converted to vb.net

Public Function ValidateAnItem(ByRef objItem As TDS.ItemField) As String
    Dim objChild As TDS.ItemField
    For iSub = 1 To objItem.value.Count
         If objItem.FieldType = TDSDefines.EnumTridionWebSchemaFieldType.FieldTypeEmbed Then
               For Each objChild In objItem.value(iSub)
                     strReturn = ValidateAnItem(objChild)
                     If strReturn <> "" Then
                         ValidateAnItem = strReturn
                         objChild = Nothing
                         Exit For
                         Exit Function
                      End If

[...]
1
  • 2
    It might help the community to answer if you show what you've tried doing to fix this and explain the context of what this code is doing. "Please fix my code" type questions don't generally get good responses. What is this code for? What does the Schema/Page/Component Template consist of? Also please specify the version of Tridion you are using. Commented Nov 22, 2013 at 17:02

1 Answer 1

1

The line "For Each objChild In objItem.value(iSub)" refers the value of a field.

I guess VB.Net is thrown off by the fact that the 'Value' can refer to non collection types (like strings) as well as collection types (like ItemFields).

The solution depends on what you're trying to iterate over. If you want to iterate over all the values of a field called objItem, do this:

For Each objChild In objItem.value*

If you want to iterate over the fields embedded inside an embeddable field, do this:

Dim objEmbeddedFields As TDS.ItemFields
Set objEmbeddedFields = objItem.value(iSub)
For Each objChild As TDS.ItemField In objEmbeddedFields*

*Untested code disclaimer.

5
  • Thank you everyone, i have removed the brakets on the line For Each objChild In objItem.value, it at least compiles now, i am trying to validate a URL on a component link field within the event system, onComponentSavePre, the version of Tridion is r2009, and the vb.net / visual studio is 2008 sp1
    – user610
    Commented Nov 25, 2013 at 15:44
  • If this answer solved the problem, could you mark it as correct please?
    – Quirijn
    Commented Nov 28, 2013 at 9:55
  • This answer has solved my problem
    – user610
    Commented Dec 3, 2013 at 14:40
  • 1
    The idea is that you mark the answer as 'correct' by clicking on the 'checkmark' icon to the left of the answer.
    – Quirijn
    Commented Dec 3, 2013 at 21:15
  • +1 to clicking the check box in StackExchange to accept this answer. It's good for future askers, good for the community, and also good for Quirijn. :-) Commented Aug 6, 2014 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.