Object life 
Author Message
 Object life

Hi -

OK - this problem is about information flow between classes. Imagine I have
two hierarchically identical  classes between which I want to pass some
variables (product of an internal method, say). I don't want to do the
following because TestClassII is a large form and it seems silly
instantiating such a large class just to get two variables from it:

using System;

namespace Test1
{
 public class StartUp
 {
  public static void Main()
  {
   int xxx,yyy;
   TestClassII tClassII = new TestClassII();
   TestClassIII tClassIII = new TestClassIII();
   tClassIII.xx = tClassII.x;
   tClassIII.yy = tClassII.y;
   xxx = tClassIII.xx;
   yyy = tClassIII.yy;

   Console.WriteLine("xxx = " + xxx);
   Console.WriteLine("yyy = " + yyy);
  }
 }
 public class TestClassII
 {
  public int x=1;
  public int y=1;

  public TestClassII()
  {
  }
 }
 public class TestClassIII
 {
  public int xx=0;
  public int yy=0;

  public TestClassIII()
  {
  }
 }

Quote:
}

I would rather (erroneously or not, I don't know) use a third class as a
"postman" to send the variables from one class to another, something like
this:
using System;

namespace Test1
{
 public struct Container
 {
  public int xa;
  public int ya;
 }

 public class StartUp
 {
  public static void Main()
  {
   int xxx,yyy;
   TestClassII tClassII = new TestClassII();
   tClassII.MethodII();
   TestClassIII tClassIII = new TestClassIII();
   tClassIII.MethodIII();
   xxx = tClassIII.xx;
   yyy = tClassIII.yy;

   Console.WriteLine("xxx = " + xxx);
   Console.WriteLine("yyy = " + yyy);
  }
 }
 public class TestClassII
 {
  public int x=1;
  public int y=1;

  public TestClassII()
  {
  }

  public void MethodII()
  {
   Container cont = new Container();
   cont.xa = x;
   cont.ya = y;
  }
 }
 public class TestClassIII
 {
  public int xx=0;
  public int yy=0;

  public TestClassIII()
  {
  }

  public void MethodIII()
  {
   Container cont = new Container();
   xx = cont.xa;
   yy = cont.ya;
  }
 }

Quote:
}

However, the above doesn't work.Couldn't anybody put me straight here?

Many thanks,

Chris.



Sun, 21 Nov 2004 17:36:58 GMT  
 Object life
Put attention, that you don't really instantiate class
for "info flow", but just pass a reference, which is quite
chip!!!

Anyway, second design is better for code brevity
(altrougth performance doesn't change). In such case make
2 variables in Container static and use following syntax
for access (without new!):
Container.xx=...



Sun, 21 Nov 2004 18:02:22 GMT  
 Object life
That's it <gg>! Thanks very much for your help Eliyahu!

Chris.



Quote:
> Put attention, that you don't really instantiate class
> for "info flow", but just pass a reference, which is quite
> chip!!!

> Anyway, second design is better for code brevity
> (altrougth performance doesn't change). In such case make
> 2 variables in Container static and use following syntax
> for access (without new!):
> Container.xx=...



Sun, 21 Nov 2004 18:19:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. synscro objects- life

2. Life of temporary CString objects?

3. ATL Server (unmanaged code) life cycle question

4. a bugs life: a programmer terror!!

5. An Event That Could Change Your Life

6. An Event That Could Change Your Life

7. life game source code

8. Artificial life please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

9. Artificial life please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

10. Quick question on scope and life-of-variables

11. Langton's Ant not Conway's Game of Life

12. An Event That Could Change Your Life

 

 
Powered by phpBB® Forum Software