Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
AnswerRe: using instance of a class ? Pin
Eduard Keilholz23-Apr-07 0:47
Eduard Keilholz23-Apr-07 0:47 
GeneralRe: using instance of a class ? Pin
Christian Graus23-Apr-07 0:58
protectorChristian Graus23-Apr-07 0:58 
GeneralRe: using instance of a class ? Pin
Software_Specialist23-Apr-07 1:41
Software_Specialist23-Apr-07 1:41 
QuestionGetter and Setter functions Pin
Dewald23-Apr-07 0:06
Dewald23-Apr-07 0:06 
AnswerRe: Getter and Setter functions Pin
andre_swnpl23-Apr-07 0:29
andre_swnpl23-Apr-07 0:29 
GeneralRe: Getter and Setter functions Pin
Colin Angus Mackay23-Apr-07 0:40
Colin Angus Mackay23-Apr-07 0:40 
AnswerRe: Getter and Setter functions Pin
Colin Angus Mackay23-Apr-07 0:37
Colin Angus Mackay23-Apr-07 0:37 
AnswerRe: Getter and Setter functions Pin
Guffa23-Apr-07 1:00
Guffa23-Apr-07 1:00 
The problem with accessing the items as an array, is that you are working with an array that is a copy of the values. Changing the copy will not change the original values that the copy was created from.

You can either keep the data as an array and make properties to access the items separately, or keep the data separately and make an indexer to access them as an array.

public class ArrayStorageClass {
   private int[] _data;
   public ArrayStorageClass() {
      _data = new int[3];
   }
   public int[] MyIntArray {
      get { return _data; }
      set {
         if (value.Length != 3) throw new ApplicationException("An array with the size of three has to be used.");
         _data = value;
      }
   }
   public int MyInt1 { get { return _data[0]; } set { _data[0] = value; } }
   public int MyInt2 { get { return _data[1]; } set { _data[1] = value; } }
   public int MyInt3 { get { return _data[2]; } set { _data[2] = value; } }
}

ArrayStorageClass storage = new ArrayStorageClass();
storage.MyIntArray[0] = 42;
storage.MyInt2 = 1;

public class ArrayEmulatorClass {
   private int _data1, _data2, _data3;
   public ArrayEmulatorClass() {}
   public int this[int index] {
      get {
         switch (index) {
            case 0: return _data1;
            case 1: return _data2;
            case 2: return _data3;
            default: throw new IndexOutOfRangeException();
         }
      }
      set {
         switch (index) {
            case 0: _data1 = value;
            case 1: _data2 = value;
            case 2: _data3 = value;
            default: throw new IndexOutOfRangeException();
         }
      }
   }
   public int MyInt1 { get { return _data1; } set { _data1 = value; } }
   public int MyInt2 { get { return _data2; } set { _data2 = value; } }
   public int MyInt3 { get { return _data3; } set { _data3 = value; } }
}

ArrayEmulatorClass emulator = new ArrayEmulatorClass();
emulator[0] = 42;
emulator.MyInt2 = 1;


If you want to declare the property in a base class without supplying the implementation, you can make it abstract. You also have to make the base class itself abstract, which means that you can't create instances of it. If you want to declare data in the base class to be used in the derived classes, make it protected.

public abstract class PropertyBaseClass {
   protected int _data;
   public abstract int MyProperty { get; set; }
}

public class SpecificPropertyClass : PropertyBaseClass {
   public override int MyProperty { get { return _data; } set { _data = value; } }
}



---
single minded; short sighted; long gone;

GeneralRe: Getter and Setter functions Pin
Dewald23-Apr-07 2:44
Dewald23-Apr-07 2:44 
AnswerRe: Getter and Setter functions Pin
Vikram A Punathambekar23-Apr-07 2:07
Vikram A Punathambekar23-Apr-07 2:07 
QuestionHow to set a parameter field in .rdlc during design time Pin
selvakumar Panchatcharam22-Apr-07 23:57
selvakumar Panchatcharam22-Apr-07 23:57 
QuestionC# time to C++ (VS 6) time Pin
Stevo Z22-Apr-07 23:49
Stevo Z22-Apr-07 23:49 
AnswerRe: C# time to C++ (VS 6) time Pin
Christian Graus23-Apr-07 0:06
protectorChristian Graus23-Apr-07 0:06 
GeneralRe: C# time to C++ (VS 6) time Pin
Stevo Z23-Apr-07 0:15
Stevo Z23-Apr-07 0:15 
QuestionSerialization problem Pin
NaNg1524122-Apr-07 23:48
NaNg1524122-Apr-07 23:48 
QuestionHow to add sub columns with C#.net for Pocket PC Pin
urairat22-Apr-07 23:44
urairat22-Apr-07 23:44 
QuestionMissing something in an unmanaged C++ function call? Pin
jozsurf22-Apr-07 23:26
jozsurf22-Apr-07 23:26 
AnswerRe: Missing something in an unmanaged C++ function call? Pin
Luc Pattyn23-Apr-07 9:00
sitebuilderLuc Pattyn23-Apr-07 9:00 
GeneralRe: Missing something in an unmanaged C++ function call? Pin
jozsurf23-Apr-07 14:58
jozsurf23-Apr-07 14:58 
GeneralRe: Missing something in an unmanaged C++ function call? Pin
Luc Pattyn23-Apr-07 15:26
sitebuilderLuc Pattyn23-Apr-07 15:26 
GeneralRe: Missing something in an unmanaged C++ function call? Pin
jozsurf23-Apr-07 15:31
jozsurf23-Apr-07 15:31 
QuestionWrite to the Visual Studio Output window Pin
anderslundsgard22-Apr-07 22:48
anderslundsgard22-Apr-07 22:48 
AnswerRe: Write to the Visual Studio Output window Pin
Christian Graus22-Apr-07 22:54
protectorChristian Graus22-Apr-07 22:54 
GeneralRe: Write to the Visual Studio Output window Pin
anderslundsgard22-Apr-07 22:59
anderslundsgard22-Apr-07 22:59 
QuestionHow to replace colors in a bitmap Pin
dudedotnet22-Apr-07 21:14
dudedotnet22-Apr-07 21:14 

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.