none
creating controlls,valriables in one class

    Question

  • i have one class to create conttolls
    ============================================
    class CreateControls
        {

            public PairControls[] CreatePairControls(int k)
            {
                PairControls[] pairs = new PairControls[k];
                {


                    for (int u = 0; u < pairs.Count(); u++)
                    {
                        pairs[u] = new PairControls(u);

                    }
                    return pairs;
                }
              
            }


        }
       public class PairControls
       {
           public PairControls()
           {
           }
           public PairControls(int i)
           {
               textbox = new TextBox
               {
                   Name = "TextBox" + i.ToString(),
                   Text = "TextBox" + i.ToString(),
                   Location = new Point(300, 100 + i * 30)
               };
               label = new Label
               {
                   Name = "Label" + i.ToString(),
                   Text = "Label" + i.ToString(),
                   Location = new Point(100, 100 + i * 30)
               };
               datetime = new DateTimePicker
               {
                   Name = "DateTime" + i.ToString(),
                  Location = new Point(500, 100 + i * 30)
               };

           }
           public TextBox textbox { get; set; }
           public Label label { get; set; }
           public DateTimePicker datetime { get; set; }
       }
    ==========================================
    and i am accessing it is in my form as
    =================
     CreateControls objcontrols = new CreateControls();

                foreach (PairControls name11 in objcontrols.CreatePairControls(4))
                {
                    this.Controls.Add(name11.label);
                    this.Controls.Add(name11.textbox);
                    this.Controls.Add(name11.datetime);
                }
    =================================== ok working fine  also i have another class to create variables
    it is
    =========================
    class Class1
        {
            public Class1()
            { }

            public Tuple<string[], int[]> MyData()
            {
                string[] SomeStringItems = { "A", "B", "C" };
                int[] SomeIntItems = { 10, 20, 30 };
                return new Tuple<string[], int[]>(SomeStringItems, SomeIntItems);
            }
        }
    ============================== and i am accessing it is
    Class1 c = new Class1();

                Tuple<string[], int[]> Results = c.MyData();
                foreach (var Item in Results.Item1)
                {
                    MessageBox.Show(Item.ToString());
                }

          

                foreach (var Item in Results.Item2)
                {
                    MessageBox.Show(Item.ToString());
                }
    ========================================ok working fine  

    how i can write these codes in one class as seperate methods( i need to combine above classes to one class and access it how i can anybody please)

    hotnthought

    Saturday, August 09, 2014 10:47 AM

Answers

  • Just put the CreatePairControls and MyData() methods in the same class:

    class CommonClass
        {
            public PairControls[] CreatePairControls(int k)
            {
                PairControls[] pairs = new PairControls[k];
                {
    
    
                    for (int u = 0; u < pairs.Count(); u++)
                    {
                        pairs[u] = new PairControls(u);
    
                    }
                    return pairs;
                }
    
            }
    
            public Tuple<string[], int[]> MyData()
            {
                string[] SomeStringItems = { "A", "B", "C" };
                int[] SomeIntItems = { 10, 20, 30 };
                return new Tuple<string[], int[]>(SomeStringItems, SomeIntItems);
            }
        }
    

    You can then call both methods on the same object:

    CommonClass c = new CommonClass();
    
    foreach (PairControls name11 in c.CreatePairControls(4))
                 {
                     this.Controls.Add(name11.label);
                     this.Controls.Add(name11.textbox);
                     this.Controls.Add(name11.datetime);
                 }
    
                Tuple<string[], int[]> Results = c.MyData();
                 foreach (var Item in Results.Item1)
                 {
                     MessageBox.Show(Item.ToString());
                 }
    
           
    
                foreach (var Item in Results.Item2)
                 {
                     MessageBox.Show(Item.ToString());
                 }
    
    Please remember to mark any helpful posts as answer when your issue has been solved in order to close the thread.
    Saturday, August 09, 2014 1:42 PM

All replies

  • I'm a bit concerned at the overall approach here mate.

    What you appear to be doing is writing an entire application question by question.

    You would do WAY better taking a course or with one on one coaching.

    .

    The answer is refactor your two pieces of code into two methods in one class.

    Saturday, August 09, 2014 11:05 AM
  • yes what is the problem?
    Saturday, August 09, 2014 11:39 AM
  • I don't think that this forum is only for Andy OBeill.. there many others are supporting beginners like us

    if I can support ... please support.. otherwise leave it that's all

    whatever may be thanks a lot for your comment

    Saturday, August 09, 2014 11:54 AM
  • I think you've misunderstood my point.

    I am trying to help you.

    My concern is for the rate you will be learning.

    I think a course would be far better way for a beginner.

    A course ought to give you the big picture first so you can understand how all the pieces of knowledge fit together.

    As it is you're learning bits but it will be difficult to understand how they fit together and what other bits you haven't learnt.

    Also.

    The design of any system is best done top down.

    Without the big picture people can give advice with the best of intentions but that might turn out to be not so helpful.

    .

    Imagine someone is building something

    He asks "I need some structure, what's good?"

    Bricks... bricks are good... you can get them from ...

    He asks " I need to see out, what's good?"

    Double glazing is good... you can get that from...

    The risk is that without the big picture this advice may not be ideal.

    He eventually asks "I need a propeller and an engine.  What's good?"

    And we go "what do you mean engine?"

    He then explains it's a plane he's building.

    Saturday, August 09, 2014 1:23 PM
  • Just put the CreatePairControls and MyData() methods in the same class:

    class CommonClass
        {
            public PairControls[] CreatePairControls(int k)
            {
                PairControls[] pairs = new PairControls[k];
                {
    
    
                    for (int u = 0; u < pairs.Count(); u++)
                    {
                        pairs[u] = new PairControls(u);
    
                    }
                    return pairs;
                }
    
            }
    
            public Tuple<string[], int[]> MyData()
            {
                string[] SomeStringItems = { "A", "B", "C" };
                int[] SomeIntItems = { 10, 20, 30 };
                return new Tuple<string[], int[]>(SomeStringItems, SomeIntItems);
            }
        }
    

    You can then call both methods on the same object:

    CommonClass c = new CommonClass();
    
    foreach (PairControls name11 in c.CreatePairControls(4))
                 {
                     this.Controls.Add(name11.label);
                     this.Controls.Add(name11.textbox);
                     this.Controls.Add(name11.datetime);
                 }
    
                Tuple<string[], int[]> Results = c.MyData();
                 foreach (var Item in Results.Item1)
                 {
                     MessageBox.Show(Item.ToString());
                 }
    
           
    
                foreach (var Item in Results.Item2)
                 {
                     MessageBox.Show(Item.ToString());
                 }
    
    Please remember to mark any helpful posts as answer when your issue has been solved in order to close the thread.
    Saturday, August 09, 2014 1:42 PM