6

I'm trying to figure out how to access values in an object through an API, but am having no luck. There is some documentation, but not a lot. I'm able to access some information, but what I'm looking for exists in a keyword field in a database that the software is using. I am able to print out the object type, but not the values in the actual object.

Here's my code:

 public class Test
    {
        public static ServiceConnection ConnectToDocuWare(Uri uri, string userName, string passWord)
        {
            return ServiceConnection.Create(uri, userName, passWord);
        }

        static void Main()
        {

            var userName;
            var passWord;
            var uri;
            var conn = ConnectToDocuWare(uri, userName, passWord);
            var org = conn.Organizations[0];
            var fileCabinets = org.GetFileCabinetsFromFilecabinetsRelation().FileCabinet;
            var fileCabinet = fileCabinets.SingleOrDefault(x => x.Name == "someValue");
            string documentID = "1";
            int DWID = int.Parse(documentID);
            string FCID = fileCabinet.Id;


            var dialogInfoItems = fileCabinet.GetDialogInfosFromDialogsRelation();
            var dialog = dialogInfoItems.Dialog.SingleOrDefault(x => x.DisplayName == "Some Value").GetDialogFromSelfRelation();
            var DocResults = RunQuery(dialog, documentID);


            foreach (Document doc in DocResults.Items)
            {
                var item = doc.GetDocumentFromSelfRelation();


                foreach (var itemInfo in item.Fields)
                {
                    if (itemInfo.ItemElementName.ToString() == "Keywords")
                    {
                        Console.WriteLine(itemInfo.Item);

                    }
                }
            }
        Console.ReadLine();
        }

        public static DocumentsQueryResult RunQuery(Dialog dialog, string DWDocID)
        {
            var q = new DialogExpression()
            {
                Operation = DialogExpressionOperation.And,
                Condition = new List<DialogExpressionCondition>()
                {
                    DialogExpressionCondition.Create("DWDOCID", DWDocID),
                },
                Count = 100,
                SortOrder = new List<SortedField>
                {
                    SortedField.Create("DWSTOREDATETIME", SortDirection.Desc)
                }
            };
            var queryResult = dialog.GetDocumentsResult(q);
            return queryResult;
        }
    }

This just outputs the object type. I've tried to use a foreach loop to output everything in the ItemInfo.Item object, but I get this error: Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

Essentially I'm trying to access the values in the keyword field to use later.

Tom D
  • 351
  • 1
  • 4
  • 13
  • At which line of code you are getting error and what is the class structure of class object you want to iterate.. it this is the case of object class then type cast it to specific list/array of objects. – Niranjan Singh Jan 03 '18 at 04:38
  • 1
    I'm getting the error here: Console.WriteLine(itemInfo.Item); – Tom D Jan 03 '18 at 04:47
  • 1
    Here's documentation about the class object: http://help.docuware.com/sdk/platform/html/T_DocuWare_Platform_ServerClient_DocumentIndexFieldKeywords.htm – Tom D Jan 03 '18 at 04:48
  • It is not possible to get error on Console.WriteLine(itemInfo.Item) because it uses the ToString() method to convert the object in string representation. you are getting error in any of foreach statement where you are trying to iterate non IEnumerable type property. – Niranjan Singh Jan 03 '18 at 05:01
  • 2
    Why did you delete [your question from earlier](https://stackoverflow.com/questions/48070315/foreach-statement-cannot-operate-on-variables-of-type-object-because-object) and post this, which certainly appears to be the exact same text? I took the time to look through your documentation for you to figure out what types are in play and comment how they relate to your error, and instead of getting a response it's now gone. Other commenters were trying to help you as well, but now we start over from scratch. Though your post did receive down/close votes, deletion discourages users from helping you – Lance U. Matthews Jan 03 '18 at 05:05
  • 1
    Sorry, I get the error when trying to enumerate through the object using this code: foreach (DocumentIndexFieldKeywords keywords in itemInfo.Item) { Console.WriteLine(keywords); } – Tom D Jan 03 '18 at 05:11
  • 1
    I apologize, for deleting, I thought people had given up on helping me so I re-posted. – Tom D Jan 03 '18 at 05:12

1 Answers1

6

The error message is very clear - you can only use a foreach loop on objects that have a public definition of the GetEnumerator method.

From the foreach, in (C# Reference) page in Microsoft docs:

The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.

This means that you can't use a foreach loop on any instance of any class (or struct) you want, because whatever is being used for the in part of the foreach loop must provide an enumerator.

Interesting fact: As Enigmativity pointed out in this answer, All you really need for a foreach loop to work is having a public method called GetEnumerator(), and have that public method returns an instance with a couple of methods and a property, namely void Reset(), bool MoveNext(), and T Current { get; private set; }, Where T is some type.

This means that you are not required to declare your class as implementing the IEnumerable or the IEnumerable<T> interface, And your Enumerator is not required to be declared as implementing the IEnumerator or IEnumerator<T> interface - it's sufficient that you provide the methods and properties defined it the relevant interfaces - That's all the c# compiler demands to use a foreach loop.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121