Reputation: 4895
I have got a global class, which saves all instances of another class in memory, everytime a new instance is added though, it should also be sent to another method which displays it. This is the code below:
/// <summary>
/// Notifications held
/// Array((int)type,(string)"message")
/// </summary>
private List<Classes.notificationObject> _notifications;
public List<Classes.notificationObject> notifications
{
get { return _notifications; }
set
{
if (_notifications != value)
{
_notifications = value;
OnPropertyChanged("notifications");
//Show new notification
Views.Notification x = new Views.Notification(value);
x.Show();
}
}
}
From my understanding I though that value
would be just the Classes.notificationObject
object, however i'm getting the following error:
Argument 1: cannot convert from
'System.Collections.Generic.List' to 'PhotoManagment.Classes.notificationObject'
This is the 'notificationObject':
public class notificationObject
{
private string _icon;
private string _message;
private string _detail;
private Color _color1;
private Color _color2;
public string Icon
{ get { return _icon; } set { _icon = value; }}
public string Message
{ get { return _message; } set { _message = value; }}
public string Detail
{ get { return _detail; } set { _detail = value; }}
public Color Color1
{ get { return _color1; } set { _color1 = value; }}
public Color Color2
{ get { return _color2; } set { _color2 = value; }}
public notificationObject newNotification(int type, string detail, string message)
{
//Create new instance of object
notificationObject x = new notificationObject();
switch (type)
{
case 1:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 2:
//Fatal
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 3:
//Unauthorized
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
case 4:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 241, 176, 24);
x.Color2 = Color.FromArgb(0, 205, 152, 28);
return x;
case 5:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 41, 161, 213);
x.Color2 = Color.FromArgb(0, 36, 142, 184);
return x;
case 6:
//Warning
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 94, 129, 7);
x.Color2 = Color.FromArgb(0, 138, 183, 28);
return x;
}
//Can't find error code
x.Icon = "";
x.Message = message;
x.Detail = detail;
x.Color1 = Color.FromArgb(0, 170, 60, 18);
x.Color2 = Color.FromArgb(0, 238, 78, 16);
return x;
}
Edit 1
Hmm, see if I was to use my 'runtimeObject' object now, and want to add a notificationObject
to my list, I would by calling `runtimeObjectInstance.Add()'.
However runtimeObject
will have many different properties, so isn't this a bad way to do it? Plus can't I do runtimeObjectInstance.notifications.Add()
without coding it myself?
Upvotes: 0
Views: 66
Reputation: 2143
This error means your property expects a List of notificationObject(s). But you are assigning a "notificationObject" instead. Instead of doing it this way, you can derive your Collection class from IList and then use the "Add" method to populate it. If that's too hard, you can create a class with List property and implement "Add" method to populate the above collection type property.
Make sure to initialise your collection in your classes constructor.
public class MyList
{
public MyList()
{
_notifications = new List<notificationObject>();
}
private List<notificationObject> _notifications;
public List<notificationObject> notifications
{
get { return _notifications; }
set
{
if (_notifications != value)
{
_notifications = value;
OnPropertyChanged("notifications");
//I am not sure about this bit of code. You may have to test it again after doing this change.
//Show new notification
//Views.Notification x = new Views.Notification(value);
//x.Show();
}
}
}
public void Add(notificationObject newnotificationObject)
{
notifications.Add(newnotificationObject);
OnPropertyChanged("notifications");
}
public void Remove(notificationObject existingnotificationObject)
{
if (notifications.Contains(existingnotificationObject))
{
notifications.Remove(existingnotificationObject);
OnPropertyChanged("notifications");
}
}
}
Here is a sample for the first method. If you are doing it this way, you will have implement all the methods.
public class MyList2 : IList<notificationObject>
{
public int IndexOf(notificationObject item)
{
throw new NotImplementedException();
}
public void Insert(int index, notificationObject item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public notificationObject this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void Add(notificationObject item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(notificationObject item)
{
throw new NotImplementedException();
}
public void CopyTo(notificationObject[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(notificationObject item)
{
throw new NotImplementedException();
}
public IEnumerator<notificationObject> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
Upvotes: 1