Link to home
Start Free TrialLog in
Avatar of G Scott
G ScottFlag for United States of America

asked on

WPF Dynamic Class Values

So I have some dynamically created TextBox controls in a window.  After the has filled in all the boxes they click a button.  I want to pass those values to a class that I wrote to store the answers until they are all done.  I get the values by doing this:

MessageBox.Show((LogicalTreeHelper.FindLogicalNode(gridQuestion, "commentSep" + i) as TextBox).Text);

Open in new window


Here is my class:
class answersClass

        //Separate Answers Section ************************************
        public static string answerSep1
        {
            get { return ansSep1; }
            set { ansSep1 = value; }
        }
        private static string ansSep1;

        public static string answerSep2
        {
            get { return ansSep2; }
            set { ansSep2 = value; }
        }
        private static string ansSep2;

        public static string answerSep3
        {
            get { return ansSep3; }
            set { ansSep3 = value; }
        }
        private static string ansSep3;

        public static string answerSep4
        {
            get { return ansSep4; }
            set { ansSep4 = value; }
        }
        private static string ansSep4;

        public static string answerSep5
        {
            get { return ansSep5; }
            set { ansSep5 = value; }
        }
        private static string ansSep5;
    }
}

Open in new window


How can I access the classes dynamically using a For Loop?  

Basically assigning value from "((LogicalTreeHelper.FindLogicalNode(gridQuestion, "commentSep" + i) as TextBox).Text);" to answersClass.answerSep + i

Thanks for any help on this.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Is there a reason for separate properties, versus a single property as a list?

public List<string> AnswerList { get; private set; }

Open in new window

Avatar of G Scott

ASKER

Hey TheLearnedOne,

I only have one valid reason and that is because I don't know what I am doing. Lol.  I am trying to advance from VB to C#, but apparently I was doing it wrong in VB, too. Haha.  So, once I have it as a list how do I loop through those values to insert them into SQL (which is what I end up doing)?  Is that List<string> indexed?  AnswerList(0), AnswerList(1) etc??
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G Scott

ASKER

I can make this work.  Thanks so much for the <List> tip.  That is a great way to do it, and I think I can apply that to a bunch of other projects I have done.