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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David24
    New Member
    • Jan 2008
    • 13

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

    i'm trying to share the variable: target

    This section of code:
    Code:
                //draw rectangles for the edges filter
                if( coordinateList.CalculateEdges().Length > 0 )
                {
                    using( Graphics graphics = Graphics.FromImage( newImage ) )
                    {
                        graphics.DrawRectangles(
                            new Pen( Color.Blue, 1 ),
                            coordinateList.CalculateEdges());
                        //code i added to display 'target'
                        graphics.DrawRectangle(
                            new Pen(Color.Red, 2), target);
                    }
                }


    Is in the ImageProcessing class and this;
    Code:
                    if (coordinateList.Count != 0)
                    {
     
                        int x1 = 0;
                        foreach (PointOfInterest p in coordinateList)
                        {
                            x1 += p.XCoordinate;
                        }
                        double aX = x1 / coordinateList.Count;
                        
                        int y1 = 0;
                        foreach (PointOfInterest p in coordinateList)
                        {
    
                            y1 += p.YCoordinate;
    
                        }
    
                        double aY = y1 / coordinateList.Count;
    
     
    
                        int x = (int)Math.Round(aX, 0);
    
                        int y = (int)Math.Round(aY, 0);
    
     
    
                        PointOfInterest target = new PointOfInterest(x, y);
    
                        coordinateList.Add(target);
    
                        
    
                    }
    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    Working...