An Object Which Contains Variant Data – Handling Arrays


Introduction

I struck this one working with WMI (Windows Management Instrumentation) and the C# classes in the System.Management Namespace.

The property value is just and object which can contain a singular value of an array of a values.

This post shows how I addressed the problem, and an interesting feature of C#.

The Object I was working with

clip_image004clip_image002clip_image006

What an Array Looks Like

clip_image008

The Code to Dump the Values in the Array

        private static void DumpArrayValues(PropertyData PropArray, int Indent)
        {
            Array x1 = (Array)PropArray.Value;
            String TabIndent = new string('\t', Indent);
            for (int i = 0; i < x1.Length; i++)
            {
                Debug.WriteLine(String.Format("{0} [{1}] {2}", TabIndent, i, x1.GetValue(i)));
            }
        }

Interesting Features

image

  • Value inherits (I have shown it as a multiple inheritance, I know C# does not support multiple inheritance but that is what it looks like to me) from both System.Array and System.String.
  • Casting the property array object as an Array object, allows you to get the length (number of elements in the array) of the array.
  • String.Format takes care of the different object types.
Advertisements

, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: