C# Event Handling - Event Handler remains null - c#

I am a beginner to C# programming.
When I run this program, the eventhandler named - newMessagePublishedEventHandler of the class MessagePool is always null while executing the OnNewMessagePublished() function of the same class.
Can anyone please tell me why? and also how could I resolve this?
namespace ConsoleApp
{
public class MessageEventArgs : System.EventArgs
{
private int publisherId;
private string message;
public MessageEventArgs(int publisherId, string messageText)
{
this.publisherId = publisherId;
this.message = messageText;
}
public int PublisherId
{
get { return publisherId; }
}
public string Message
{
get { return message; }
}
}
public class Publisher
{
private int publisherId;
public Publisher(int publisherId)
{
this.publisherId = publisherId;
}
public int PublisherId
{
get { return publisherId; }
}
public void Publish(string message)
{
MessagePool.GetInstance().Publisher_PostMessage(this.publisherId, message);
Console.WriteLine("published message - " + message);
}
}
public class MessagePool
{
private static MessagePool theOnlyInstanceOfMessageBroker;
public static MessagePool GetInstance()
{
if (theOnlyInstanceOfMessageBroker == null)
{
return new MessagePool();
}
else
{
return theOnlyInstanceOfMessageBroker;
}
}
private MessagePool()
{
}
private EventHandler newMessagePublishedEventHandler;
public event EventHandler NewMessagePublished
{
add
{
newMessagePublishedEventHandler += value;
}
remove
{
newMessagePublishedEventHandler -= value;
}
}
public void Publisher_PostMessage(int publisherId, string message)
{
DateTime publishedTime = DateTime.Now;
this.OnNewMessagePublished(publisherId, message);
}
private void OnNewMessagePublished(int publisherId, string message)
{
MessageEventArgs eventArgs = new MessageEventArgs(publisherId, message);
if (newMessagePublishedEventHandler != null)
{
newMessagePublishedEventHandler(this, eventArgs);
}
}
}
public class Subscriber
{
private int subscriberId;
public Subscriber(int subscriberId)
{
this.subscriberId = subscriberId;
this.SubscribeToMessagebroker();
}
public int SubscriberId
{
get { return subscriberId; }
}
private void SubscribeToMessagebroker()
{
MessagePool.GetInstance().NewMessagePublished -= Subscriber_NewMessageArrived;
MessagePool.GetInstance().NewMessagePublished += Subscriber_NewMessageArrived;
}
private void Subscriber_NewMessageArrived(object sender, EventArgs eventArgs)
{
if (eventArgs != null && eventArgs is MessageEventArgs)
{
var data = eventArgs as MessageEventArgs;
if (data != null)
Console.WriteLine("Recieved message : '" + data.Message + "' from " + data.PublisherId);
}
}
}
class Program
{
static void Main(string[] args)
{
Subscriber subscriber = new Subscriber(1);
Publisher publisher = new Publisher(1001);
publisher.Publish("Hey man.. whats up?");
Console.ReadLine();
}
}
}

MessagePool is not a proper Singleton, you are always returning a new MessagePool.
public class MessagePool
{
private static MessagePool theOnlyInstanceOfMessageBroker;
private static object _syncRoot = new object();
public static MessagePool GetInstance()
{
if (theOnlyInstanceOfMessageBroker == null)
{
lock(_syncRoot)
{
if (theOnlyInstanceOfMessageBroker == null)
theOnlyInstanceOfMessageBroker = new MessagePool();
}
}
return theOnlyInstanceOfMessageBroker;
}
private MessagePool()
{
}
//...
}

Related

NetMQ Actor pattern

I'm trying to have one WPF application to communicate with an other application when something change on the UI. For example : a slider changes and I send the new value or a textbox input change and I send the new input value.
The second application is listening and receive those changes to update different properties.
To achieve this I'm using NetMQ following the Actor pattern example from the documentation : https://netmq.readthedocs.io/en/latest/actor/
Right now I have these Client/Server class :
The Server :
public class NetMQServer
{
public class ShimHandler : IShimHandler
{
private PairSocket shim;
private NetMQPoller poller;
private PublisherSocket publisher;
private string Address;
public ShimHandler(string address)
{
Address = address;
}
public void Initialise(object state)
{
}
public void Run(PairSocket shim)
{
using (publisher = new PublisherSocket())
{
publisher.Bind(Address);
publisher.Options.SendHighWatermark = 1000;
this.shim = shim;
shim.ReceiveReady += OnShimReady;
shim.SignalOK();
poller = new NetMQPoller { shim, publisher };
poller.Run();
}
}
private void OnShimReady(object sender, NetMQSocketEventArgs e)
{
string command = e.Socket.ReceiveFrameString();
if(command == NetMQActor.EndShimMessage)
{
poller.Stop();
return;
}
else
{
byte[] byteMessage = e.Socket.ReceiveFrameBytes();
publisher.SendMoreFrame(command).SendFrame(byteMessage);
}
}
private void UpdateString(string stringmessage, string propertyToUpdate)
{
propertyToUpdate = stringmessage;
}
}
public NetMQServer(string ip, int port)
{
IP = ip;
Port = port;
Serializer = new CerasSerializer();
}
public CerasSerializer Serializer { get; set; }
private NetMQActor actor;
private string _name;
public string Name
{
get { return _name; }
set { _name = value;}
}
private int _port;
public int Port
{
get { return _port; }
set
{
_port = value;
ReStart();
}
}
private string _ip;
public string IP
{
get { return _ip; }
set
{
_ip = value;
ReStart();
}
}
public string Address
{
get { return String.Format("tcp://{0}:{1}", IP, Port); }
}
public void Start()
{
if (actor != null)
return;
actor = NetMQActor.Create(new ShimHandler(Address));
}
public void Stop()
{
if (actor != null)
{
actor.Dispose();
actor = null;
}
}
public void ReStart()
{
if (actor == null)
return;
Stop();
Start();
}
public void SendObject(string topic, object commandParameter)
{
if (actor == null)
return;
byte[] Serialized = Serializer.Serialize(commandParameter);
var message = new NetMQMessage();
message.Append(topic);
message.Append(Serialized);
actor.SendMultipartMessage(message);
}
}
Here when the property of a control change I call SendStringMessage(string stringToSend) function and it is sending with the NetMQ publisher (the string is only for testing, I could send whatever object as byte too).
Anyway, here is the client running on the second app :
public class NetMQClient
{
public class ShimHandler : IShimHandler
{
private PairSocket shim;
private NetMQPoller poller;
private SubscriberSocket subscriber;
private ByteMessage ByteMessage;
private string Address;
private string Topic;
private string MessageType;
public ShimHandler(ByteMessage byteMessage, string address, string topic)
{
this.ByteMessage = byteMessage;
this.Address = address;
this.Topic = topic;
}
public void Initialise(object state)
{
}
public void Run(PairSocket shim)
{
using (subscriber = new SubscriberSocket())
{
subscriber.Connect(Address);
subscriber.Subscribe(Topic);
subscriber.Options.ReceiveHighWatermark = 1000;
subscriber.ReceiveReady += OnSubscriberReady;
this.shim = shim;
shim.ReceiveReady += OnShimReady;
shim.SignalOK();
poller = new NetMQPoller { shim, subscriber };
poller.Run();
}
}
private void OnSubscriberReady(object sender, NetMQSocketEventArgs e)
{
string topic = e.Socket.ReceiveFrameString();
if (topic == Topic)
{
this.ByteMessage.Message = e.Socket.ReceiveFrameBytes();
}
}
private void OnShimReady(object sender, NetMQSocketEventArgs e)
{
string command = e.Socket.ReceiveFrameString();
if (command == NetMQActor.EndShimMessage)
{
poller.Stop();
}
}
}
public ByteMessage ByteMessage { get; set; }
private NetMQActor actor;
private string _command;
public string Command
{
get { return _command; }
set { _command = value;}
}
private string _topic;
public string Topic
{
get { return _topic; }
set
{
_topic = value;
ReStart();
}
}
private int _port;
public int Port
{
get { return _port; }
set
{
_port = value;
ReStart();
}
}
private string _ip;
public string IP
{
get { return _ip; }
set
{
_ip = value;
ReStart();
}
}
public string Address
{
get { return String.Format("tcp://{0}:{1}", IP, Port); }
}
public NetMQClient(string ip, int port, string topic)
{
ByteMessage = new ByteMessage();
IP = ip;
Port = port;
Topic = topic;
}
public void Start()
{
if (actor != null)
return;
actor = NetMQActor.Create(new ShimHandler(ByteMessage, Address, Topic));
}
public void Stop()
{
if (actor != null)
{
actor.Dispose();
actor = null;
}
}
public void ReStart()
{
if (actor == null)
return;
Stop();
Start();
}
}
Now the ByteMessage class just look as simple as this :
public class ByteMessage : INotifyPropertyChanged
{
public ByteMessage()
{
}
private byte[] _message;
public byte[] Message
{
get { return _message; }
set
{
_message = value;
OnPropertyChanged("Message");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
One the second App, Classes will have a NetMQClient as property and register to the OnPropertyChange event of the NetMQClient's ByteMessage.
Now when ByteMessage.Message is updated from the ShimHandler I can Deserialize the data and do something with it.
So far this is working but I'm really not sure if I'm doing it correctly... and if this is ThreadSafe
Shouldn't it be better if each class on the Listening app would have their own NetMQClient listening to a specific topic ?
Really confuse here how to use all that, even after reading all examples provided around NetMQ.
Thanks

How to asynchronously report progress of Zip file creation to status bar?

I'm trying to report progress to my status bar when creating a zip file.
For reporting the progress of the creation i have used the following guide:
ZipFile Creation Report Progress
My status bar is currently updated using 2 classes:
an INotifyPropertyChanged class called StatusBar
a separate class called Status.
The StatusBar class:
public class StatusBar : INotifyPropertyChanged
{
private string _message;
private string _submessage;
private bool _isindeterminate;
private bool _visible;
private int _min;
private int _max;
private double _progress;
public event PropertyChangedEventHandler PropertyChanged;
public StatusBar() { }
public int Min
{
get { return this._min; }
set
{
this._min = value;
this.OnPropertyChanged("Min");
}
}
public int Max
{
get { return this._max; }
set
{
this._max = value;
this.OnPropertyChanged("Max");
}
}
public double Progress
{
get { return this._progress; }
set
{
this._progress = value;
this.OnPropertyChanged("Progress");
}
}
public string Message
{
get { return this._message; }
set
{
this._message = value;
this.OnPropertyChanged("Message");
}
}
public string SubMessage
{
get { return this._submessage; }
set
{
this._submessage = value;
this.OnPropertyChanged("SubMessage");
}
}
public bool IsIndeterminate
{
get { return this._isindeterminate; }
set
{
this._isindeterminate = value;
this.OnPropertyChanged("IsIndeterminate");
}
}
public bool Visible
{
get { return this._visible; }
set
{
this._visible = value;
this.OnPropertyChanged("Visible");
}
}
void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The Status class:
public class Status
{
public static void Initialise(StatusBar status, string _message, string _submessage, int _min, int _max, double _progress, bool _visible, bool _isindeterminate)
{
status.Visible = _visible;
if (_isindeterminate == false)
{
status.Message = _message;
status.SubMessage = _submessage;
status.Progress = _progress;
status.Min = _min;
status.Max = _max;
status.IsIndeterminate = _isindeterminate;
}
else
{
status.Message = _message;
status.IsIndeterminate = _isindeterminate;
}
}
public static void UpdateProgress(StatusBar status, double _progress)
{
if (_progress == status.Max)
{
Complete(status);
}
else
{
status.Progress = _progress;
}
}
public static void UpdateMessage(StatusBar status, string _message)
{
status.Message = _message;
}
public static void UpdateSubMessage(StatusBar status, string _submessage)
{
status.SubMessage = _submessage;
}
public static void UpdateProgressMessage(StatusBar status, string _message, double _progress)
{
status.Message = _message;
status.Progress = _progress;
}
public static void Complete(StatusBar status)
{
status.Message = "Ready";
status.SubMessage = " ";
status.Min = 0;
status.Max = 0;
status.Progress = 0;
status.Visible = false;
status.IsIndeterminate = false;
}
}
For creating the zip file and updating the status bar I currently have the following:
string sourceDirectory = "C:\\Access\\Test";
string archive = #"C:\Access\Test.zip";
Status.Initialise(statusbar, "Creating Zip File...", "", 0, 100, 0, true, false);
Backup.CreateFromDirectory(sourceDirectory, archive,
new BasicProgress<double>(p => Status.UpdateProgressMessage(statusbar, $"{p:P2} archiving complete", p)));
The code above is working and the Zip file is created.
However the progress is only reported once the process has been completed and not during the process.
How can I change this to update the progress during the zip file creation?

c# using other class method

Thanks to NHMountainGoat for an answer!
Implementing Interface looks a good choice so we have only the 'needed' method instanciated.
It looks like this now:
EDIT
class Machine
{
//REM: MachineConnexion is a link to the main server where asking the data
internal linkToPLC LinkToPLC;
public IlinkToPLC ILinkPLC;
public interface IlinkToPLC//Interface to linkPLC
{
Int16 MachineNumIS { get; set; }
}
internal class linkToPLC : IlinkToPLC
{
private Int16 Act_MachineNum;
private List<string> genlnkPLCCanvas;
private List<string> genlnkPLCworkingwith;
static private List<string> ListSymbolNoExist;
private string[] ListToPLClnk = {
"GlobalFolder.PMachine[{0}].",
"GlobalFolder.PMachine[{0}].STATE.",
"GlobalFolder.Machine[{0}].",
"GlobalFolder.Machine[{0}].STATE.",
};
public linkToPLC()//ctor
{
genlnkPLCCanvas = new List<string>(ListToPLClnk);
genlnkPLCworkingwith = new List<string>(ListToPLClnk);
ListSymbolNoExist = new List<string>();
Act_MachineNum = MachineNumIS;
}
public Int16 MachineNumIS { get { return (Int16)ReadWriteMachine("data"); } set { ReadWriteMachine("data", value); } }
public string ValueExist(string ValueToreach, bool WorkingDATA = false)
{
if (!WorkingDATA)
{
for (int inc = 0; inc < genlnkPLCworkingwith.Count; inc++)
{
string StrValueToReach = genlnkPLCworkingwith[inc] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[inc] + ValueToreach);
}
}
else if (WorkingDATA)
{
string StrValueToReach = genlnkPLCworkingwith[10] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[10] + ValueToreach);
}
if (ListSymbolNoExist.Count != 0)
{
string ErrorList = "";
for (int inc = 0; inc < ListSymbolNoExist.Count; inc++)
{
ErrorList = string.Concat(ErrorList + "Num: " + inc.ToString() + " " + ListSymbolNoExist[inc].ToString() + "\n");
}
Console.WriteLine("Error" + ErrorList);
}
return null;
}
public object ReadWriteMachine(string VariableName, object DataToWrite = null, bool WorkingDATA = false)
{
string valueToFind = "";
if (ValueExist(VariableName) != "FALSE")
{
if (DataToWrite != null) { MachineConnexion.WriteSymbol(valueToFind, DataToWrite); }
return MachineConnexion.ReadSymbol(valueToFind);
}
return VariableName;
}
}
public Machine() //constructor
{
LinkToPLC = new linkToPLC();
}
}
And It doesn't work telling me that the reference object is not defined to an instance of the object..... in the line : Machine() LinkToPLC = new linkToPLC();//REM I found the bug, it was me ;o)) 24112016
//REM 24112016
What are the main differences between those two concept: static Instance and Interface?
Example:
class Program
{
static void Main(string[] args)
{
ITestInterface InterInstance = new TestInterface();
//test Interface
bool value1 = true;
value1 = InterInstance.invert(value1);
InterInstance.print(value1);
//test Instance static
TestStaticInstance staticInstance = new TestStaticInstance();
staticInstance.Instance.invert(value1);
staticInstance.Instance.print(value1);
Console.ReadKey();
}
}
class TestInterface : ITestInterface
{
public bool invert(bool value)
{
return !value;
}
public void print(bool value)
{
Console.WriteLine(value.ToString()+"\n");
}
private void methodX()
{ }
}
interface ITestInterface
{
bool invert(bool value);
void print(bool value);
}
public class TestStaticInstance
{
public TestStaticInstance Instance;
public TestStaticInstance()
{
Instance = this;
}
internal bool invert(bool value)
{
return !value;
}
internal void print(bool value)
{
Console.WriteLine(value.ToString());
}
}
Thanks
Can you structure your other classes to take an instance of the link class? See:
/// <summary>
/// just a stub to demonstrate the model
/// </summary>
internal class Machine
{
public string ReadData() { return "this is data"; }
public void WriteData(string data) { Console.WriteLine(data); }
}
internal interface IMachineDataAccessor
{
string Read();
void Write(string data);
}
class LinkClass : IMachineDataAccessor
{
protected Machine _machine;
public LinkClass(Machine machine)
{
_machine = machine;
}
public void DoMyWork()
{
// insert work somewhere in here.
string dataFromMachine = Read();
Write("outbound data");
}
public string Read()
{
return _machine.ReadData();
}
public void Write(string data)
{
_machine.WriteData(data);
}
}
class PersistentClass
{
IMachineDataAccessor _machineImpl;
public PersistentClass(IMachineDataAccessor machineAccessImplementation)
{
_machineImpl = machineAccessImplementation;
}
public void DoMyWork()
{
string dataFromMachine = _machineImpl.Read();
// insert work here. Or anywhere, actually..
_machineImpl.Write("outbound data");
}
}
class StateClass
{
IMachineDataAccessor _machineImpl;
public StateClass(IMachineDataAccessor machineAccessImplementation)
{
_machineImpl = machineAccessImplementation;
}
public void DoMyWork()
{
string dataFromMachine = _machineImpl.Read();
// insert work here. Or anywhere, actually..
_machineImpl.Write("outbound data");
}
}
static void Main(string[] args)
{
LinkClass link = new LinkClass(new Machine());
PersistentClass persistent = new PersistentClass(link as IMachineDataAccessor);
StateClass state = new StateClass(link as IMachineDataAccessor);
persistent.DoMyWork();
state.DoMyWork();
link.DoMyWork();
}

System.TypeInitializationException in Uwp using mvvm?

ResourceDictionary:
<locator:ConstructionLocator x:Key="Construction"/>
addEngineerView.xaml
<Page DataContext="{Binding Source={StaticResource Construction}, Path=AddEngineerViewModel}"/>
Locator:
public class ConstructionLocator
{
private static IDialogServices _dialog = new DialogServices();
private static IAddEngineerDataService _addEngineerDataServices = new AddEngineerDataServices(new AddEngineerData());
private static AddEngineerViewModel _addEngineerViewModel = new AddEngineerViewModel(_addEngineerDataServices, _dialog);
private static ViewEmployeeViewModel _viewEmployeeViewModel = new ViewEmployeeViewModel(_addEngineerDataServices, _dialog);
public static AddEngineerViewModel AddEngineerViewModel
{
get { return _addEngineerViewModel; }
}
public static ViewEmployeeViewModel ViewEmployeeViewModel
{
get { return _viewEmployeeViewModel; }
}
}
ViewModel:
public class AddEngineerViewModel:BindableBase
{
IAddEngineerDataService _engineerDataService;
IDialogServices _dialog;
public AddEngineerViewModel(IAddEngineerDataService AddEngineerDataService,IDialogServices Dialog)
{
_engineerDataService = AddEngineerDataService;
_dialog = Dialog;
LoadCommand();
}
private AddEngineerModel _addEngineer;
public AddEngineerModel AddEngineer
{
get { return _addEngineer; }
set
{
_addEngineer = value;
RaisePropertyChanged("AddEngineer");
}
}
public ICommand AddEngineerCommand { get; set; }
public ICommand ViewEngineerCommand { get; set; }
private void LoadCommand()
{
AddEngineerCommand = new CustomCommand(Add, CanAdd);
ViewEngineerCommand = new CustomCommand(Views, CanView);
}
private void Views(object obj)
{
_dialog.ShowDialog();
}
private bool CanView(object obj)
{
return true;
}
private bool CanAdd(object obj)
{
return true;
}
private void Add(object obj)
{
_engineerDataService.Add_Engineer_Details(_addEngineer);
}
}
When navigate to view on button click it shows Type initializer threw exception in locator file of addengineerviewmodel static property. Above are my locator and viewmodel. what would be the problem?

showing form by select from list invalid cast exception

I want to add two lists to the box. It doesn't have a problem with adding items to the listbox, but a problem occurs when I try to click on one of the items and show a related form with details filled in to allow users to make amendments. Obviously the form didn't show, but instead an error occurs, no matter if I was try to open the form "delivery" or "pickup". The problem still occurs on one line
Error:
An unhandled exception of type 'System.InvalidCastException' occurred
in coursework2.exe
Additional information: Unable to cast object of type 'System.String'
to type 'coursework2.Pickup'.
namespace coursework2
{
public partial class MainForm : Form
{
private DeliveryForm deliveryform = new DeliveryForm();
private List<Visit> requests = new List<Visit>();
private Visit theVisit = new Visit();
private PickupForm pickupform = new PickupForm();
public void requestVisit(Visit newVisit)
{
requests.Add(newVisit);
}
public MainForm()
{
InitializeComponent();
}
private void btnNpickup_Click(object sender, EventArgs e)
{
pickupform.pickup = new Pickup();
pickupform.ShowDialog();
if (pickupform.pickup != null)
{
Pickup newPu = pickupform.pickup;
theVisit.addPick(newPu);
List<String> listOfPic = theVisit.listofPicks();
listboxVisits.Items.AddRange(listOfPic.ToArray());
}
updateList();
//this will upload details from pickup form to the list
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnNdelivery_Click(object sender, EventArgs e)
{
deliveryform.delivery = new Delivery();
deliveryform.ShowDialog();
if (deliveryform.delivery != null)
{
Delivery newDe = deliveryform.delivery;
theVisit.addDeliver(newDe);
List<String> listOfDel = theVisit.listofDeliver();
listboxVisits.Items.AddRange(listOfDel.ToArray());
}
updateList();
//this will upload detail of the delivery to the list
}
private void btnVall_Click(object sender, EventArgs e)
{
}
private void updateList()
{
listboxVisits.Items.Clear();
List<String> listofVis = theVisit.LisitVisits();
listboxVisits.Items.AddRange(listofVis.ToArray());
}
private void listboxVisits_SelectedIndexChanged(object sender, EventArgs e)
{
listboxVisits.FormattingEnabled = false;
int index = listboxVisits.SelectedIndex;
if (listboxVisits.SelectedItems.Count>0)
{
object object1 = listboxVisits.SelectedItems[0];
if (object1 is Delivery)
{
Delivery deliver = (Delivery)object1;
deliveryform.delivery = deliver;
deliveryform.ShowDialog();
}
else
{
Pickup pick = (Pickup)object1;// this is where error occur
pickupform.pickup = pick;
pickupform.ShowDialog();
}
}
this is the pickup class
public class Pickup
{
private string pickupname;
private string pickupaddress;
private Visit collects;
private Delivery sends;
private DateTime pdatetime;
private string dname;
private string daddress;
private DateTime dtime;
public string PickupName
{
get { return pickupname; }
set { pickupname = value; }
}
public string PickupAddress
{
get { return pickupaddress; }
set { pickupaddress = value; }
}
public string Dname
{
get { return dname; }
set { dname = value; }
}
public string Daddress
{
get { return daddress; }
set {daddress = value; }
}
public DateTime Pdatetime
{
get { return pdatetime; }
set { pdatetime = value; }
}
public DateTime Dtime
{
get { return dtime; }
set { dtime = value; }
}
public override string ToString()
{
return pickupname + " " + pickupaddress + " " + pdatetime.ToString()+" "+dname+" "+daddress+" "+dtime.ToString()+" Pickup ";
}
}
this is the visit class
public class Visit : Customer
{
private Customer requester;
private DateTime datetime;
private Delivery reciever;
private Pickup collect;
public DateTime DateTime
{
get { return datetime; }
set { datetime = value; }
}
private List<Pickup> picks = new List<Pickup>();
private List<Visit> visits = new List<Visit>();
private List<Delivery> deliver = new List<Delivery>();
public void addDeliver(Delivery de)
{
//adding new Delivery ToString the visit
deliver.Add(de);
}
public List<String> listofDeliver()
{
List<string> listofDeliver = new List<string>();
foreach (Delivery de in deliver)
{
String deAsString = de.ToString();
listofDeliver.Add(deAsString);
}
return listofDeliver;
}
public Delivery getDeliver(int index)
{
int count = 0;
foreach (Delivery de in deliver)
{
if (index == count)
return de;
count++;
}
return null;
}
public void addPick(Pickup pu)
{
picks.Add(pu);
}
public List<String> listofPicks()
{
List<string> listofPicks = new List<string>();
foreach (Pickup pu in picks)
{
String puAsString = pu.ToString();
listofPicks.Add(puAsString);
}
return listofPicks;
}
public Pickup getPicks(int index)
{
int count = 0;
foreach (Pickup pu in picks)
{
if (index == count)
return pu;
count++;
}
return null;
}
public List<String> LisitVisits()
{
List<String> visits = new List<string>();
visits.AddRange(listofDeliver());
visits.AddRange(listofPicks());
return visits;
}
public Visit getVisits(int index)
{
int count = 0;
foreach (Visit vis in visits)
{
if (index == count)
return vis;
count++;
}
return null;
}
public string VisitDetails()
{
return collect.PickupName + " " + collect.PickupAddress + " Pickup " + reciever.DeliveryName + " " + reciever.DeliveryAddress + " Delivery";
}
}

Categories

Resources