Home > database >  Sharing properties and methods across classes in winforms
Sharing properties and methods across classes in winforms

Time:08-25

I have an interface class as such.

internal interface Venue
{
    int VenueID { get; set; }
    String VenueName { get; set; }
    String VenueAddress1 { get; set; }
    String VenueAddress2 { get; set; }
    String City { get; set; }
    String County { get; set; }
    String Eircode { get; set; }
    int Capacity { get; set; }
    String PhoneNo { get; set; }
    int Fee { get; set; }
    String Status { get; set; }
}

I then have two forms that implement this interface, each with its own methods to get data from form controls.

Both forms then are made children a TabPage, 1 form is an Update form, the other is an Add form.

Currently, I can only access the properties via each form type, e.g

frmAddVenue addVenue = new frmAddVenue();
frmUpdateVenue updateVenue = new frmUpdateVenue();

// Accessing form properties
addVenue.ID
updateVenue.ID

But I would actually like to do something more like this

// 
Form venueDetails = new frmAddVenue(); // or new frmUpdateVenue();
venueDetails.ID;

Doing something like this would allow me to access each child forms properties which are the exact same and contain the exact same information.

I was going to create a class in each form that inherited from a VenueModel class, but when I went to create overriding methods inside it told me I can't access the forms controls, I also presume this is something to do with form controls not being static.

What would I have to do to achieve such a thing?

CodePudding user response:

assuming ID is actually VenueID in your interface and that the forms implement that interface

frmAddVenue addVenue = new frmAddVenue();
var id = ((Venue)frmAddVenue).VenueID;

ie cast the form to the interace you want to use

  • Related