Inheritance question

  • Thread starter Thread starter Taylor
  • Start date Start date
T

Taylor

I'm trying to understand inheritance. I'd like to make my own type of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
try change
private IPAddress ip; to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


Taylor said:
I'm trying to understand inheritance. I'd like to make my own type of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


Michael Culley said:
try change
private IPAddress ip; to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own type of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Ah, now I see the problem. The Parse function still returns an IPAddress, not a myIp. You need to add this code to your myIp class.

public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Taylor said:
Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


Michael Culley said:
try change
private IPAddress ip; to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own type of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much richer
now! I will study it further. You can tell I've spent some time staring at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Michael Culley said:
Ah, now I see the problem. The Parse function still returns an IPAddress,
not a myIp. You need to add this code to your myIp class.
public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


Michael Culley said:
try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own type of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and assigning that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the same
IP.

(did that make any sense?)


Good luck!
Brian W









Taylor said:
Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much richer
now! I will study it further. You can tell I've spent some time staring at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Michael Culley said:
Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.
public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Good point. Hmmmm.... I'm trying to get this right... This shouldn't be so
hard, but I'm just not getting the right combination. This next attempt
doesn't work either because the an implicit conversion from a myIp to a
System.Net.IPAddress is not possible. I've tried to make an explicit
conversion, but I can't seem to get it right.


namespace test
{
using System;
using System.Net;

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;
public myIp(long l)
: base(l)
{

}
}

class TestMyIP
{
[STAThread]
static void Main(string[] args)
{
myIp ip = myIp.Parse("127.0.0.1");

Console.WriteLine(ip.ToString());


}
}
}







Brian W said:
Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and assigning that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the same
IP.

(did that make any sense?)


Good luck!
Brian W









Taylor said:
Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Michael Culley said:
Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.
public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find that I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




Brian W said:
Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and assigning that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the same
IP.

(did that make any sense?)


Good luck!
Brian W









Taylor said:
Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Michael Culley said:
Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.
public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when I supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Well, firstly, IPAddress isn't a static class. Parse is a static method and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






Taylor said:
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find that I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




Brian W said:
Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and assigning that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the same
IP.

(did that make any sense?)


Good luck!
Brian W









Taylor said:
Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Ah, now I see the problem. The Parse function still returns an IPAddress,
not a myIp. You need to add this code to your myIp class.

public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when I
supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Hi Brian,

I tried your code, but it produced an InvalidCastException.

Taylor


Brian W said:
Well, firstly, IPAddress isn't a static class. Parse is a static method and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






Taylor said:
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find that I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




the
same
when
I
supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert
type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Brian,

Here is the pattern you're suggesting

Given

class B{}
class D:B{}
class C{

public static void Main(string[] args)
{
D d = (D)(new B());
}

}

which is a widening conversion, which I thought to be invalid. Certainly

B b = (D)(new B())

is valid cause base class references can refer to derived class instances
otherwise we wouldn't have polymorphism, right?

Well, I guess my problem is that I don't understand how to make the derived
class's "new" operator return a derived class instance. Error code CS0029
suggests that we do something like

public static implicit operator new(...

but that doesn't seem to work either.


Hmmmm...


I like Brian's reasoning, and he has a point as to how using Micahel's
solution we create too many IPAddress's, but I can't seem to pull these two
ideas together...


Taylor



Brian W said:
Well, firstly, IPAddress isn't a static class. Parse is a static method and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






Taylor said:
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find that I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




the
same
when
I
supply
the full name for IPAddress. Here is what I have now, but same error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert
type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Oops! I also made a widening conversion.

D d = (D)(new B())

will not work! I meant to say

B b = d

which is an implicit conversion from a derived class instance to a base
class reference. Hence polymorphism...Right?



Taylor said:
Brian,

Here is the pattern you're suggesting

Given

class B{}
class D:B{}
class C{

public static void Main(string[] args)
{
D d = (D)(new B());
}

}

which is a widening conversion, which I thought to be invalid. Certainly

B b = (D)(new B())

is valid cause base class references can refer to derived class instances
otherwise we wouldn't have polymorphism, right?

Well, I guess my problem is that I don't understand how to make the derived
class's "new" operator return a derived class instance. Error code CS0029
suggests that we do something like

public static implicit operator new(...

but that doesn't seem to work either.


Hmmmm...


I like Brian's reasoning, and he has a point as to how using Micahel's
solution we create too many IPAddress's, but I can't seem to pull these two
ideas together...


Taylor



Brian W said:
Well, firstly, IPAddress isn't a static class. Parse is a static method and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






Taylor said:
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find
that
I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and
assigning
that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the
same
IP.

(did that make any sense?)


Good luck!
Brian W









Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much
richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.

public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs
when
I
supply
the full name for IPAddress. Here is what I have now, but same
error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert
type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Right you are, Taylor, My Bad!. That's what I get for writing-compiling and
not running. ;-) You can't cast it that way.

You will have to write your own static Parse method to using the new syntax
as Michael suggested to return myIp. But the rest of the class and example
is still valid.

I would still incourage you to get your hands on some books on the subject
of OO design/programming


Good luck
Brian W


Taylor said:
Hi Brian,

I tried your code, but it produced an InvalidCastException.

Taylor


Brian W said:
Well, firstly, IPAddress isn't a static class. Parse is a static method and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






Taylor said:
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find
that
I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class similar to
what they're talking about in the above url?

Taylor




Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and
assigning
that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that represent the
same
IP.

(did that make any sense?)


Good luck!
Brian W









Hi Michael,

You've have made me a very happy camper! Thank you, Thank you! Finally
I've entered the OO world, this was my first inheritance. I'm much
richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.

public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs
when
I
supply
the full name for IPAddress. Here is what I have now, but same
error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make my own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert
type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Brian W said:
Right you are, Taylor, My Bad!. That's what I get for writing-compiling and
not running. ;-) You can't cast it that way.

