views:

664

answers:

7

Hello everyone. 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

+2  A: 

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]; }
    }
}
Marc Gravell
Sorry, I meant something like public string[] Name { get { return _name[index]; }}
kobra
+4  A: 

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?

Fredy Treboux
I don't want to return a copy of the whole string array, just an element of the array. I think Indexers will be helpful. Thanks
kobra
+3  A: 

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!

Vitaliy
+1  A: 

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;
}
tvanfosson
I could see having a string array returned from something like a pre-built CSV parser, and wanting to build a simple object to wrap the contents of that array. But it's probably better to just copy the strings to backing stores, since they're just references.
Joel Coehoorn
Sorry, if I confused you. What I really want to get a specific element using the index from a string array of another class without making the string array public. So I wanted to use public Property. Thanks
kobra
A: 

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]
Tordek
+1  A: 

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.

Rik
+2  A: 

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);
    }
}
John K