Inheritance in Visual Studio LightSwitch 2011

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.

first.png

Step 2 : Click on create new table.

create.png

Step 3 : Create a parent table with the name Vehicles. Go to properties of type->Click on choice list->Add value->ok.

1.png

Step 4 : Now we will create child tables like Bike, Car, Bus.

image3.png
image4.png
lats.png

Step 5 : Now we will make a relationship among these tables. Like as in the following images.

2.png

3.png

4.png

 

Step 6 : When we establish relationships among these tables then the vehicles tables will look like as in the following image.

5.png

Step 7 : Now we will add a screen. Right-click on screens-Add screen.

6.png

Step 8 : Select New Data Screen->Select screen data (Vehicles)->Ok.

7.png

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.

8.png

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.

9.png

Step 11 : Now we will add a button. Right-click on Command bar->Add button.

10.png

Step 12 : Click on new method->Write name (edit)->Ok.

11.png

Step 13 : Expand edit->Select link.

12.png

Step 14 : Right click on edit button->Select edit execute code->Write code.

13.png

Code

  1. using System;  
  2. using System.Linq;  
  3. using System.IO;  
  4. using System.IO.IsolatedStorage;  
  5. using System.Collections.Generic;  
  6. using Microsoft.LightSwitch;  
  7. using Microsoft.LightSwitch.Framework.Client;  
  8. using Microsoft.LightSwitch.Presentation;  
  9. using Microsoft.LightSwitch.Presentation.Extensions;  
  10. namespace LightSwitchApplication  
  11. {  
  12.     public partial class SearchVehiclesSet  
  13.     {  
  14.         partial void Edit_Execute()  
  15.         {  
  16.             // Write your code here.\  
  17.             if (this.VehiclesSet.SelectedItem != null)  
  18.             {  
  19.                 switch (this.VehiclesSet.SelectedItem.Type)  
  20.                 {  
  21.                     case "Car":  
  22.                         this.Application.ShowCarDetail(this.VehiclesSet.SelectedItem.Id);  
  23.                         break;  
  24.                     case "Bus":  
  25.                         this.Application.ShowBusDetail(this.VehiclesSet.SelectedItem.Id);  
  26.                         break;  
  27.                     case "Bike":  
  28.                         this.Application.ShowBikeDetail(this.VehiclesSet.SelectedItem.Id);  
  29.                         break;  
  30.                 }  
  31.              }  
  32.          }  
  33.     }  

Step 15 : Go to CreateNewVehicles Screen->Click on write code->Select CreateNewVehicles_Saved->Write code.

14.png

Code

  1. using System;  
  2. using System.Linq;  
  3. using System.IO;  
  4. using System.IO.IsolatedStorage;  
  5. using System.Collections.Generic;  
  6. using Microsoft.LightSwitch;  
  7. using Microsoft.LightSwitch.Framework.Client;  
  8. using Microsoft.LightSwitch.Presentation;  
  9. using Microsoft.LightSwitch.Presentation.Extensions;  
  10. namespace LightSwitchApplication  
  11. {  
  12.     public partial class CreateNewVehicles  
  13.     {  
  14.         partial void CreateNewVehicles_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService>  
  15. saveChangesTo)  
  16.         {  
  17.             // Write your code here.  
  18.             this.VehiclesProperty = new Vehicles();  
  19.         }  
  20.         partial void CreateNewVehicles_Saved()  
  21.         {  
  22.             // Write your code here.  
  23.             this.Close(false);  
  24.             Application.Current.ShowDefaultScreen(this.VehiclesProperty);  
  25.             switch (this.VehiclesProperty.Type) {  
  26.                    case "Car":  
  27.                              this.Application.ShowCarDetail(this.VehiclesProperty.Id);  
  28.                              break;  
  29.                    case "Bus":  
  30.                              this.Application.ShowBusDetail(this.VehiclesProperty.Id);  
  31.                              break;  
  32.                    case "Bike":  
  33.                              this.Application.ShowBikeDetail(this.VehiclesProperty.Id);  
  34.                              break;  
  35.         }  
  36.     }  

Step 16 : Go to vehicles tables->Click on write code->Select VehiclesSet_Inserting->Write code.

15.png

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Microsoft.LightSwitch;  
  6. using Microsoft.LightSwitch.Security.Server;  
  7. namespace LightSwitchApplication  
  8. {  
  9.     public partial class ApplicationDataService  
  10.     {  
  11.         partial void VehiclesSet_Inserting(Vehicles entity)  
  12.         {  
  13.             switch (entity.Type)  
  14.             {  
  15.                 case "Car":  
  16.                     if (entity.Car == null)  
  17.                     {  
  18.                         entity.Car = new Car();  
  19.                     }  
  20.                     break;  
  21.                 case "Bus":  
  22.                     if (entity.Bus == null)  
  23.                     {  
  24.                         entity.Bus = new Bus();  
  25.                     }  
  26.                     break;  
  27.                 case "Bike":  
  28.                     if (entity.Bike == null)  
  29.                     {  
  30.                         entity.Bike = new Bike();  
  31.                     }  
  32.                     break;  
  33.             }  
  34.         }  
  35.     }  

Step 17 : Run application (Press F5). Click on create new vehicles->Fill data->Save.

16.png

When we have filled data in the new vehicles then the search vehicles looks like in the following image.

17.png

Step 18 : Go to right corner of application->Click on design screen->Expand vehicles set->Select list->Save

18.png

Step 19 : Now we can edit any vehicles set. Click on edit button for edit any vehicles set.

19.png

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


Similar Articles