Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Nested Structures
By Saju

Nested Structure with method overloading ,method overriding and Interface Like C and C++ it is possible to declare a structure within other structure. The inner structure can inherit an interface. Method overloading and method overriding is also possible with nested structures. The following code explains the various implementations of Nested Structures. Here I declared a class examination as public with in structure student. The class contains a property 'property' to set the Examination.

//Explains the use of nested structure 
 //Explaining structure with method overriding and overloading
 //Also Explains the multiple inheritance with interface;

using System;

interface aaa
 {
    void show();
    void getdata(string x,string y);
 }


//********************************************************************

//Here bbb extends aaa

interface bbb : aaa
 {

  //Just an implementation of interface aaa

 }
//***************************************************************
 struct student
 {
   public string std_name;
   public string rollno;
   public static string grade;

   //Setting the property with struct student

   public string content
    {
      set
        {
          rollno="121212";
        }
      get
        {
          return rollno;
        }
    }

   //Declaring a class examination with in the structure student

   public class examination
    {
      public string exam_center_code;
      public string exam_center_name;

      //Setting property for exam_center_code

      public string property
       {
         set
           {
              exam_center_code=value;
           }
         get
           {
             return exam_center_name;
           }
       }
      public examination()
       {
         exam_center_code="Ex002";
         exam_center_name="St Mary's School TVM";
       }
      public void put_details(string str,string str2)
        {
          exam_center_code=str;
          exam_center_name=str2;
        }
      public void get_details()
        {
         Console.WriteLine("Examination Center Code {0} ",exam_center_code);
         Console.WriteLine("Examination Center Name {0} ",exam_center_name);  

        }
    }//End of inner class examination

   //declaring inner structure faculty
   //as public
   //Implementing the interface bbb


     public struct faculty :bbb
       {
         public string fcode;
         public string fname;

         //Overriding the function getdata()

         public void getdata(string st,string str)
           {
             grade="A-Grade"; //Possible to access a static
                             //member of parent struct
             fcode=st;
             fname=str;
           }

         public void show()
           {
             Console.WriteLine("Faculty Code {0} ",fcode);
             Console.WriteLine("Faculty Name {0} ",fname);
           }
       }

    //End of nested structure

   //Here method overloading applicable for the function
   //add() and add()

   public void add()
    {
       std_name="Saju";
       rollno="A001";
    }

   public void add(string aa,string bb)
    {

      //fcode="F0987"; cannot access inner struct member

      std_name=aa;
      rollno=bb;
    }

   public void show()
    {
      Console.WriteLine("Student Name  {0} ",std_name);
      Console.WriteLine("Roll Number   {0} ",rollno);
    }
};

//***************************************************************
class struct2
 {
    static void Main()
     {
       //creating an instance of student

       student xx=new student();
       xx.content="56678ADC";
       Console.WriteLine("Roll Number After Setting the Property {0}",xx.rollno);
       //Creating an instance of inner structure faculty
       

       student.faculty yy=new student.faculty();

       //Directly Refering the fields of inner structure faculty

       yy.fcode="F007";
       yy.fname="Vishnu Namboodiri";

       //Accessing the getdata() of inner structure faculty

       yy.getdata("S009","SivaKumar");

       //calling the show() of inner structure of faculty

       yy.show();

       //overriding the instance of student.faculty with the
       //instance of interface  bbb

       bbb temp=new student.faculty();

       //calling the getdata() of faculty using the interface
       //instance temp;

       temp.getdata("A009","AnilKumar");

       //calling the show() of faculty using the interface instance
       //temp

       temp.show();

       //Creating the instance of the nested class examination

       student.examination ee=new student.examination();
       ee.property="EXE2345";
       ee.get_details();
      
     }
 }
//End of Code

Output

Roll Number After Setting the Property 121212
Faculty Code S009 
Faculty Name SivaKumar 
Faculty Code A009 
Faculty Name AnilKumar 
Examination Center Code EXE2345 
Examination Center Name St Mary's School TVM