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

C#

 
Questionusing instance of a class ? Pin
Software_Specialist23-Apr-07 0:12
Software_Specialist23-Apr-07 0:12 
AnswerRe: using instance of a class ? Pin
Colin Angus Mackay23-Apr-07 0:23
Colin Angus Mackay23-Apr-07 0:23 
GeneralRe: using instance of a class ? Pin
Software_Specialist23-Apr-07 0:27
Software_Specialist23-Apr-07 0:27 
GeneralRe: using instance of a class ? Pin
Colin Angus Mackay23-Apr-07 0:47
Colin Angus Mackay23-Apr-07 0:47 
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 
Hi all, I have two questions pertaining to accessor methods in classes. I'm hoping someone can help.

Question 1: How do you write get and set methods for a member variable that is an array? I'll use a simple example to explain what I'm trying to achieve.
class MyClass
{
        public int MyInt1;
        public int MyInt2;
        public int MyInt3;
        public int[] MyIntArray;
        {
                get
                {
                        int[] tmparray = new int[3];
                        tmparray[0] = MyInt1;
                        tmparray[1] = MyInt2;
                        tmparray[2] = MyInt3;
                        return tmparray;
                }
                set
                {
                        MyInt1 = value[0];
                        MyInt2 = value[1];
                        MyInt3 = value[2];
                }
        }

        public MyClass()
        {
                MyIntArray = new int[3];
        }
}


In plain English, this is a class that has three member variables of type int and an additional member variable of type int[] which I'm hoping to map to the three member variables through accessor methods so that the three member variables of the class can be obtained and modified in an int array. This is how I'm hoping to do it:
MyClass MyInstance = new (MyClass);
int[] SomeArray = {10, 20, 30};
MyInstance.MyIntArray = SomeArray; // Note 1
MyInstance.MyIntArray[1] = 40; // Note 2

Note 1: At this stage everything work as expected and the three int member variables (MyInt1, MyInt2 and MyInt3) have the values 10, 20 and 30 respectively.

Note 2: The problem comes when I'm trying to set only certain elements in the array. I would expect that, at this point, the three int member variables would hold the values 10, 40 and 30 but the set method is never called when I allocate a value to an individual element in the array as done here.

Does anyone know how to write the get and set methods so that the array can be accessed both as an entire array or as individual elements?

Question 2: Can get and set metods be virtual (or something similar)? What I'm hoping to do is to put the MyIntArray member variable in a base class from which other classes inherit and then write the actual get and set methods in the inheriting classes rather than in the base class. Again, here is a simple exaple of what I'd like to do.
class MyBaseClass
{
        public virtual int[] MyIntArray;

        public MyBaseClass()
        {
                MyIntArray = new int[3];
        }
}

class MyClass : MyBaseClass
{
        public int MyInt1;
        public int MyInt2;
        public int MyInt3;
        public int[] MyIntArray;
        {
                get
                {
                        int[] tmparray = new int[3];
                        tmparray[0] = MyInt1;
                        tmparray[1] = MyInt2;
                        tmparray[2] = MyInt3;
                        return tmparray;
                }
                set
                {
                        MyInt1 = value[0];
                        MyInt2 = value[1];
                        MyInt3 = value[2];
                }
        }
}


This doesn't work. It doesn't even compile and I'm not entirely surprised. But is there a way of doing what I'm trying to do here?

Thanks in advance.
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 
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 

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.