C
C#2mo ago
Nakama

Extending a class while overwriting its properties

Hello, I'm trying to use MudCalendar and for this I need to use a CalendarItem. To make it possible to pass extra data I would like to extend my own DiscordEvent class, and followed by that whenever properties of Calendar Item are queried, retrieve data from the correct properties. I have done part of this already but it has ended with my having duplicate properties like in the picture below.
public class DiscordEvent : CalendarItem
{
[Key]
public int EventId { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public DateTime StartDate { get; set; }
public TimeSpan Duration { get; set; } = TimeSpan.FromHours(1);

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime ModifiedAt { get; set; } = DateTime.UtcNow;

[NotMapped]
public new string Id { get; set; } = Guid.NewGuid().ToString();
[NotMapped]
public new DateTime Start
{
get
{
return StartDate;
}
set
{
StartDate = value;
}
}
[NotMapped]
public new DateTime? End
{
get
{
return StartDate + Duration;
}
set
{
value ??= StartDate + Duration;
Duration = value.Value - StartDate;
}
}
[NotMapped]
public new bool AllDay { get; set; }
[NotMapped]
public new string Text
{
get
{
return Name ?? string.Empty;
}
set { Name = value; }
}
[NotMapped]
public new bool IsMultiDay { get; set; }
}
public class DiscordEvent : CalendarItem
{
[Key]
public int EventId { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public DateTime StartDate { get; set; }
public TimeSpan Duration { get; set; } = TimeSpan.FromHours(1);

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime ModifiedAt { get; set; } = DateTime.UtcNow;

[NotMapped]
public new string Id { get; set; } = Guid.NewGuid().ToString();
[NotMapped]
public new DateTime Start
{
get
{
return StartDate;
}
set
{
StartDate = value;
}
}
[NotMapped]
public new DateTime? End
{
get
{
return StartDate + Duration;
}
set
{
value ??= StartDate + Duration;
Duration = value.Value - StartDate;
}
}
[NotMapped]
public new bool AllDay { get; set; }
[NotMapped]
public new string Text
{
get
{
return Name ?? string.Empty;
}
set { Name = value; }
}
[NotMapped]
public new bool IsMultiDay { get; set; }
}
The duplicate properties have (Heron.MudCalendar.CalendarItem) behind them. How do I fix this?
No description
7 Replies
ACiDCA7
ACiDCA72mo ago
you arent overwriting the properties because of the new keyword you added to every property
Nakama
Nakama2mo ago
So I've removed the "new" keyword and the result unfortunately remains the same.
public new string Id { get; set; } = Guid.NewGuid().ToString();
[NotMapped]
public DateTime Start
{
get
{
return StartDate;
}
set
{
StartDate = value;
}
}
[NotMapped]
public DateTime? End
{
get
{
return StartDate + Duration;
}
set
{
value ??= StartDate + Duration;
Duration = value.Value - StartDate;
}
}
[NotMapped]
public bool AllDay { get; set; }
[NotMapped]
public string Text
{
get
{
return Name ?? string.Empty;
}
set { Name = value; }
}
public new string Id { get; set; } = Guid.NewGuid().ToString();
[NotMapped]
public DateTime Start
{
get
{
return StartDate;
}
set
{
StartDate = value;
}
}
[NotMapped]
public DateTime? End
{
get
{
return StartDate + Duration;
}
set
{
value ??= StartDate + Duration;
Duration = value.Value - StartDate;
}
}
[NotMapped]
public bool AllDay { get; set; }
[NotMapped]
public string Text
{
get
{
return Name ?? string.Empty;
}
set { Name = value; }
}
ACiDCA7
ACiDCA72mo ago
are the properties you want to overwrite even virtual/abstract?
Nakama
Nakama2mo ago
Apologies I forgot to include the CalendarItem class. None of the properties are virtual or abstract.
public class CalendarItem
{
protected internal readonly string Id = Guid.NewGuid().ToString();

public DateTime Start { get; set; }

public DateTime? End { get; set; }

public bool AllDay { get; set; }

public string Text { get; set; } = string.Empty;


protected internal bool IsMultiDay
{
get
{
if (End.HasValue || !(Start.TimeOfDay > TimeSpan.FromHours(23.0)))
{
if (End.HasValue)
{
return End.Value.Date > Start.Date;
}

return false;
}

return true;
}
}
}
public class CalendarItem
{
protected internal readonly string Id = Guid.NewGuid().ToString();

public DateTime Start { get; set; }

public DateTime? End { get; set; }

public bool AllDay { get; set; }

public string Text { get; set; } = string.Empty;


protected internal bool IsMultiDay
{
get
{
if (End.HasValue || !(Start.TimeOfDay > TimeSpan.FromHours(23.0)))
{
if (End.HasValue)
{
return End.Value.Date > Start.Date;
}

return false;
}

return true;
}
}
}
ACiDCA7
ACiDCA72mo ago
then make them virtual when they are virtual you can use the keyword override in the subclass @Nakama
Nakama
Nakama2mo ago
Its not my class, its from the MudCalendar, a nugget package I am using The calendar takes : Items IEnumerable<CalendarItem> The data to display in the Calendar. https://danheron.github.io/Heron.MudCalendar/api/calendar#properties
ACiDCA7
ACiDCA72mo ago
then these properties werent intended to be overriden so you would have to live with that the object has two properties with the same name, base and sub class what oyu could do tho is to pass the changes of the subtype up to the basetype eg.
public DateTime Start
{
get
{
return StartDate;
}
set
{
base.Start = value; // or something in that direction
StartDate = value;
}
}
public DateTime Start
{
get
{
return StartDate;
}
set
{
base.Start = value; // or something in that direction
StartDate = value;
}
}
why are you eventrying to override them anyway? just keep and use them and then add your own stuff