Ask about iherite

  • Thread starter Thread starter Nguyen Thanh Danh
  • Start date Start date
N

Nguyen Thanh Danh

i want to inherit class Socket in System.Nets.Sockets, but when i use this code:

public class NewSocket : System.Net.Sockets.Socket{
public NewSocket(){
}
}

and when i build it warning : No overload for method 'Socket' takes '0' arguments. Anyone please help me. Can U show me the way to inherit Socket class because request of my project is rewrite Socket with some new function. Thanks alot!
 
The signature of the public Socket..ctor is:
public Socket(AddressFamily addressFamily, SocketType socketType,
ProtocolType protocolType)

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
i want to inherit class Socket in System.Nets.Sockets, but when i use this
code:

public class NewSocket : System.Net.Sockets.Socket{
public NewSocket(){
}
}

and when i build it warning : No overload for method 'Socket' takes '0'
arguments. Anyone please help me. Can U show me the way to inherit Socket
class because request of my project is rewrite Socket with some new
function. Thanks alot!
 
Nguyen Thanh Danh said:
i want to inherit class Socket in System.Nets.Sockets, but when i use this code:

public class NewSocket : System.Net.Sockets.Socket{
public NewSocket(){
}
}

and when i build it warning : No overload for method 'Socket' takes '0' arguments. Anyone please help me. Can U show me the way to inherit Socket class because request of my project is rewrite Socket with some new function. Thanks alot!

You have to call the superclass constructor is what I think the other guy is
getting at. The way you have it written the compiler is trying to find a
default constructor for the Socket class. 'No constructor for socket takes 0
arguments' so your constructor cannot construct the base class. The syntax
escapes me right now (auto completion makes you so lazy!) but in Java you
would do

public MySocket
{
super(addressFamily, socketType, protocolType);
...

in C++ I think it was

public NewSocket() : Socket(addressFamily, socketType, protocolType)
{
...

You can pass the arguments from your constructor to the base class
constructor. I can't believe I can't remember the syntax, but hopefully I
have put you on the right track and someone will come along shortly to close
the thread. :-)

Aaron.
 
Aaron said:
You have to call the superclass constructor is what I think the other
guy is getting at. The way you have it written the compiler is trying
to find a default constructor for the Socket class. 'No constructor
for socket takes 0 arguments' so your constructor cannot construct
the base class. The syntax escapes me right now (auto completion
makes you so lazy!) but in Java you would do

public MySocket
{
super(addressFamily, socketType, protocolType);
...

in C++ I think it was

public NewSocket() : Socket(addressFamily, socketType, protocolType)
{
...

You can pass the arguments from your constructor to the base class
constructor. I can't believe I can't remember the syntax, but
hopefully I have put you on the right track and someone will come
along shortly to close the thread. :-)

Aaron.

in C# :

public MySocket(<params>): base(addressFamily, socketType, protocolType)
{
}

... which is a problem if you want to *calculate* the values to be passed,
instead of just passing the parameters to your own constructor + constants.
Possibly you can use functions here to calculate the values you really want.

Hans Kesting
 
thanks all of u, but i can't do it myselt, i try all way like u said but it
still warning(another warning). Can u give me a code that can inherit class
Socket without nay warning?
 
To achieve what you want, you have to provide more information, such as what
the Socket is supposed to be used for.

When you inherit from Socket, you have to call the constructor of the
super-class with valid arguments. There is a large list of constants you can
use there, but which ones to use is depending on what you'll be using it
for.

One simple example that compiles without warnings:

using System.Net.Sockets;

public class MySocket : Socket
{
public MySocket( ) :
base(
AddressFamily.InterNetwork,
SocketType.Unknown,
ProtocolType.Tcp)
{
}
}

I suggest you look into the different constants of AddressFamily, SocketType
and ProtocolType, to decide which ones are best suited for your project.

If it's just added functionality you're after, you don't have to use the
*empty* constructor at all. To create an instance of Socket you have to
provide three arguments, whick you can do with the sub-class as well. This
example will also work:

using System.Net.Sockets;

public class MySocket : Socket
{
public MySocket(
AddressFamily a,
SocketType s,
ProtocolType p ) : base(a, s, p)
{
}
}

// Bjorn A
 
Thanks for ur help!

Bjorn Abelli said:
To achieve what you want, you have to provide more information, such as what
the Socket is supposed to be used for.

When you inherit from Socket, you have to call the constructor of the
super-class with valid arguments. There is a large list of constants you can
use there, but which ones to use is depending on what you'll be using it
for.

One simple example that compiles without warnings:

using System.Net.Sockets;

public class MySocket : Socket
{
public MySocket( ) :
base(
AddressFamily.InterNetwork,
SocketType.Unknown,
ProtocolType.Tcp)
{
}
}

I suggest you look into the different constants of AddressFamily, SocketType
and ProtocolType, to decide which ones are best suited for your project.

If it's just added functionality you're after, you don't have to use the
*empty* constructor at all. To create an instance of Socket you have to
provide three arguments, whick you can do with the sub-class as well. This
example will also work:

using System.Net.Sockets;

public class MySocket : Socket
{
public MySocket(
AddressFamily a,
SocketType s,
ProtocolType p ) : base(a, s, p)
{
}
}

// Bjorn A
 
Back
Top