Inconsistent Accessibility: Property Type Error - c#

I've done the googling to no avail. This is the one sole error preventing my code from compiling and running but I can't seem to figure it out. The exact text of the error is "...Dictionary is less accessible than property FleetAirliner.InsuranceProperties"
Any ideas what could be causing this?
namespace TheAirline.Model.AirlinerModel
{
[Serializable]
public class FleetAirliner
{
public Airliner Airliner { get; set; }
public string Name { get; set; }
public Airport Homebase { get; set; }
public enum PurchasedType { Bought, Leased,BoughtDownPayment }
public DateTime PurchasedDate { get; set; }
public PurchasedType Purchased { get; set; }
public Boolean HasRoute { get { return this.Routes.Count > 0; } set { ;} }
public AirlinerStatistics Statistics { get; set; }
/*Changed for deleting routeairliner*/
public enum AirlinerStatus { Stopped, On_route, On_service, Resting, To_homebase, To_route_start }
public AirlinerStatus Status { get; set; }
public Coordinates CurrentPosition { get; set; }
public List<Route> Routes { get; private set; }
public Flight CurrentFlight { get; set; }
public DateTime GroundedToDate { get; set; }
public List<Pilot> Pilots { get; set; }
public Dictionary<string, AirlinerInsurance> InsurancePolicies { get; set; } //error occurs here
public int NumberOfPilots {get {return this.Pilots.Count;} private set {;}}
public FleetAirliner(PurchasedType purchased,DateTime purchasedDate, Airline airline,Airliner airliner,Airport homebase)
{
this.Airliner = airliner;
this.Purchased = purchased;
this.PurchasedDate = purchasedDate;
this.Airliner.Airline = airline;
this.Homebase = homebase;
this.Name = airliner.TailNumber;
this.Statistics = new AirlinerStatistics(this);
this.Status = AirlinerStatus.Stopped;
this.CurrentPosition = new Coordinates(this.Homebase.Profile.Coordinates.Latitude, this.Homebase.Profile.Coordinates.Longitude);
this.Routes = new List<Route>();
this.Pilots = new List<Pilot>();
this.InsurancePolicies = new Dictionary<string, AirlinerInsurance>();
}

It means that class "AirlinerInsurance" Is not Public.
It is a property that is public, but other classes, that are allowed to use the property, might not have access rights to the class itself (it is private / internal).
Edit
Now that you have posted the code of class "AirlinerInsurance", just add a "public" modifier to it.
You can read more about it here and here

you need
class AirlinerInsurance {
// stuff
}
to be
public class AirlinerInsurance {
//stuff
}

Related

Property value changes without setter being called

I'm working on a project where a record needs to be inserted, using a context class. However, before the insertion, one of the properties 'DataArea' is being set to a value that it should not have.
I started debugging the code to see where exactly this value is being set, and I have altered the short written version of the getter and setter (get; set;), to this:
public DataAreaEnum _DataArea;
public DataAreaEnum DataArea
{
get
{
return _DataArea;
}
set
{
_DataArea = value;
Debugger.Break();
}
}
By doing this, I had hoped that the debugger would break when the value was being set, however this is never hit. I am stuck and do not know how to think about this. Are there any other ways this value could have been set? How can I found out where this value was being set, if the breakpoint in the setter is never hit?
The complete class code:
using Works.Common.Models;
using System.Collections.Generic;
using System.Diagnostics;
namespace Works.Models.Toolsheets
{
public partial class Toolblad : ModelBase
{
public Toolblad()
{
Bijlagen = new HashSet<Bijlage>();
//Carrousels = new HashSet<Carrousel>();
KlauwplaatSlijpers = new HashSet<KlauwplaatSlijper>();
Klauwplaten = new HashSet<Klauwplaat>();
Laadtafels = new HashSet<Laadtafel>();
Laders = new HashSet<Lader>();
MblStangenLaders = new HashSet<MblStangenLader>();
MblStangenLaderWisselpunten = new HashSet<MblStangenLaderWisselpunt>();
Shuttles = new HashSet<Shuttle>();
Spankoppen = new HashSet<Spankop>();
SpankopSlijpers = new HashSet<SpankopSlijper>();
Revolvers = new HashSet<ToolbladRevolver>();
}
public DataAreaEnum _DataArea;
public DataAreaEnum DataArea
{
get
{
return _DataArea;
}
set
{
_DataArea = value;
Debugger.Break();
}
}
public bool IsTemplate { get; set; }
#region Navigation Properties
public virtual AlgemeneInfo AlgemeneInfo { get; set; }
public int AlgemeneInfoId { get; set; }
public ICollection<Bijlage> Bijlagen { get; set; }
//public ICollection<Carrousel> Carrousels { get; set; }
public ICollection<KlauwplaatSlijper> KlauwplaatSlijpers { get; set; }
public ICollection<Klauwplaat> Klauwplaten { get; set; }
public ICollection<Laadtafel> Laadtafels { get; set; }
public ICollection<Lader> Laders { get; set; }
public MachineGroep MachineGroep { get; set; }
public int MachineGroepId { get; set; }
public ICollection<MblStangenLader> MblStangenLaders { get; set; }
public ICollection<MblStangenLaderWisselpunt> MblStangenLaderWisselpunten { get; set; }
public ICollection<ToolbladRevolver> Revolvers { get; set; }
public ICollection<Shuttle> Shuttles { get; set; }
public ICollection<Spankop> Spankoppen { get; set; }
public ICollection<SpankopSlijper> SpankopSlijpers { get; set; }
#endregion Navigation Properties
}
}
I note that Toolblad is a partial class. Is there any code elsewhere changing _DataArea? Try renaming it to something else (e.g. _dataArea) just in that code file and see if it all still compiles.
Doing that will catch any unexpected code that tries to access it.
Whether you set the value of a Enum or not, it defaults to 0 which is why you are seeing a value for it without setting one.

How to populate object with another object with almost same structure? [duplicate]

This question already has answers here:
copy chosen properties to object of other type
(2 answers)
How to cast between 2 types of the same name and internal sturcture but from different assemblies?
(5 answers)
Copy two identical object with different namespaces (recursive reflection)
(7 answers)
Closed 2 years ago.
I'm trying to find the best way to populate my constructor with another constructor that have almost the same structure without setting each attribute,
So i have constructor Altridatiidentificativi in ModelRA constructor:
public class Altridatiidentificativi
{
public string denominazione { get; set; }
public string indirizzo { get; set; }
public string numeroCivico { get; set; }
public string cap { get; set; }
public string comune { get; set; }
public string provincia { get; set; }
public string nazione { get; set; }
public bool modificati { get; set; }
public string defAliquotaIVA { get; set; }
public bool nuovoUtente { get; set; }
}
And Altridatiidentificativi in Documenti:
public class Altridatiidentificativi
{
public bool nuovoUtente { get; set; }
public string denominazione { get; set; }
public string indirizzo { get; set; }
public string numeroCivico { get; set; }
public string cap { get; set; }
public string comune { get; set; }
public string provincia { get; set; }
public string nazione { get; set; }
}
As you can see the structure is almost the same, just constructor in ModelRA has this two extras modificati and defAliquotaIVA
So i was wondering if it's possible in some way to pass inside ModelRA.Altridatiidentificativi the Documenti.Altridatiidentificativi and then add the value to the extras
I was trying to do something like this :
public ModelRA initializeRA(Documento documento)
{
ModelRA model = new ModelRA();
model.altriDatiIdentificativi = <Altridatiidentificativi>(documento.altriDatiIdentificativi);
model.altriDatiIdentificativi.defAliquotaIVA = "";
model.altriDatiIdentificativi.modificati = false;
return model;
}
but i get error in <Altridatiidentificativi> "it's a type not a valid constructor in specific context"
Is there a way to reach what i'm trying to do or i have to set all the attributes manually?
Usually this pattern is a signal that there's a concept in your business model that needs to be abstracted into a composable pattern. The ModelRA.Altridatiidentificativi class could look like:
public class Altridatiidentificativi
{
public ModelRA.Altridatiidentificativi ModelRAAltridatiidentificativi { get; set; }
public bool modificati { get; set; }
public string defAliquotaIVA { get; set; }
}
Then your initialization code could look like this:
public ModelRA initializeRA(Documento documento)
{
ModelRA model = new ModelRA();
model.altriDatiIdentificativi.ModelRAAltridatiidentificativi = documento;
model.altriDatiIdentificativi.defAliquotaIVA = "";
model.altriDatiIdentificativi.modificati = false;
return model;
}
Tangentially I should mention that it's usually good practice to use property initializers and constructors unless you have a specific reason that you need initialization methods.
public class Altridatiidentificativi
{
public string ModelRA.Altridatiidentificativi ModelRAAltridatiidentificativi { get; set; }
public bool modificati { get; set; } = false; // unnecessary: this is default.
public string defAliquotaIVA { get; set; } = "";
public Altridatiidentificativi(ModelRA.Altridatiidentificativi modelRAAltridatiidentificativi)
{
this.modelRAAltridatiidentificativi = ModelRAAltridatiidentificativi;
}
}

Trying to work out these interfaces

I'm trying to create some interfaces. The IReportSection object will have one string and a collection of items, which could be different depending on what we're working with. Do I need to make it generic?
The IReport will have one string and a collection of IReportSection.
Here's how I'm trying to define it now.
public interface IReport
{
string ReportName { get; set; }
ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
string ReportSectionName { get; set; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection ReportItems { get; set; }
}
And my models:
pulic class ProjectSubmissionViewModel
{
public int ProjectSubmissionId { get; set; }
public string SubmissionTitle { get; set; }
}
pulic class AffiliateViewModel
{
public int AffiliateId { get; set; }
public string AffiliateName { get; set; }
}
This is how I'm trying to use it in code:
public class ChapterAffiliates : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Affiliates"; } }
public ICollection<AffiliateViewModel> ReportItems { get; set; }
}
public class ChapterTitles : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Titles"; } }
public ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
}
public class SubmissionListViewModel : IReport
{
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public ICollection<AffiliateViewModel> Affiliates{ get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections
{
get
{
var affiliateSection = new ChapterAffiliates
{
ReportItems = Affiliates
};
var titleSection = new ChapterTitles
{
ReportItems = Submissions.Where(s => s.SubmissionTitle.Contains("SomePhrase")).ToList()
};
var sections = new List<IReportSection> { {subSection}, {titleSection} };
return sections;
}
}
}
I'm not sure how to best define this. I'm pretty sure I've done it before, but it's not coming to me.
Are the type parameters for TRType all the same within a certain report? E.g. will you have report sections with different report types in them?
If all types within a report are the same, the solution is relatively simple:
public interface IReport<T> { ... }
If this is not the case - you'll have to do something different, e.g:
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase<TRType> : IReportSection {
...
}
This allows you to put different underlying types in the ReportSections collection related to the report. You'll have to do some more work to get the exact information that you need out of each report section.

How do you handle NullReferences in EF when creating a new entity in runtime?

I have the following example schema:
public class CounterReading
{
public int CounterReadingId { get; set; }
public virtual Counter Counter { get; set; }
public DateTime Date { get; set; }
public decimal Reading { get; set; }
public CounterReading()
{
Date = DateTime.Now;
}
}
public class Counter
{
[Key, ForeignKey("Meter")]
public int CounterId { get; set; }
public virtual Meter Meter { get; set; }
public virtual ObservableCollection<CounterReading> Readings { get; set; }
[NotMapped]
public CounterReading CurrentReading
{
get
{
if(Readings.Count > 0)
{
return Readings.MaxBy(m => m.Reading);
}
return null;
}
}
}
public abstract class Meter
{
public int MeterId { get; set; }
public string EANNumber { get; set; }
public string MeterNumber { get; set; }
public virtual Premise Premise { get; set; }
}
public class WaterMeter : Meter
{
public virtual Counter Counter { get; set; }
public WaterMeter()
{
Counter = new Counter();
Counter.Readings = new ObservableCollection<CounterReading>();
}
}
And what doesn't work is that my WaterMeter does not have any CounterReadings when I load it from database. This is because I set my Counter in my constructor, to avoid NullReferencesExceptions when I create a new WaterMeter in runtime.
If I remove my WaterMeterconstructor, EF loads my readings just fine. But that means I have loads of NullReferences when using my application without reloading my data each and every time.
What's the best way to solve this?
Edit:
NRE:
Well firstly, there is a disjoint in the code you have in your question and the actual code you are executing (as per the image you added) which is why I couldn't see a problem.
However, from the image you posted it's clear that the problem is the fact that you have Readings as a private field - EF CodeFirst requires navigation properties to be marked as public virtual in order for them to be initialised.
To avoid null reference exception with collection, you should introduce local readonly collection field and initialize it empty collection.
public class Counter
{
private readonly ObservableCollection<CounterReading> readings = new ObservableCollection<CounterReading>();
public virtual ObservableCollection<CounterReading> Readings
{
get { return readings; }
set { readings = value; }
}
[Key, ForeignKey("Meter")]
public int CounterId { get; set; }
public virtual Meter Meter { get; set; }
[NotMapped]
public CounterReading CurrentReading
{
get
{
return Readings.MaxBy(m => m.Reading);
}
}
}
I've got one addition to Evgeny's answer. Instead of always creating a new instance of the collection , you can make it conditionally so it only creates a new instance when this property is actually called:
private readonly ObservableCollection<CounterReading> readings;
public virtual ObservableCollection<CounterReading> Readings
{
get
{
if(_readings == null)
{
_readings = new ObservableCollection<CounterReading>();
}
return readings;
}
set { readings = value; }
}
If you don't need to add logic to the getter and setter, why don't you consider using this:
public virtual ObservableCollection<CounterReading> Readings {get;set;}

Access Protected Members in a Subclass

Im a little stuck and after some searching i turn to you:
class StatusResponse
{
protected int _statusCode { get; set; }
protected string _statusMessage { get; set; }
public StatusResponse(string Response)
{
if (!String.IsNullOrEmpty(Response))
{
this._statusCode = int.Parse((Response.Split(' '))[0].Trim());
this._statusMessage = Response;
}
}
}
class GroupStatusResponse : StatusResponse
{
public int Count { get; private set; }
public int FirstArticle { get; private set; }
public int LastArticle { get; private set; }
public string Newsgroup { get; private set; }
public GroupStatusResponse(string Response) : base(Response)
{
string[] splitResponse = Response.Split(' ');
this.Count = int.Parse(splitResponse[1].Trim());
this.FirstArticle = int.Parse(splitResponse[2].Trim());
this.LastArticle = int.Parse(splitResponse[3].Trim());
this.Newsgroup = splitResponse[4].Trim();
}
Why cant i do this:
GroupStatusResponse resp = new GroupStatusResponse("211 1234 3000234 3002322 misc.test");
Console.Writeline(resp._statusCode);
using
Console.Writeline(resp._statusCode);
from outside the derived class is public, and not protected use.
However, you could add something like:
class GroupStatusResponse : StatusResponse
{
public int GetStatusCode()
{
return _statusCode;
}
}
which is completely valid use.
Moreover, if the scenario is that _statusCode should be allowed to read by anyone, but only the base class should be able to set it, you could change its definition to:
public string _statusMessage { get; private set; }
It's because _statusCode is protected. This means the field is inaccessible outside of the class.

Categories

Resources