Main Site Documentation

system.array.SetValue and GetValue extension


#1

I’m porting code which uses alot of SetValue and GetValue methods
I’ve figure out how to do extensions for strings but still learning how to do them for arrays.
At bottom of this post is an extension for SetAllValues I found on the web.
I’m trying to rewrite for SetValue and GetValue.

So will this work? I’m concerned that the second parameter (value) needs to be declared as byte but I’m not sure. Any help is greatly appreciated as I’m still learning.
Thanks


public static T[] SetValue<T>(this T[] array, T value, int index)
        {
                array[index] = value;

            return array;
        }

Example for SetAllValues


/// <summary>
        /// Sets all values.
        /// </summary>
        /// <typeparam name="T">The type of the elements of the array that will be modified.</typeparam>
        /// <param name="array">The one-dimensional, zero-based array</param>
        /// <param name="value">The value.</param>
        /// <returns>A reference to the changed array.</returns>
        public static T[] SetAllValues<T>(this T[] array, T value)
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = value;
            }

            return array;
        }


#2

I don’t think this will work. MF does not support generics.


#3

MF doesn’t support extensions either.


#4

Extensions work fine


#5

Yes, extensions work.
I’ve written a few to extend the string manipulation and tested them on hardware
They compile and work.

If one searches the forum, there are several posts on extensions and even a library of common extensions exists at http://netmfcommonext.codeplex.com/ - created by some other forum members.

One needs to have this in the project for extensions to work but I can say with certainty that extensions work. What I really need is for somebody to look at how I’ve defined my SetValue extension and comment.
Thanks


namespace System.Runtime.CompilerServices 
{ 
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] 
    public sealed class ExtensionAttribute : Attribute { } 
}



#6

Hmmm… I tried creating extensions last week and it wasn’t. I interpreted it as another limitation. I’ll have to look at that again. Thanks for the info.


#7

Yes, extensions definitely work.
Here is another example from this forum -
http://www.tinyclr.com/forum/6/1258/ and this example includes the code that is required to get the compiler to properly work with extensions.

Was hoping somebody would answer my question, but perhaps its not an easy one.
I’ll be working later tonight and once I get into debugging, I’ll come back and update the code with a fix or final extensions for SetValue and GetValue. If anybody knows the answer, please feel free to chime in now. Thanks


#8

I finally got back around to this. I initially got the error when trying to use the “this” keyword and assumed it was another limitation. Since it seems fairly difficult to find info about this on the net, I gave extension methods in NETMF a full write-up on my blog to hopefully help others that might be looking.

http://blog.ianlee.info/2011/10/netmf-extension-methods.html