C
C#3mo ago
PANZER234425

Call method with updated data

Hello, im currently trying to make an update function. I got 2 lists:
public class AircraftData //stores the data for the startup
{
public string? Registration { get; set; }
public string? Type { get; set; }
public int Seats { get; set; }
public string? Engines { get; set; }
public string? Airline { get; set; }
public DateTime FirstFlight { get; set; }
public string? SpecialLivery { get; set; }
public byte[]? AircraftPicture { get; set; }
public bool Favorite { get; set; }
}

public class SpecificAircraftData
{
public string? Type { get; set; }
public int Seats { get; set; }
public string? Engines { get; set; }
public DateTime FirstFlight { get; set; }
public string? SpecialLivery { get; set; }
public bool Favorite { get; set; }
}
public class AircraftData //stores the data for the startup
{
public string? Registration { get; set; }
public string? Type { get; set; }
public int Seats { get; set; }
public string? Engines { get; set; }
public string? Airline { get; set; }
public DateTime FirstFlight { get; set; }
public string? SpecialLivery { get; set; }
public byte[]? AircraftPicture { get; set; }
public bool Favorite { get; set; }
}

public class SpecificAircraftData
{
public string? Type { get; set; }
public int Seats { get; set; }
public string? Engines { get; set; }
public DateTime FirstFlight { get; set; }
public string? SpecialLivery { get; set; }
public bool Favorite { get; set; }
}
The first list stores every aircraft, while the second one only stores variables that are updatable. If I exit the form a sql script runs which pulls the updated data in the second list, then It gets updated on the "overview" in a User Control, the user control has a method which calls up a more detailed form where I can update attributes. Problem: If I open the aircraftDetails (picAircraft_Click) Form the old data is getting showed because the method with the older data is given to it, now I had the idea to compare the two lists and if there is any difference then it should give the updated data and if not it should give the old (currentAircraft). You guys got any fix for that or an better solution I can use to update the Details form?
7 Replies
PANZER234425
PANZER234425OP3mo ago
AircraftField_UC.cs
public partial class AircraftField_UC : UserControl
{
public AircraftData currentAircraft;
public SpecificAircraftData currentSpecificAircraft;

public event EventHandler<AircraftData> AircraftClicked;


public AircraftField_UC()
{
InitializeComponent();
}


//Gets called on opening Aircrafts.cs
public void SetAircraftData(AircraftData aircraft)
{
if (aircraft != null)
{
currentAircraft = aircraft;

lblRegistration.Text = aircraft.Registration ?? "N/A";
lblType.Text = aircraft.Type ?? "N/A";
lblEngines.Text = aircraft.Engines ?? "N/A";
lblSeats.Text = aircraft.Seats.ToString();
lblAirline.Text = aircraft.Airline ?? "N/A";
lblFirstFlight.Text = aircraft.FirstFlight != DateTime.MinValue ? aircraft.FirstFlight.ToShortDateString() : "N/A";

if (aircraft.AircraftPicture != null)
{
using (var ms = new MemoryStream(aircraft.AircraftPicture))
{
picAircraft.Image = Image.FromStream(ms);
}
}
else
{
picAircraft.Image = null;
}

if (!string.IsNullOrEmpty(aircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = aircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}
}
else
{
MessageBox.Show("Aircraft data is null");
}
}
public partial class AircraftField_UC : UserControl
{
public AircraftData currentAircraft;
public SpecificAircraftData currentSpecificAircraft;

public event EventHandler<AircraftData> AircraftClicked;


public AircraftField_UC()
{
InitializeComponent();
}


//Gets called on opening Aircrafts.cs
public void SetAircraftData(AircraftData aircraft)
{
if (aircraft != null)
{
currentAircraft = aircraft;

lblRegistration.Text = aircraft.Registration ?? "N/A";
lblType.Text = aircraft.Type ?? "N/A";
lblEngines.Text = aircraft.Engines ?? "N/A";
lblSeats.Text = aircraft.Seats.ToString();
lblAirline.Text = aircraft.Airline ?? "N/A";
lblFirstFlight.Text = aircraft.FirstFlight != DateTime.MinValue ? aircraft.FirstFlight.ToShortDateString() : "N/A";

if (aircraft.AircraftPicture != null)
{
using (var ms = new MemoryStream(aircraft.AircraftPicture))
{
picAircraft.Image = Image.FromStream(ms);
}
}
else
{
picAircraft.Image = null;
}

if (!string.IsNullOrEmpty(aircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = aircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}
}
else
{
MessageBox.Show("Aircraft data is null");
}
}

//Updates USC after exit AircraftDetails
public void UpdateAircraft(SpecificAircraftData specificAircraft)
{
if (specificAircraft != null)
{
currentSpecificAircraft = specificAircraft;

lblType.Text = specificAircraft.Type ?? "N/A";
lblEngines.Text = specificAircraft.Engines ?? "N/A";
lblSeats.Text = specificAircraft.Seats.ToString();
lblFirstFlight.Text = specificAircraft.FirstFlight != DateTime.MinValue ? specificAircraft.FirstFlight.ToShortDateString() : "N/A";

if (!string.IsNullOrEmpty(specificAircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = specificAircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}
}
}

//public AircraftData CurrentAircraft { get; private set; }
//public SpecificAircraftData SpecificAircraftData { get; private set; }
private void picAircraft_Click(object sender, EventArgs e)
{
AircraftDetails aircraftDetails = new AircraftDetails();


aircraftDetails.LoadDetailsAircraft(currentAircraft);

// Abonniere das AircraftUpdated Event
aircraftDetails.AircraftUpdated += (s, updatedAircraft) =>
{
// Aktualisiere die Anzeige mit den neuen Daten
UpdateAircraft(updatedAircraft);


};

// Zeige das Detail-Formular modal an
aircraftDetails.ShowDialog();
}
}

//Updates USC after exit AircraftDetails
public void UpdateAircraft(SpecificAircraftData specificAircraft)
{
if (specificAircraft != null)
{
currentSpecificAircraft = specificAircraft;

lblType.Text = specificAircraft.Type ?? "N/A";
lblEngines.Text = specificAircraft.Engines ?? "N/A";
lblSeats.Text = specificAircraft.Seats.ToString();
lblFirstFlight.Text = specificAircraft.FirstFlight != DateTime.MinValue ? specificAircraft.FirstFlight.ToShortDateString() : "N/A";

if (!string.IsNullOrEmpty(specificAircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = specificAircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}
}
}

//public AircraftData CurrentAircraft { get; private set; }
//public SpecificAircraftData SpecificAircraftData { get; private set; }
private void picAircraft_Click(object sender, EventArgs e)
{
AircraftDetails aircraftDetails = new AircraftDetails();


aircraftDetails.LoadDetailsAircraft(currentAircraft);

// Abonniere das AircraftUpdated Event
aircraftDetails.AircraftUpdated += (s, updatedAircraft) =>
{
// Aktualisiere die Anzeige mit den neuen Daten
UpdateAircraft(updatedAircraft);


};

// Zeige das Detail-Formular modal an
aircraftDetails.ShowDialog();
}
}
Method that is being called in the details Page:
public void LoadDetailsAircraft(AircraftData aircraft, bool load = true)
{
if (aircraft != null && load)
{
currentAircraft = aircraft;

lblRegistration.Text = aircraft.Registration ?? "N/A";
lblType.Text = aircraft.Type ?? "N/A";
lblSeats.Text = aircraft.Seats.ToString();
lblEngines.Text = aircraft.Engines ?? "N/A";
lblFirstFlight.Text = aircraft.FirstFlight != DateTime.MinValue ? aircraft.FirstFlight.ToShortDateString() : "N/A";

//Main Picture
if (aircraft.AircraftPicture != null)
{
//Picture to byte Array
using (var ms = new MemoryStream(aircraft.AircraftPicture))
{
picMainAircraft.Image = Image.FromStream(ms);
}
}
else
{
picMainAircraft.Image = null;
}

//SpecialLivery
if (!string.IsNullOrEmpty(aircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = aircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}

//Favorite Button
if (aircraft.Favorite == false)
{
btnUnSetFavorite.Visible = false;
btnSetFavorite.Visible = true;
}
else
{
btnUnSetFavorite.Visible = true;
btnSetFavorite.Visible = false;
}
}
LoadDetailsAirline();
LoadPictures();
}
public void LoadDetailsAircraft(AircraftData aircraft, bool load = true)
{
if (aircraft != null && load)
{
currentAircraft = aircraft;

lblRegistration.Text = aircraft.Registration ?? "N/A";
lblType.Text = aircraft.Type ?? "N/A";
lblSeats.Text = aircraft.Seats.ToString();
lblEngines.Text = aircraft.Engines ?? "N/A";
lblFirstFlight.Text = aircraft.FirstFlight != DateTime.MinValue ? aircraft.FirstFlight.ToShortDateString() : "N/A";

//Main Picture
if (aircraft.AircraftPicture != null)
{
//Picture to byte Array
using (var ms = new MemoryStream(aircraft.AircraftPicture))
{
picMainAircraft.Image = Image.FromStream(ms);
}
}
else
{
picMainAircraft.Image = null;
}

//SpecialLivery
if (!string.IsNullOrEmpty(aircraft.SpecialLivery))
{
lblSpecialLiveryTag.Visible = true;
lblSpecialLivery.Visible = true;
lblSpecialLivery.Text = aircraft.SpecialLivery;
}
else
{
lblSpecialLiveryTag.Visible = false;
lblSpecialLivery.Visible = false;
}

//Favorite Button
if (aircraft.Favorite == false)
{
btnUnSetFavorite.Visible = false;
btnSetFavorite.Visible = true;
}
else
{
btnUnSetFavorite.Visible = true;
btnSetFavorite.Visible = false;
}
}
LoadDetailsAirline();
LoadPictures();
}
HtmlCompiler
HtmlCompiler3mo ago
i don't understand this part
the old data is getting showed because the method with the older data is given to it
why do you have to use the old data?
PANZER234425
PANZER234425OP3mo ago
Basically I can Update Data in The AircraftDeatails.cs Form, Like eg. Change Type or seats. Then I click on save, which Uploads The Changed Data to The Database. If I press The Exit Button in The Details Form it pulls The new Data from the Database and puts it in The AircraftField_UC.cs User Control, which is Then showing The updated Data. If I click on The AircraftPicture in The UC an Event triggers to Open The Details Page, but if It opens the old Data is givien to The Details Page
HtmlCompiler
HtmlCompiler3mo ago
so the event of having updated the data should go to the other component to update its data
PANZER234425
PANZER234425OP3mo ago
No, both data needs to be updated In the userControl, which is an overview its updating, but then it also needs to be updated in the details page
HtmlCompiler
HtmlCompiler3mo ago
yes, but the point is the architecture of the program: you said you have cached/duplicated data somewhere, and you have also a form that updates the real source of the data, the database so you need to host that cached data in a service that is aware of the fact that something else has updated the real data, ergo you need an event (or a maybe even a less detached mechanism) from the class that is updating the db to who is listening for updates of that data
PANZER234425
PANZER234425OP3mo ago
I fixed it with these lines: currentAircraft.Type = specificAircraft.Type; currentAircraft.Engines = specificAircraft.Engines; currentAircraft.Seats = specificAircraft.Seats; currentAircraft.FirstFlight = specificAircraft.FirstFlight; currentAircraft.SpecialLivery = specificAircraft.SpecialLivery; currentAircraft.Favorite = specificAircraft.Favorite; Thanks
Want results from more Discord servers?
Add your server