Introduction
Inheritance is a concept of Object Oriented Programming. Inheritance is a mechanism of deriving a new class from a base class. A derived class can inherit the properties of a base class.
In this article you will see inheritance among a tables. In this article I make a parent table (Vehicles) and their child tables Car, Bus, Bike. The child table inherits the properties of the parent table.
Step 1 : First of all open Visual Studio LightSwitch->New project->Select LightSwitch application->Write name of project->Ok.
Step 2 : Click on create new table.
Step 3 : Create a parent table with the name Vehicles. Go to properties of type->Click on choice list->Add value->ok.
Step 4 : Now we will create child tables like Bike, Car, Bus.
Step 5 : Now we will make a relationship among these tables. Like as in the following images.
Step 6 : When we establish relationships among these tables then the vehicles tables will look like as in the following image.
Step 7 : Now we will add a screen. Right-click on screens-Add screen.
Step 8 : Select New Data Screen->Select screen data (Vehicles)->Ok.
Step 9 : Now we will a add details screen for Car, Bike, Bus.
Go to Solution Explorer->Right-click on screen->Add screen->Select Details screen->Change screen name->Unmark checkbox for default details screen->Ok.
In the same manner we will create a Bus details screen and a Bike details screen.
Step 10 : Now we will add a search screen. Go to Solution Explorer->Right click on screen->Add screen->Select search data screen->Select screen data (Vehicles)->Ok.
Step 11 : Now we will add a button. Right-click on Command bar->Add button.
Step 12 : Click on new method->Write name (edit)->Ok.
Step 13 : Expand edit->Select link.
Step 14 : Right click on edit button->Select edit execute code->Write code.
Code
- using System;
- using System.Linq;
- using System.IO;
- using System.IO.IsolatedStorage;
- using System.Collections.Generic;
- using Microsoft.LightSwitch;
- using Microsoft.LightSwitch.Framework.Client;
- using Microsoft.LightSwitch.Presentation;
- using Microsoft.LightSwitch.Presentation.Extensions;
- namespace LightSwitchApplication
- {
- public partial class SearchVehiclesSet
- {
- partial void Edit_Execute()
- {
-
- if (this.VehiclesSet.SelectedItem != null)
- {
- switch (this.VehiclesSet.SelectedItem.Type)
- {
- case "Car":
- this.Application.ShowCarDetail(this.VehiclesSet.SelectedItem.Id);
- break;
- case "Bus":
- this.Application.ShowBusDetail(this.VehiclesSet.SelectedItem.Id);
- break;
- case "Bike":
- this.Application.ShowBikeDetail(this.VehiclesSet.SelectedItem.Id);
- break;
- }
- }
- }
- }
- }
Step 15 : Go to CreateNewVehicles Screen->Click on write code->Select CreateNewVehicles_Saved->Write code.
Code
- using System;
- using System.Linq;
- using System.IO;
- using System.IO.IsolatedStorage;
- using System.Collections.Generic;
- using Microsoft.LightSwitch;
- using Microsoft.LightSwitch.Framework.Client;
- using Microsoft.LightSwitch.Presentation;
- using Microsoft.LightSwitch.Presentation.Extensions;
- namespace LightSwitchApplication
- {
- public partial class CreateNewVehicles
- {
- partial void CreateNewVehicles_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService>
- saveChangesTo)
- {
-
- this.VehiclesProperty = new Vehicles();
- }
- partial void CreateNewVehicles_Saved()
- {
-
- this.Close(false);
- Application.Current.ShowDefaultScreen(this.VehiclesProperty);
- switch (this.VehiclesProperty.Type) {
- case "Car":
- this.Application.ShowCarDetail(this.VehiclesProperty.Id);
- break;
- case "Bus":
- this.Application.ShowBusDetail(this.VehiclesProperty.Id);
- break;
- case "Bike":
- this.Application.ShowBikeDetail(this.VehiclesProperty.Id);
- break;
- }
- }
- }
Step 16 : Go to vehicles tables->Click on write code->Select VehiclesSet_Inserting->Write code.
Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.LightSwitch;
- using Microsoft.LightSwitch.Security.Server;
- namespace LightSwitchApplication
- {
- public partial class ApplicationDataService
- {
- partial void VehiclesSet_Inserting(Vehicles entity)
- {
- switch (entity.Type)
- {
- case "Car":
- if (entity.Car == null)
- {
- entity.Car = new Car();
- }
- break;
- case "Bus":
- if (entity.Bus == null)
- {
- entity.Bus = new Bus();
- }
- break;
- case "Bike":
- if (entity.Bike == null)
- {
- entity.Bike = new Bike();
- }
- break;
- }
- }
- }
- }
Step 17 : Run application (Press F5). Click on create new vehicles->Fill data->Save.
When we have filled data in the new vehicles then the search vehicles looks like in the following image.
Step 18 : Go to right corner of application->Click on design screen->Expand vehicles set->Select list->Save
Step 19 : Now we can edit any vehicles set. Click on edit button for edit any vehicles set.
Conclusion
So in this article you saw how to apply the inheritance concept to tables. Inheritance is a primary concept of the object oriented approach.
Some Helpful Resources