Get/Set for array properties...

  • Thread starter Thread starter 2fast
  • Start date Start date
2

2fast

C# allows to have public array properties, but is there a way to make that
array property private, with a get/set method using the get/set syntax that
c# introduced? If not, what is the cleanest way to accomplish that?
Perhaps just create get_arrayname(index) and set_arrayname(index, value)
methods?

Thanks!

- 2fast
 
2fast:

I'm not sure I understand your question, but if you want the properties to
be private, where do you want to fire the Get/Set from? If youy want to do
it within the class, just change the access modifier to private.
 
You can have private properties. Just mark them as private. You cannot
have the "get" be private and the "set" be public.
 
Looks like everyone is confused as to what you are asking... maybe you are
talking about the indexers?? If you wanted an array where you don't allow
indexing, try something like...

public class MyArray : ArrayList
{
new protected object this[int index]
{
get { ... }
set { ... }
}
...
}
 
What I meant in my original post was the following. For example, with an
integer variable, I can make a property in the following way:
public class MyClass {
public int MyProperty {
get {...}
set {...}
}
}

If MyProperty is now an array, one valid syntax seems to be this:
public class My Class2 {
public int[] MyPropertyArray;
}

Is there any way to implement the above with the builtin get/set syntax? I
want the get to return one value for one position within the property array.
The compiler seems to think that the below should return an array of values.
Even if the below did compile, how would the get/set parts know the index
that is being referenced?
public class MyClass3 {
public int[] MyPropertyArray {
get {}
set {}
}
}

- 2fast
 
use an indexer:

class MyClass
{
int [] myArray;

public this[int i]
{
get { return myArray;}
set { myArray=value;}
}
}

then you can do:

MyClass c = new MyClass()

c[0]=1234;
 
Back
Top