开发者

Using Property to get array element in C#

Is there a way to get a specific element (based in index) from a string array using Property. I prefer using public property in place of making the string array public. I am working on C#开发者_如何转开发.NET 2.0

Regards


Are you possibly trying to protect the original array; do you mean you want a protective wrapper around the array, through "a Property" (not of its own)? I'm taking this shot at guessing the details of your question. Here's a wrapper implementation for a string array. The array cannot be directly access, but only through the wrapper's indexer.

using System;

public class ArrayWrapper {

    private string[] _arr;

    public ArrayWrapper(string[] arr) { //ctor
        _arr = arr;
    }

    public string this[int i] { //indexer - read only
        get {
            return _arr[i];
        }
    }
}

// SAMPLE of using the wrapper
static class Sample_Caller_Code {
    static void Main() {
        ArrayWrapper wrapper = new ArrayWrapper(new[] { "this", "is", "a", "test" });
        string strValue = wrapper[2]; // "a"
        Console.Write(strValue);
    }
}


If I understand correctly what you are asking, You can use an indexer. Indexers (C# Programming Guide)

Edit: Now that I've read the others, maybe you can expose a property that returns a copy of the array?


If the property exposes the array:

string s = obj.ArrayProp[index];

If you mean "can I have an indexed property", then no - but you can have a property that is a type with an indexer:

static class Program
{
    static void Main()
    {
        string s = ViaArray.SomeProp[1];
        string t = ViaIndexer.SomeProp[1];
    }
}
static class ViaArray
{
    private static readonly string[] arr = { "abc", "def" };
    public static string[] SomeProp { get { return arr; } }
}
static class ViaIndexer
{
    private static readonly IndexedType obj = new IndexedType();
    public static IndexedType SomeProp { get { return obj; } }
}
class IndexedType
{
    private static readonly string[] arr = { "abc", "def" };
    public string this[int index]
    {
        get { return arr[index]; }
    }
}


What you need is a Property that can have input (an index).

There is only one property like that, called an Indexer.

Look it up on MSDN.

A shortcut: use a built in code snippet: go to your class and type 'indexer' then press tab twice. Viola!


Properties don't take parameters, so that won't be possible.

You can build a method, for instance

public string GetStringFromIndex(int i)
{
  return myStringArray[i];
}

Of course you'll probably want to do some checking in the method, but you get the idea.


I'm assuming that you have a class that has a private string array and you want to be able to get at an element of the array as a property of your class.

public class Foo
{
   private string[] bar;

   public string FooBar
   {
       get { return bar.Length > 4 ? bar[4] : null; }
   }
}

This seems horribly hacky, though, so I'm either not understanding what you want or there's probably a better way to do what you want, but we'd need to know more information.

Update: If you have the index of the element from somewhere else as you indicate in your comment, you could use an indexer or simply create a method that takes the index and returns the value. I'd reserve the indexer for a class that is itself a container and use the method route otherwise.

public string GetBar( int index )
{
     return bar.Length > index ? bar[index] : null;
}


Just return the array from the property; the resulting object will behave as an array, so you can index it.

E.G.:

string s = object.Names[15]


What you're asking can be done, like so:
You can initialize an object that holds your array, giving you exactly what you need:

public class ArrayIndexer<T> {
    private T[] myArrRef;
    public ArrayIndexer(ref T[] arrRef) {
        myArrRef = arrRef;
    }

    public T this [int index] {
        get { return myArrRef[index]; }
    }
}

Then, in your class:

public ArrayIndexer arr;
private SomeType[] _arr;    

//Constructor:
public MyClass(){
    arr = new ArrayIndexer<SomeType>(ref _arr);
}

Usage:

myClassObj.arr[2] // Gives the second item in the array.

Et Voila! An indexed property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