Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add a property to another class from different class file in C#,asp.net ?
I have two classes,
1. class1.cs
2. class2.cs
Now , I need to add a new property to a class1.cs from class2.cs in asp.net MVC...
Posted
Comments
Maciej Los 25-Mar-13 3:45am    
Please, be more specific and provide more details. Show example code. Does these classes are inheritable?

1 solution

If i understand you well, you need to read about inheritance[^], polimorphism[^].

Have a look at below example. There are 2 classes: Human and Student, where Student inherits from Human. Both classes have Name property and additionaly Student class have Class property.
C#
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Human h = new Human();
            Student s = new Student();

            Console.WriteLine(h.Name);
            Console.WriteLine(s.Name + " class: " + s.Class);
            Console.ReadKey();
        }
    }

    class Human
    {
        private string sName = "Human";

        public virtual string  Name
        {
            get {return sName;}
            set {sName = value;}
        }
    }

    class Student : Human 
    {
        private string sName = "Student";
        private string sClass = "A";

        public override string Name
        { 
            get {return base.Name + "->" + sName;}
            set {sName = value;}
        }

        public string Class
        {
            get { return sClass; }
            set { sName = value; }
        }
    
    }
}
 
Share this answer
 
Comments
kalaiselvant 25-Mar-13 5:01am    
Thanks Maciej Los, but i am not expecting this.
Actually my requirement is , I want to add a new attribute to the base class from its derived class.

example I have a class A:
public class A
{
public string Name{get;set;}
public string Age{get;set;}
}

and class 'B' doesnot knw abt 'A' but it s derived from A:
public class B:A
{
---
---
}

Now I want to add a new attribute/property in Class 'A'(not in B) by its object, is it possible ?
Maciej Los 25-Mar-13 5:12am    
You can add new attributes/properties to base class.
Did you read about inheritance adn polimorphism?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900