CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2008
    Posts
    10

    how would you share a variable between 2 classes?

    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.
    Last edited by David24; February 3rd, 2008 at 03:48 PM.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how would you share a variable between 2 classes?

    Please edit your post (NOT re-post) to include code tags. It is very difficult to read without them. I will look at the code after it is readable.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Oct 2007
    Posts
    25

    Re: how would you share a variable between 2 classes?

    I had a similar problem, altho I also wanted a "global" function or object of sorts. I found this page to be extremely useful:

    http://www.yoda.arachsys.com/csharp/singleton.html

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how would you share a variable between 2 classes?

    Thanks for the edit, sorry it took a while to get back to you....

    If you are only going to have 1 "PointOfInterest", then simply use a Singleton.

    If you are going to have multiple instances of "PointOfInterest", how would you coordinate them?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Sep 2004
    Posts
    1,361

    Re: how would you share a variable between 2 classes?

    Aside from just passing the reference around via methods, you could just use a static class or static container class.

    For example, you could have a static class called "Player" which has a "Target". You could then just reference the player's target anywhere like: Player.Target.

    When writing a game, I generally have a big static instance which contains all my global stuff such as a list of player objects, settings, and whatnot. So anywhere I could do: Game.Player[N].Score += 100;

    A singleton is just as good as long as there is a static method to get the reference. I prefer the static class method myself though.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how would you share a variable between 2 classes?

    Quote Originally Posted by DeepT
    A singleton is just as good as long as there is a static method to get the reference. I prefer the static class method myself though.
    Since you brought it up.... Yes a static class is an alternative. There are some advantages to a static class, but there are also some limitations that can "bite" you.

    Taking your example (Games State)...With a static class, it is non-trivial to take a "snapshot" of your global state so that you could restore it at a later point (e.g. Go back to this point in time if I get killed). With a Singleton, it is trivial to add a static method that generates a Clone of the structure.

    This also appliens during unit, functional, and system level testing, where it is desirable to have multiple instances, even if the application never will.

    One additional benefit it that it is much easier to control the lifetime of a singleton. There are many design patterns where you need a single instance for only a portion of the excutables duration. Cleaning up a singleton can be done with one very simple static method. Cleaning up a static clas can be much more difficult.

    Finally, I have seen to many cases where something that was thought to be a Single Instance (or No Instance) object, later requires multiples. Scott Meyers gave the best example of a Printer (the application only talks to one) evolving into an application that required a Color and a B/W printer.

    So my preference (and it is preference, provideding you have understanding), is to almost never use static classes and vastly prefer singletons.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured