9.4: FgfsDataObject
#region Usings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using FgfsSharp;
#endregion
namespace FgfsSharp
{
//TODO: Documentation
public class FgfsDataObject : IObservable
{
#region Privates
//the obeserver list
private List _observer;
private int _speed;
private int _altitude;
private int _roll;
private int _pitch;
private int _headingMagneticNorth;
#endregion
#region Properties
public int Speed
{
get {return _speed;}
set { _speed = value; }
}
public int Altitude
{
get { return _altitude; }
set { _altitude = value; }
}
public int Roll
{
get { return _roll; }
set { _roll = value; }
}
public int Pitch
{
get { return _pitch; }
set { _pitch = value; }
}
public int HeadingMagN
{
get { return _headingMagneticNorth; }
set { _headingMagneticNorth = value; }
}
#endregion
#region Constructor
public FgfsDataObject()
{
Console.WriteLine("Constructor: DataObject");
_observer = new List();
}
#endregion
#region Interface Methods
public void RemoveObserver(IObserver observer)
{
int i = _observer.IndexOf(observer);
if (i >= 0)
_observer.Remove(observer);
}
public void RegisterObserver(IObserver observer)
{
this._observer.Add(observer);
Console.WriteLine("Observer {0} added", observer.ToString());
}
public void NotifyObservers()
{
//update every observer here!
foreach (IObserver observer in this._observer)
{
//Console.WriteLine("Notifying...");
observer.UpdateObserver(this);
}
}
#endregion
}
}
June 10, 2008 at 5:24 pm |
This software does not compile due to the use of “List”, according to the compiler, “Using the generic type ‘System.Collections.Generic.List’ requires ‘1’ type arguments”
June 20, 2008 at 2:53 am |
I am not familiar with C# and I’m having difficulty with this particular code. Specifically, with the private List _observer declaration. Should that be of some form List? I really do not know what to do with it and can’t seem to find the answer anywhere.
June 20, 2008 at 3:08 am |
such as List ?
June 23, 2008 at 12:15 pm |
@Dustin:
A list is a c# collection. Think of it as an ‘dynamically growable’ array. E.g. the c# collections contain an ArrayList. You can (after creating it) easily add objects to it by it’s .Add()-Method. The disadvantage is, that the ArrayList contains objects, which means, that you have to cast the objects when you iterate over the collection. You can declare a ‘typed’ List:
List myPlaneList = new List();
This is the notation for a List containing only plane-objects.
@Brent:
I had a quick look at the code above. As I mentioned above, replace the accordant code with the following:
List _observer = new List();
June 23, 2008 at 12:18 pm |
oops, ‘less than’ or ‘greater than’ signs aren’t posted (until you’ll write ‘ampersand’ + lt/gt + semicolon)
The right notation is:
List<IObserver> _observer = new List<IObserver>();