By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,140 Members | 1,295 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,140 IT Pros & Developers. It's quick & easy.

how would you share a variable between 2 classes in VC#?

P: 13
i'm trying to share the variable: target

This section of code:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             //draw rectangles for the edges filter
  3.             if( coordinateList.CalculateEdges().Length > 0 )
  4.             {
  5.                 using( Graphics graphics = Graphics.FromImage( newImage ) )
  6.                 {
  7.                     graphics.DrawRectangles(
  8.                         new Pen( Color.Blue, 1 ),
  9.                         coordinateList.CalculateEdges());
  10.                     //code i added to display 'target'
  11.                     graphics.DrawRectangle(
  12.                         new Pen(Color.Red, 2), target);
  13.                 }
  14.             }


Is in the ImageProcessing class and this;
Expand|Select|Wrap|Line Numbers
  1.  
  2.                 if (coordinateList.Count != 0)
  3.                 {
  4.  
  5.                     int x1 = 0;
  6.                     foreach (PointOfInterest p in coordinateList)
  7.                     {
  8.                         x1 += p.XCoordinate;
  9.                     }
  10.                     double aX = x1 / coordinateList.Count;
  11.  
  12.                     int y1 = 0;
  13.                     foreach (PointOfInterest p in coordinateList)
  14.                     {
  15.  
  16.                         y1 += p.YCoordinate;
  17.  
  18.                     }
  19.  
  20.                     double aY = y1 / coordinateList.Count;
  21.  
  22.  
  23.  
  24.                     int x = (int)Math.Round(aX, 0);
  25.  
  26.                     int y = (int)Math.Round(aY, 0);
  27.  
  28.  
  29.  
  30.                     PointOfInterest target = new PointOfInterest(x, y);
  31.  
  32.                     coordinateList.Add(target);
  33.  
  34.  
  35.  
  36.                 }
  37.  
is in the CoordinateList inner-class. I could never get classes to share a variable..Itís as if I need a global variable but C# of course doesnít use them plus it would be considered bad OOP.
Feb 3 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Sharing variables is a big no-no.

Just write a method in class A that returns the value. Have class B call that method, fiddle with the value and then call another method in A to record the new value.
Feb 3 '08 #2

Post your reply

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