473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

smart argument passing

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

(1) Now I have ServiceChild.MyMethod() 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(inheriting) class inside inherited class...

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. public class Child : Form1
  2. {
  3.   public Child()
  4.   {
  5.   }
  6.  
  7.   public void MyMethod()
  8.   {
  9.   }
  10.  
  11.   public void printarr()
  12.   {
  13.   }
  14. }
  15.  
  16. ///////////////////////////////
  17.  
  18. public partial class Form1 : Form
  19. {
  20.   public Form1()
  21.   {
  22.     InitializeComponent();
  23.   }
  24.  
  25.   private Child ch = new Child(); // runtime error - possible infinite recursion 
  26.   protected int[] array1 = new int[] {0, 1, 2, 3}; // inherited member 
  27.  
  28.   private void button2_Click(object sender, EventArgs e)
  29.   {
  30.     ch.printarr(); 
  31.   }
  32.  
  33.   private void button1_Click(object sender, EventArgs e)
  34.   {
  35.     ch.MyMethod(); 
  36.   } 
  37. }
Is there another "smart" way of handling this (1) ?
Nov 14 '09 #1
5 1912
tlhintoq
3,525 Expert 2GB
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)

Expand|Select|Wrap|Line Numbers
  1.     static class Program
  2.     {
  3.         #region Fields
  4.         public bool  isProcessing = false;
  5.         public bool List<string> fileNames = new List<string>();
  6.         #endregion Fileds
  7.      }
  8.  
  9.      public partial class From1 : Form
  10.     {
  11.          bool processing
  12.          {
  13.               get
  14.                  {
  15.                     return Program.isProcessing;
  16.                  }
  17.          }
  18.  
  19.            void SomeMethod()
  20.            {
  21.                 if (!processing)
  22.                 {
  23.                     foreach (string bob in Program.fileNames)
  24.                     {
  25.                         // Do some stuff here
  26.                         // based on the commonly visible variable isProcessing via a get/set property accessor
  27.                         // using the commonly visible variable fileNames directly referenced.
  28.                      }
  29.                 } 
  30.           }                
  31.     }
  32.  
Nov 14 '09 #2
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:
Expand|Select|Wrap|Line Numbers
  1. static class Program
  2. {
  3.   /// <summary>
  4.   /// The main entry point for the application.
  5.   /// </summary>
  6.   [STAThread]
  7.   static void Main()
  8.   {
  9.     Application.EnableVisualStyles();
  10.     Application.SetCompatibleTextRenderingDefault(false);
  11.     Application.Run(new Form1());
  12.   }
  13.  
  14.   #region Fields
  15.   public int[] array1 = new int[] { 0, 1, 2, 3 }; // 'Nasledovanie.Program.array1': cannot declare instance members in a static class
  16.   public System.Windows.Forms.RichTextBox richTextBox1; // 'Nasledovanie.Program.richTextBox1: cannot declare instance members in a static class
  17.   #endregion Fileds
  18. }
Nov 14 '09 #3
Markus
6,050 Expert 4TB
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.
Nov 14 '09 #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.
line 34: same runtime err

Expand|Select|Wrap|Line Numbers
  1. public class Child : Form1
  2. {
  3.   public Child()
  4.   {
  5.   }
  6.  
  7.   // change array values 
  8.   public void method1()
  9.   {
  10.     for (int i = 0; i < Program.array1.Length; i++)
  11.       Program.array1[i] = Program.array1.Length - 1 - Program.array1[i]; 
  12.   }
  13.  
  14.   // print array 
  15.   public void printarr()
  16.   {
  17.     int i = 0;
  18.     Program.richTextBox1.Text = ""; // clear richTextBox
  19.     for (; i < Program.array1.Length - 1; i++)
  20.       Program.richTextBox1.Text += Program.array1[i] + ", ";
  21.     Program.richTextBox1.Text += Program.array1[i]; // last value w\o comma (,)
  22.   }
  23. }
  24.  
  25. ///////////////////////////////
  26.  
  27. public partial class Form1 : Form
  28. {
  29.   public Form1()
  30.   {
  31.     InitializeComponent();
  32.   }
  33.  
  34.   private Child ch = new Child(); // runtime error - possible infinite recursion 
  35.  
  36.   private void button2_Click(object sender, EventArgs e)
  37.   {
  38.     ch.printarr(); 
  39.   }
  40.  
  41.   private void button1_Click(object sender, EventArgs e)
  42.   {
  43.     ch.method1(); 
  44.   } 
  45. }
  46.  
  47. ///////////////////////////////
  48.  
  49. static class Program
  50. {
  51.   /// <summary>
  52.   /// The main entry point for the application.
  53.   /// </summary>
  54.   [STAThread]
  55.   static void Main()
  56.   {
  57.     Application.EnableVisualStyles();
  58.     Application.SetCompatibleTextRenderingDefault(false);
  59.     Application.Run(new Form1());
  60.   }
  61.  
  62.   #region Fields
  63.   static public int[] array1 = new int[] { 0, 1, 2, 3 };
  64.   static public System.Windows.Forms.RichTextBox richTextBox1;
  65.   #endregion Fileds
  66. }
Nov 14 '09 #5
Ah! Sorry. I should have removed this:
Expand|Select|Wrap|Line Numbers
  1. public class Child : Form1 
Nov 14 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
11
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
I have a problem with a cyclic dependency of two classes: class Iref_count // Interface for the intrusive_ptr { friend class int_ptr_base; // for access to Count private: volatile unsigned...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.