Click here to Skip to main content
15,889,877 members
Home / Discussions / C#
   

C#

 
GeneralRe: Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. - why? Pin
turbosupramk34-Jan-15 6:34
turbosupramk34-Jan-15 6:34 
Questionarray property Pin
TMattC1-Jan-15 5:13
TMattC1-Jan-15 5:13 
AnswerRe: array property Pin
Thomas Daniels1-Jan-15 5:29
mentorThomas Daniels1-Jan-15 5:29 
GeneralRe: array property Pin
PIEBALDconsult1-Jan-15 5:47
mvePIEBALDconsult1-Jan-15 5:47 
AnswerRe: array property Pin
OriginalGriff1-Jan-15 6:07
mveOriginalGriff1-Jan-15 6:07 
GeneralRe: array property Pin
harold aptroot1-Jan-15 6:16
harold aptroot1-Jan-15 6:16 
AnswerRe: array property Pin
BillWoodruff1-Jan-15 9:23
professionalBillWoodruff1-Jan-15 9:23 
AnswerRe: array property Pin
BillWoodruff2-Jan-15 2:48
professionalBillWoodruff2-Jan-15 2:48 
TMattC wrote:
Can it be done with an automatic property
One further comment: an automatic property is one where the private backing-field is created for you by using the declarative syntax:

private char[] CharProp { set; get; }

The moment you specify a "body" for either the 'set or the 'get, it's no longer an automatic property, and, since it's clear your use-case would involved accessing characters within the Array here, you'd have to do that in the 'get, and, for that to be done, you'd have to have an value to use as an Index into the Array, and there's no way to "pass in" a value to be used internally in a 'get, or 'set.

The next version of C#, C#6, is going to add some features to Property declaration: [^].

Of course, with a Property whose Type is Array you can always use the 'SetValue and 'GetValue methods of the Array object; however, as you'll see in this example, 'GetValue returns a Type Object which will need to be cast to 'Char to be used in the way you probably want to use it.
C#
private char[] CharProp { set; get; }

private void TestCharProp()
{
    CharProp = new char[3] {'a', 'b', 'c'};

    char value2 = (char) CharProp.GetValue(2);
    
    CharProp.SetValue('x',2);
}
That's not the type of code I'd write: no error check for null; no check for bounds; unnecessary cast required. But, you can write your own methods to 'set and 'get, of course that do validate and do avoid the cast.
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push»  Wittgenstein

Question(solved) Multiple reads of a file when it is open in another application ? Pin
BillWoodruff1-Jan-15 4:15
professionalBillWoodruff1-Jan-15 4:15 
AnswerRe: Multiple reads of a file when it is open in another application ? Pin
OriginalGriff1-Jan-15 4:42
mveOriginalGriff1-Jan-15 4:42 
GeneralRe: Multiple reads of a file when it is open in another application ? Pin
BillWoodruff1-Jan-15 6:03
professionalBillWoodruff1-Jan-15 6:03 
GeneralRe: Multiple reads of a file when it is open in another application ? Pin
OriginalGriff1-Jan-15 6:12
mveOriginalGriff1-Jan-15 6:12 
GeneralRe: Multiple reads of a file when it is open in another application ? Pin
BillWoodruff1-Jan-15 21:55
professionalBillWoodruff1-Jan-15 21:55 
GeneralRe: Multiple reads of a file when it is open in another application ? Pin
OriginalGriff1-Jan-15 23:05
mveOriginalGriff1-Jan-15 23:05 
AnswerRe: Multiple reads of a file when it is open in another application ? Pin
Pete O'Hanlon1-Jan-15 23:04
mvePete O'Hanlon1-Jan-15 23:04 
GeneralRe: Multiple reads of a file when it is open in another application ? Pin
BillWoodruff2-Jan-15 2:22
professionalBillWoodruff2-Jan-15 2:22 
QuestionReference to array element Pin
Krishnakumartg31-Dec-14 22:55
Krishnakumartg31-Dec-14 22:55 
AnswerRe: Reference to array element Pin
OriginalGriff31-Dec-14 23:56
mveOriginalGriff31-Dec-14 23:56 
GeneralRe: Reference to array element Pin
BillWoodruff1-Jan-15 4:24
professionalBillWoodruff1-Jan-15 4:24 
GeneralRe: Reference to array element Pin
OriginalGriff1-Jan-15 4:34
mveOriginalGriff1-Jan-15 4:34 
GeneralRe: Reference to array element Pin
harold aptroot1-Jan-15 4:44
harold aptroot1-Jan-15 4:44 
GeneralRe: Reference to array element Pin
Krishnakumartg26-Jan-15 18:52
Krishnakumartg26-Jan-15 18:52 
Question[Solved]C# Database Image Manager Pin
rattlerrFx31-Dec-14 10:44
rattlerrFx31-Dec-14 10:44 
AnswerRe: C# Database Image Manager Pin
Richard Andrew x6431-Dec-14 11:13
professionalRichard Andrew x6431-Dec-14 11:13 
GeneralRe: C# Database Image Manager Pin
rattlerrFx31-Dec-14 11:25
rattlerrFx31-Dec-14 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.