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.
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.
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
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?
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.
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.