smart argument passing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EntryTeam
    New Member
    • Aug 2009
    • 55

    smart argument passing

    Hello.
    I'm trying to figure out the best way of communication between two classes:
    • MainForm.cs
    • ServiceChild.cs


    (1) Now I have ServiceChild.My Method() which gets around 10 arguments using REF keyword in order to use arrays and variables and returns via those arguments the result values.
    And I have more than one method of this type.

    I don't like this kind of programming and I'm trying to make the code look more clear and short.

    Yesterday I've tryed next scheme:

    ServiceChild inherits MainForm to gain access to all necessary feilds and members, instead of getting them as method arguments.
    The problem pops out on the run time - possible infinite recursion.
    The cause: it's not allowed to make copy of child(inheritin g) class inside inherited class...

    Here's the code:
    Code:
    public class Child : Form1
    {
      public Child()
      {
      }
            
      public void MyMethod()
      {
      }
            
      public void printarr()
      {
      }
    }
    
    ///////////////////////////////
    
    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private Child ch = new Child(); // runtime error - possible infinite recursion 
      protected int[] array1 = new int[] {0, 1, 2, 3}; // inherited member 
    
      private void button2_Click(object sender, EventArgs e)
      {
        ch.printarr(); 
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
        ch.MyMethod(); 
      } 
    }
    Is there another "smart" way of handling this (1) ?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    if lots of forms are going to need to access the same data/variables - then don't create the data/variables in one of the forms.

    Take a moment and stop and think about 'scope'. What is the scope of your variables? If you want the scope to be just a form then create them in a form. If you want the scope to be the entire program, then create the variables in the Program.cs file (public of course)

    Code:
        static class Program
        {
            #region Fields
            public bool  isProcessing = false;
            public bool List<string> fileNames = new List<string>();
            #endregion Fileds
         }
    
         public partial class From1 : Form
        {
             bool processing
             {
                  get
                     {
                        return Program.isProcessing;
                     }
             }
    
               void SomeMethod()
               {
                    if (!processing)
                    {
                        foreach (string bob in Program.fileNames)
                        {
                            // Do some stuff here
                            // based on the commonly visible variable isProcessing via a get/set property accessor
                            // using the commonly visible variable fileNames directly referenced.
                         }
                    } 
              }                
        }

    Comment

    • EntryTeam
      New Member
      • Aug 2009
      • 55

      #3
      Originally posted by tlhintoq
      If you want the scope to be the entire program, then create the variables in the Program.cs file (public of course)
      This causes compile problems:
      Code:
      static class Program
      {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
        }
      
        #region Fields
        public int[] array1 = new int[] { 0, 1, 2, 3 }; // 'Nasledovanie.Program.array1': cannot declare instance members in a static class
        public System.Windows.Forms.RichTextBox richTextBox1; // 'Nasledovanie.Program.richTextBox1: cannot declare instance members in a static class
        #endregion Fileds
      }

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        From the given error, I would believe that you need to give the members the 'static' keyword, as you have done with the method Main.

        Comment

        • EntryTeam
          New Member
          • Aug 2009
          • 55

          #5
          Originally posted by Markus
          From the given error, I would believe that you need to give the members the 'static' keyword, as you have done with the method Main.
          line 34: same runtime err

          Code:
          public class Child : Form1
          {
            public Child()
            {
            }
                  
            // change array values 
            public void method1()
            {
              for (int i = 0; i < Program.array1.Length; i++)
                Program.array1[i] = Program.array1.Length - 1 - Program.array1[i]; 
            }
                  
            // print array 
            public void printarr()
            {
              int i = 0;
              Program.richTextBox1.Text = ""; // clear richTextBox
              for (; i < Program.array1.Length - 1; i++)
                Program.richTextBox1.Text += Program.array1[i] + ", ";
              Program.richTextBox1.Text += Program.array1[i]; // last value w\o comma (,)
            }
          }
          
          ///////////////////////////////
          
          public partial class Form1 : Form
          {
            public Form1()
            {
              InitializeComponent();
            }
          
            private Child ch = new Child(); // runtime error - possible infinite recursion 
          
            private void button2_Click(object sender, EventArgs e)
            {
              ch.printarr(); 
            }
          
            private void button1_Click(object sender, EventArgs e)
            {
              ch.method1(); 
            } 
          }
          
          ///////////////////////////////
          
          static class Program
          {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new Form1());
            }
          
            #region Fields
            static public int[] array1 = new int[] { 0, 1, 2, 3 };
            static public System.Windows.Forms.RichTextBox richTextBox1;
            #endregion Fileds
          }

          Comment

          • EntryTeam
            New Member
            • Aug 2009
            • 55

            #6
            Ah! Sorry. I should have removed this:
            Code:
            public class Child [U]: Form1[/U]

            Comment

            Working...