4

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

8 Answers 8

6

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

5

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?

1 Comment

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
4

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]; }
    }
}

1 Comment

Sorry, I meant something like public string[] Name { get { return _name[index]; }}
3

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!

Comments

1

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.

Comments

1

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;
}

2 Comments

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.
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
0

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]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.