Working with lists in C# for ZeroTouchNodes

Hello,

I’m trying to make a simple ZeroTouchNode and learn C# a bit but stuck on simple problem. Can anyone help with this?

using DSCore.Properties;
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using DSCore;
using Autodesk.DesignScript.Runtime;

namespace ADS
{
    public class R_nodes
    {
        public static IList Empty
        {
            get
            {
                return (IList)new ArrayList();
            }
        }

        private IList _columnData;
        private IList _columnHeader;
        private IList _searchHeaders;

        internal R_nodes(IList columnData, IList columnHeader, IList searchHeaders)
        {
            _columnData = columnData;
            _columnHeader = columnHeader;
            _searchHeaders = searchHeaders;
        }

        public static IList RetrieveFromColumnHeaderMatrix(IList columnData, IList columnHeader, IList searchHeaders)
        {
            int index = List.IndexOf(columnHeader, searchHeaders);
            var getIt = List.GetItemAtIndex(columnData, index);
            return getIt;
        }
    }
}

I’m getting error at getIt that’s stating:
image

In advance, much obliged

i believe the List.GetItemAtIndex is from DSCore right? means you will need a cast to convert it to IList as List.GetItemAtIndex returns an object

in your case,
IList getIt = List.GetItemAtIndex(columnData, index) as IList;
should do the trick

but with that being said, since you are already developing in C#, why not use LINQ or any other c# built-in modules to work with it

1 Like

Thanks! Error’s gone but the node doesn’t seems to work as intended. Or actually at all…

I’m quite fresh to C# and I run into an issue that not all the basic knowledge can be found on the internet in digestible form. For example, I knew there was a problem with type’s of something (VS thankfully show’s the problem) but how on earth could I ever knew that it’s possible to cast one thing as simple as with, well as . There aren’t any tutorials showing specifically that so it’s impossible to search for them. So somebody like me who’s trying to learn everything on his own, is dependent on somebody like you who thankfully there to answer questions like this.

Well, almost everthing regarding c# problems that you gonna encounter, or encountered can be found in stackoverflow, or should i say google will be your best friend in this. I myself doesnt has any programming background when i first step into dynamo and i manage to solve almost all the issue (even though it might not be the cleanest way to do it :rofl: ) using google. so i got to disagree with you on that point.
With that being said, maybe you can describe more about your use case and we can see what i can help you from there

Oh don’t get me wrong, the answers are out there that’s for sure. I claim that it’s not easy to search in regards to search terms. At least for me. :wink:

If you insist… I’m trying to feed 1D list with strings as headers, 2D list with variable types as data and a single item or 1D list as search term that corresponds with the a term(s) from headers. And retrieve a the requested data. And it’s not working :thinking:

just to clarify, i have no background of programming so 1D means one dimension list which means there wont be nested list right?

So what you want is basically retrieve items from key in a dictionary right? each headers will have their own list and by feeding the header, it will return the list of data. Am i right?

Your are correct sir.

okay that should be easy (havent tested it out yet but it should be working)

    public static IList RetrieveFromColumnHeaderMatrix(IList columnData, List<string> columnHeader, List<string> searchHeaders)
    {
        List<object> searchlist = new List<object>();
        Dictionary<string, object> dict = new Dictionary<string, object>();
        for (int i = 0; i < columnHeader.Count; i = i + 1)
        {
            if (i < columnData.Count)
                dict.Add(columnHeader[i], columnData[i]);
        }
        foreach (string searchHeader in searchHeaders)
        {
            searchlist.Add(dict[searchHeader]);
        }

        return searchlist;
    }
2 Likes

It’s working! If you truly don’t have programming background, you deferentially have a skill for it. Thanks a lot! :stuck_out_tongue_winking_eye:

No it’s not… I don’t get thing what you did :pleading_face:

Basically just add all tables to dictionary values with respective headers as key. Then use search index to search for the right key in the dictionary. thats all :slight_smile:

It’s a head scratcher for me. But much obliged for explanation!