You will have to write your own static Parse method to using the new syntax
as Michael suggested to return myIp. But the rest of the class and example
is still valid.

I would still incourage you to get your hands on some books on the subject
of OO design/programming


Good luck
Brian W


Taylor said:
Hi Brian,

I tried your code, but it produced an InvalidCastException.

Taylor


Brian W said:
Well, firstly, IPAddress isn't a static class. Parse is a static
method
and
this

Perhaps you need to pick up a book on OO design and/or programming. Reading
such a book is certainly better, not to mention more productive, than just
jumping in head first.

In addition, you may want to try something simpler.

In this case if you really want to derive from IPAddress the following
should be what you are looking for. Note that myIp has not member of type
IPAddress. Also note the cast from IPAddress to myIp.

In this specific example there really is not reason for this constructor.
the default constructor would suffice.

namespace ConsoleApplication1
{

class myIp : System.Net.IPAddress
{

public myIp(long l) : base(l)
{
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
///


class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// Just cast it.
myIp ip = (myIp)myIp.Parse("127.0.0.1");

//
// TODO: Add code to start application here
//
}
}
}






I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247reference/msgs/25/126903.aspx


<quote>
The problem with all-static classes is that when you
derive from them, you can't override or 'new' the methods.

If you cast DerivedClass to BaseClass and call
MethodOne(), it'll call BaseClass.MethodOne() instead of
DerivedClass.MethodOne(), even though it's really
a DerivedClass to begin with.
</quote>

Going on this advice I'm reduced to only being able to create a BaseClass
IPAddress object, right? Going off advice in the above url I find
that
I
can do this:

IPAddress ip = myIp.Parse("127.0.0.1");

but does that mean that I'm reduced to never bing able to create an
instance
of myIp? I can only then call static methods?

Need I go searching on how to create an abstract factory class
similar
to
what they're talking about in the above url?

Taylor




Sure, you can get this to work, but, in this situation, more importantly
why?

You have myIp that derive from IPAddress.

myIp has a field (member) of type IPAddress

you are constructing an instance of myIp by passing l

you are then creating and instance of IPAddress using l and assigning
that
to the member ip.

since myIp _IS_ a IPAddress you now have 2 instances that
represent
the
same
IP.

(did that make any sense?)


Good luck!
Brian W









Hi Michael,

You've have made me a very happy camper! Thank you, Thank you!
Finally
I've entered the OO world, this was my first inheritance. I'm much
richer
now! I will study it further. You can tell I've spent some time
staring
at
this one by how incredibly pleased I am!

I really appreciate your sticking with me on this one!

Thanks Michael,
Taylor





Ah, now I see the problem. The Parse function still returns an
IPAddress,
not a myIp. You need to add this code to your myIp class.

public static new myIp Parse(string ipString)
{
myIp x = new myIp(0);
x.ip = System.Net.IPAddress.Parse(ipString);
return x;
}

--
Michael Culley


Thanks for the suggestion Michael, but the same error occurs when
I
supply
the full name for IPAddress. Here is what I have now, but same
error.

class myIp : System.Net.IPAddress
{
private System.Net.IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1"); // ---->error here
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}


try change
private IPAddress ip;
to
private System.Net.IPAddress ip;

and the same for new IPAddress(l);

--
Michael Culley


I'm trying to understand inheritance. I'd like to make
my
own
type
of
IPAddress lets call it myIp.

The following gives me CS0029 error: Cannot implicitly convert
type
'System.Net.IPAddress' to 'Inheritance_Testing.myIp'

Could you steer me in the right direction?


class myIp : System.Net.IPAddress
{
private IPAddress ip;

public myIp(long l) : base(l)
{
this.ip = new IPAddress(l);
}


[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127.0.0.1");
byte[] bts = t.GetAddressBytes();
foreach(byte b in bts)
{
Console.WriteLine(b.ToString());
}

}
}
 
Back
Top