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.
LVL 1
G ScottAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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

0
G ScottAuthor Commented:
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??
0
Bob LearnedCommented:
Here is an example coded in the comment block, where you can put all the TextBox controls into a GroupBox:

var answers = new AnswersClass();

var count = 0;

foreach (var control in answersGroup.Controls)
{
  if (control is TextBox)
  {
       answers.AnswerList[count++] = (control as TextBox).Text;
  } 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
G ScottAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.