Reputation: 75
Please take note that this is uncompleted code, but just facing some small issue, as i using a lot of c++ OOP concept. i might have some issue when trying to change from another platform.
I get error when compiled and stated nonstatic method/property error
using System;
public class People
{
string name;
int age;
int height;
public virtual void insertDetail(People stu)
{
Console.Write("Please enter name : ");
stu.name = Console.ReadLine();
Console.Write("Please enter age : ");
while(!int.TryParse(Console.ReadLine(), out stu.age))
{
Console.WriteLine("You enter characters! Please re-insert");
}
Console.Write("Please enter height: ");
while(!int.TryParse(Console.ReadLine(), out stu.height))
{
Console.WriteLine("You enter characters! Please re-insert");
}
}
}
public class Class : People
{
static People[] students = new People[5];
public override void insertDetail(People stu)
{
Console.WriteLine("==================================");
base.insertDetail(stu);
}
public static void Main(string[] args)
{
for (int i = 0; i < students.Length; i++)
{
students[i] = new People();
insertDetail(students[i]);
}
Console.ReadKey();
}
}
Upvotes: 1
Views: 382
Reputation: 16956
As said in comments, you need an instance to use instance method.
Create an instance for Class
inside Main
public class Class : People
{
static People[] students = new People[5];
public override void insertDetail(People stu)
{
Console.WriteLine("==================================");
base.insertDetail(stu);
}
public static void Main(string[] args)
{
Class c = new Class(); // this is required to access insertDetail
for (int i = 0; i < students.Length; i++)
{
students[i] = new People();
c.insertDetail(students[i]);
}
Console.ReadKey();
}
}
Check this Demo
Upvotes: 3
Reputation: 3880
You get that error when you make a static call to an instance method like Object.ToString() using the type name as the qualifier when you really need an instance.
Upvotes: 0
Reputation: 786
First of all never use Class
as your class name.
As for the error you need to give more information about what are you trying to do. You have to add static
modifier to your method:
public static void insertDetail(People stu)
Or if you want for it to be override than:
public virtual void insertDetail()
{
this.name = "Some name";
//...
}
Upvotes: -1