Click here to Skip to main content
15,912,756 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I check to see if a file is being accessed by another process? Pin
Vasudevan Deepak Kumar29-Mar-07 9:03
Vasudevan Deepak Kumar29-Mar-07 9:03 
GeneralRe: How can I check to see if a file is being accessed by another process? Pin
Not Active29-Mar-07 9:25
mentorNot Active29-Mar-07 9:25 
GeneralRe: How can I check to see if a file is being accessed by another process? Pin
Vasudevan Deepak Kumar29-Mar-07 12:13
Vasudevan Deepak Kumar29-Mar-07 12:13 
QuestionHELP NEEDED - DCOM Server in C#? Pin
r6yamaha29-Mar-07 8:16
r6yamaha29-Mar-07 8:16 
QuestionProblem using ListView control? Pin
Khoramdin29-Mar-07 8:10
Khoramdin29-Mar-07 8:10 
AnswerRe: Problem using ListView control? Pin
kubben29-Mar-07 8:19
kubben29-Mar-07 8:19 
QuestionHow to install the IE plugin from DLL Pin
Nadia Monalisa29-Mar-07 7:59
Nadia Monalisa29-Mar-07 7:59 
QuestionCan I create an array or List<> of class properties? [modified] Pin
Member 9629-Mar-07 7:38
Member 9629-Mar-07 7:38 
I want to add indexed access to a series of identical pre existing properties in a business object class. I can't change the existing properties in any way (they contain business rules and other code and are already in use in other areas) so I want to add some sort of method to access them index style.

For example, here are some hypothetical properties that pre-exist:

public string Custom1 {get{somecodehere;}set{somemorecodehere};}<br />
public string Custom2 {get{somecodehere;}set{somemorecodehere};}<br />
public string Custom3 {get{somecodehere;}set{somemorecodehere};}<br />
etc etc etc


Can I create a generic List<> of those existing properties or an array of properties (or any collection) of those existing properties so I can set and get those properties using their existing code but access them by index?

(without using reflection?)


-- modified at 14:53 Thursday 29th March, 2007

Example (This is part of a much larger and complex class with dozens of other properties, database code etc, this is only an excerpt of the relevant bits for this issue):
<br />
 class Class1<br />
    {<br />
<br />
        //Custom fields<br />
        private string mCustom0 = null;<br />
        private string mCustom1 = null;<br />
        private string mCustom2 = null;<br />
        //etc etc<br />
<br />
        public string Custom0<br />
        {<br />
            get { return mCustom0; }<br />
            set<br />
            {<br />
                if (bReadOnly) ThrowSetError();<br />
                else<br />
                {<br />
                    if (mCustom0 != value)<br />
                    {<br />
                        mCustom0 = value;<br />
                        BrokenRules.Assert("Custom0", Messages.e255, "Custom0", value.Length > 255);<br />
                        MarkDirty();<br />
                    }<br />
                }<br />
            }<br />
        }<br />
<br />
        public string Custom1<br />
        {<br />
            get { return mCustom1; }<br />
            set<br />
            {<br />
                if (bReadOnly) ThrowSetError();<br />
                else<br />
                {<br />
                    if (mCustom1 != value)<br />
                    {<br />
                        mCustom1 = value;<br />
                        BrokenRules.Assert("Custom1", Messages.e255, "Custom1", value.Length > 255);<br />
                        MarkDirty();<br />
                    }<br />
                }<br />
            }<br />
        }<br />
<br />
        public string Custom2<br />
        {<br />
            get { return mCustom2; }<br />
            set<br />
            {<br />
                if (bReadOnly) ThrowSetError();<br />
                else<br />
                {<br />
                    if (mCustom2 != value)<br />
                    {<br />
                        mCustom2 = value;<br />
                        BrokenRules.Assert("Custom2", Messages.e255, "Custom2", value.Length > 255);<br />
                        MarkDirty();<br />
                    }<br />
                }<br />
            }<br />
        }<br />
<br />
        //etc etc<br />
<br />
<br />
        //I'd like to access the above CustomX properties using an indexer.  <br />
        //I can make no changes to the existing properties, but I'd like to be able to <br />
        //get and set them in a consuming class like this pseudo code shows<br />
        //CustomProperties[x]="somevalue"<br />
        //string s=CustomProperties[x];<br />
        //where x is an integer value corresponding to the property, i.e. if x was zero it would<br />
        //use property Custom0<br />
<br />
        //I can go with a switch statement like this:<br />
        public void SetCustomField(int index, string svalue)<br />
        {<br />
            switch (index)<br />
            {<br />
                case 0:<br />
                    Custom0 = svalue;<br />
                    break;<br />
                case 1:<br />
                    Custom1 = svalue;<br />
                    break;<br />
                case 2:<br />
                    Custom2 = svalue;<br />
                    break;<br />
                    //etc<br />
            }<br />
        }<br />
<br />
        //However that seems inefficient and clunky<br />
        //a clean collection I can access as a property and index to them would be better<br />
        //Trouble is I can't simply do something like this pseudo code:<br />
        //List<Property> CustomProperties = new List<Property>(20)<br />
        //CustomProperties[0]=Custom0;<br />
        //CustomProperties[1]=Custom1;<br />
        //etc etc<br />
<br />
        //Perhaps what I'm after is storing a list of pointers to the properties in the List or<br />
        //Array or whatever collection <br />
<br />
<br />
    }



-- modified at 14:55 Thursday 29th March, 2007
AnswerRe: Can I create an array or List of class properties? Pin
Not Active29-Mar-07 7:58
mentorNot Active29-Mar-07 7:58 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 8:09
Member 9629-Mar-07 8:09 
GeneralRe: Can I create an array or List of class properties? Pin
Not Active29-Mar-07 8:28
mentorNot Active29-Mar-07 8:28 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 8:58
Member 9629-Mar-07 8:58 
GeneralRe: Can I create an array or List of class properties? Pin
Not Active29-Mar-07 9:22
mentorNot Active29-Mar-07 9:22 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 10:02
Member 9629-Mar-07 10:02 
AnswerRe: Can I create an array or List of class properties? Pin
led mike29-Mar-07 8:13
led mike29-Mar-07 8:13 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 8:19
Member 9629-Mar-07 8:19 
GeneralRe: Can I create an array or List of class properties? Pin
led mike29-Mar-07 8:30
led mike29-Mar-07 8:30 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 8:59
Member 9629-Mar-07 8:59 
AnswerRe: Can I create an array or List of class properties? Pin
Leslie Sanford29-Mar-07 10:41
Leslie Sanford29-Mar-07 10:41 
GeneralRe: Can I create an array or List of class properties? Pin
Member 9629-Mar-07 10:51
Member 9629-Mar-07 10:51 
QuestionQuick Question Pin
Ennis Ray Lynch, Jr.29-Mar-07 7:14
Ennis Ray Lynch, Jr.29-Mar-07 7:14 
AnswerRe: Quick Question Pin
kubben29-Mar-07 7:26
kubben29-Mar-07 7:26 
GeneralRe: Quick Question Pin
Ennis Ray Lynch, Jr.29-Mar-07 8:10
Ennis Ray Lynch, Jr.29-Mar-07 8:10 
GeneralRe: Quick Question Pin
kubben5-Apr-07 9:35
kubben5-Apr-07 9:35 
GeneralRe: Quick Question Pin
Vasudevan Deepak Kumar29-Mar-07 9:05
Vasudevan Deepak Kumar29-Mar-07 9:05 

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.