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 dwMa开发者_开发知识库chineNumber)
{
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;
}
}
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.
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.
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.
精彩评论