get/set array properties (followup)

2

2fast

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
 
N

Nicholas Paldino [.NET/C# MVP]

2fast,

What you are doing will require the creation of another class.
Basically, you will have to create a class with an indexer (of which there
is only one allowed). In that class you would store the array, and create
the get/set on the indexer. Then in the main class, you would expose a
property that exposes an instance of that class.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

2fast said:
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


2fast said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top