ghie
ghie

Reputation: 587

How to use Abstract Class in C#

I have a API and it use a abstract class.

I not familiar with abstract class and I also researched about it.

So far this is my code:

[DataContract]
public abstract class Questions_Base
{
 public void Import(Oasis OasisSource);
}

  public class Questions : Questions_Base
   {
            public  Questions()
            {
                //
                // TODO: Add constructor logic here
                //
                }


    public void Import(Oasis OasisSource) {

        string B1String;
        while ((B1String = OasisFile.ReadLine()) != null)
        {
            Questions oQuestions = new Questions();
            Oasis oOasis = new Oasis();
            oOasis.B1 = B1String;
            oQuestions.Import(oOasis); //error here Object reference not set to an instance of an object.
         }
      }
}

Please Advice me.. thanks!

Upvotes: 1

Views: 4573

Answers (5)

aeval
aeval

Reputation: 11

The abstract modifier can be used with classes, methods, properties, indexers, and events.

Upvotes: 1

hoodaticus
hoodaticus

Reputation: 3880

Your exception is caused by something in the body of the Questions.Import method. Which you have not provided us.

Upvotes: 1

jaywayco
jaywayco

Reputation: 6296

First up:

public void Import(Oasis OasisSource);

Should be

public abstract void Import(Oasis OasisSource);

If you want all children of this abstract class to implement there own Import functionality, otherwise implement the functionality in the base abstract class, mark it as virtual so the children can override if necessary.

The error is because you have not implemented this function correctly.

Upvotes: 1

hungryMind
hungryMind

Reputation: 7009

abstract classes are used to provide common functionality to child classes and force child to have own implementation of abstract members. it cannot be initialized, so individually it is not an object, but takes part in behaviour of child class

Upvotes: 2

Schroedingers Cat
Schroedingers Cat

Reputation: 3139

It is correct - you cannot create an instance of an abstract class. You need to define an class that inherits from your abstract class, and create the objects of this.

An abstract class is only for inheriting from. Nothing else.

Basically.

Upvotes: 1

Related Questions