0

i have created a class and with that class i passed constructor and then i made that class abstract class, but when i want to get 1 attribute of the abstract class from the inherit class it showing some error can not take argument 0

public class Device1
{
    public int dwMachineNumber;
    public int dwBaudrate;
    public int dwCommPort;
    public string dwIPAddress;
    public int dwPort;
    public int dwPassWord;

    public Device1(int dwMachineNumber)
    {
        this.dwMachineNumber = dwMachineNumber;
    }

    public Device1(int dwMachineNumber, int dwBaudrate, int dwCommPort, string dwIPAddress, int dwPort, int dwPassWord)
    {
        this.dwMachineNumber = dwMachineNumber;
        this.dwBaudrate = dwBaudrate;
        this.dwCommPort = dwCommPort;
        this.dwIPAddress = dwIPAddress;
        this.dwPort = dwPort;
        this.dwPassWord = dwPassWord;
    }

}

public class EnableMachine : Device1
{
    public int Device_Busy; //if 0 busy and 1 not busy 

    public EnableMachine(int dwMachineNumber, int Device_Busy)
    {
        this.Device_Busy = Device_Busy;
        this.dwMachineNumber = dwMachineNumber;
    }
}
3
  • 1
    I have no idea what your question is? Commented May 26, 2011 at 9:19
  • 1
    public int Device_Busy; looks really strange. I don't see any abstract classes either... Commented May 26, 2011 at 9:19
  • it is just a attribute of a inherit class Commented May 26, 2011 at 9:21

3 Answers 3

4

This won't compile, because your EnableMachine constructor is effectively:

public EnableMachine(int dwMachineNumber, int Device_Busy)
    : base() // Call base class parameterless constructor
{
    this.Device_Busy = Device_Busy;
    this.dwMachineNumber = dwMachineNumber;
}

Now the base class doesn't have a parameterless constructor.

The code should be:

public EnableMachine(int dwMachineNumber, int Device_Busy)
    : base(dwMachineNumber)
{
    this.Device_Busy = Device_Busy;
}

As for the rest of the code: I would strongly encourage you not to use public fields, not to use underscores in public member names, not to use pseudo-Hungarian naming (e.g. the dw prefix), and to use more descriptive class names.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

public class EnableMachine : Device1
{
    public int Device_Busy; //if 0 busy and 1 not busy 

    public EnableMachine(int dwMachineNumber, int Device_Busy) 
        : base(dwMachineNumber)
    {
        this.Device_Busy = Device_Busy;
    }
}

EDIT:

When calling the constructor of a derived class, it also tries to call the constructor of the base class. Since you just had:

public EnableMachine(int dwMachineNumber, int Device_Busy)
{...}

it by default tries to call the parameterless constructor Device() but Device1 does not have a parameterless constructor; hence the error "..does not contain a method accepting 0 arguments".

You need to tell it to use the constructor accepting the dwMachineNumber argument by adding the line

: base(dwMachineNumber)

to your derived class's constructor. So, effectively, when you instantiate the derived class, it takes the dwMachineNumber argument and trunks it through to the base class's constructor.

1 Comment

@Mano: See my edited answer. Jon Skeet had the most clear answer, actually.
0

Make sure you call the base constructor in the derived class constructor:

public EnableMachine(int dwMachineNumber, int Device_Busy)
    : base(dwMachineNumber)
{
    this.Device_Busy = Device_Busy;
}

Notice that you no longer need to set the dwMachineNumber in the derived constructor as this is done in the base class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.