473,779 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance question

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.IPA ddress' to 'Inheritance_Te sting.myIp'

Could you steer me in the right direction?
class myIp : System.Net.IPAd dress
{
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.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}


Nov 15 '05 #1
12 1725
try change
private IPAddress ip; to private System.Net.IPAd dress ip;
and the same for new IPAddress(l);

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message news:#e******** ******@TK2MSFTN GP12.phx.gbl... 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.IPA ddress' to 'Inheritance_Te sting.myIp'

Could you steer me in the right direction?
class myIp : System.Net.IPAd dress
{
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.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}

Nov 15 '05 #2
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.IPAd dress
{
private System.Net.IPAd dress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAd dress(l);
}
[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
byte[] bts = t.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:e2******** ******@TK2MSFTN GP11.phx.gbl...
try change
private IPAddress ip;

to
private System.Net.IPAd dress ip;


and the same for new IPAddress(l);

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:#e******** ******@TK2MSFTN GP12.phx.gbl...
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.IPA ddress' to 'Inheritance_Te sting.myIp'

Could you steer me in the right direction?
class myIp : System.Net.IPAd dress
{
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.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}


Nov 15 '05 #3
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.IPAd dress.Parse(ipS tring);
return x;
}

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message news:Oz******** *****@tk2msftng p13.phx.gbl...
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.IPAd dress
{
private System.Net.IPAd dress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAd dress(l);
}
[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
byte[] bts = t.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:e2******** ******@TK2MSFTN GP11.phx.gbl...
try change
private IPAddress ip;

to
private System.Net.IPAd dress ip;


and the same for new IPAddress(l);

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:#e******** ******@TK2MSFTN GP12.phx.gbl...
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.IPA ddress' to 'Inheritance_Te sting.myIp'

Could you steer me in the right direction?
class myIp : System.Net.IPAd dress
{
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.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}



Nov 15 '05 #4
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" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uK******** ******@TK2MSFTN GP11.phx.gbl...
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.IPAd dress.Parse(ipS tring);
return x;
}

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:Oz******** *****@tk2msftng p13.phx.gbl...
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.IPAd dress
{
private System.Net.IPAd dress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAd dress(l);
}
[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
byte[] bts = t.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:e2******** ******@TK2MSFTN GP11.phx.gbl...
try change
> private IPAddress ip;
to
> private System.Net.IPAd dress ip;

and the same for new IPAddress(l);

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> 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.IPA ddress' to 'Inheritance_Te sting.myIp'
>
> Could you steer me in the right direction?
>
>
> class myIp : System.Net.IPAd dress
> {
> 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.GetAddressByt es();
> foreach(byte b in bts)
> {
> Console.WriteLi ne(b.ToString() );
> }
>
> }
> }
>
>
>
>



Nov 15 '05 #5
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" <ta****@u.washi ngton.edu> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
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" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uK******** ******@TK2MSFTN GP11.phx.gbl...
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.IPAd dress.Parse(ipS tring);
return x;
}

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message news:Oz******** *****@tk2msftng p13.phx.gbl... 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.IPAd dress
{
private System.Net.IPAd dress ip;

public myIp(long l) : base(l)
{
this.ip = new System.Net.IPAd dress(l);
}
[STAThread]
static void Main(string[] args)
{
myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
byte[] bts = t.GetAddressByt es();
foreach(byte b in bts)
{
Console.WriteLi ne(b.ToString() );
}

}
}
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:e2******** ******@TK2MSFTN GP11.phx.gbl...
> try change
> > private IPAddress ip;
> to
> > private System.Net.IPAd dress ip;
>
> and the same for new IPAddress(l);
>
> --
> Michael Culley
>
>
> "Taylor" <ta****@u.washi ngton.edu> wrote in message
news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> > 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.IPA ddress' to 'Inheritance_Te sting.myIp'
> >
> > Could you steer me in the right direction?
> >
> >
> > class myIp : System.Net.IPAd dress
> > {
> > 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.GetAddressByt es();
> > foreach(byte b in bts)
> > {
> > Console.WriteLi ne(b.ToString() );
> > }
> >
> > }
> > }
> >
> >
> >
> >
>
>



Nov 15 '05 #6
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.IPAd dress 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.IPAd dress
{
private System.Net.IPAd dress ip;
public myIp(long l)
: base(l)
{

}
}

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

Console.WriteLi ne(ip.ToString( ));
}
}
}



"Brian W" <brianw@gold_de ath_2_spam_rush .com> wrote in message
news:e6******** *****@tk2msftng p13.phx.gbl...
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" <ta****@u.washi ngton.edu> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
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" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uK******** ******@TK2MSFTN GP11.phx.gbl...
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.IPAd dress.Parse(ipS tring);
return x;
}

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:Oz******** *****@tk2msftng p13.phx.gbl...
> 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.IPAd dress
> {
> private System.Net.IPAd dress ip;
>
> public myIp(long l) : base(l)
> {
> this.ip = new System.Net.IPAd dress(l);
> }
>
>
> [STAThread]
> static void Main(string[] args)
> {
> myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
> byte[] bts = t.GetAddressByt es();
> foreach(byte b in bts)
> {
> Console.WriteLi ne(b.ToString() );
> }
>
> }
> }
>
>
> "Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
> news:e2******** ******@TK2MSFTN GP11.phx.gbl...
> > try change
> > > private IPAddress ip;
> > to
> > > private System.Net.IPAd dress ip;
> >
> > and the same for new IPAddress(l);
> >
> > --
> > Michael Culley
> >
> >
> > "Taylor" <ta****@u.washi ngton.edu> wrote in message
> news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> > > 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.IPA ddress' to 'Inheritance_Te sting.myIp'
> > >
> > > Could you steer me in the right direction?
> > >
> > >
> > > class myIp : System.Net.IPAd dress
> > > {
> > > 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.GetAddressByt es();
> > > foreach(byte b in bts)
> > > {
> > > Console.WriteLi ne(b.ToString() );
> > > }
> > >
> > > }
> > > }
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #7
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247referenc...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.Metho dOne() instead of
DerivedClass.Me thodOne(), 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" <brianw@gold_de ath_2_spam_rush .com> wrote in message
news:e6******** *****@tk2msftng p13.phx.gbl...
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" <ta****@u.washi ngton.edu> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
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" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uK******** ******@TK2MSFTN GP11.phx.gbl...
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.IPAd dress.Parse(ipS tring);
return x;
}

--
Michael Culley
"Taylor" <ta****@u.washi ngton.edu> wrote in message

news:Oz******** *****@tk2msftng p13.phx.gbl...
> 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.IPAd dress
> {
> private System.Net.IPAd dress ip;
>
> public myIp(long l) : base(l)
> {
> this.ip = new System.Net.IPAd dress(l);
> }
>
>
> [STAThread]
> static void Main(string[] args)
> {
> myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
> byte[] bts = t.GetAddressByt es();
> foreach(byte b in bts)
> {
> Console.WriteLi ne(b.ToString() );
> }
>
> }
> }
>
>
> "Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
> news:e2******** ******@TK2MSFTN GP11.phx.gbl...
> > try change
> > > private IPAddress ip;
> > to
> > > private System.Net.IPAd dress ip;
> >
> > and the same for new IPAddress(l);
> >
> > --
> > Michael Culley
> >
> >
> > "Taylor" <ta****@u.washi ngton.edu> wrote in message
> news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> > > 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.IPA ddress' to 'Inheritance_Te sting.myIp'
> > >
> > > Could you steer me in the right direction?
> > >
> > >
> > > class myIp : System.Net.IPAd dress
> > > {
> > > 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.GetAddressByt es();
> > > foreach(byte b in bts)
> > > {
> > > Console.WriteLi ne(b.ToString() );
> > > }
> > >
> > > }
> > > }
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #8
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 ConsoleApplicat ion1
{

class myIp : System.Net.IPAd dress
{

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.Pars e("127.0.0.1" );

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


"Taylor" <ta****@u.washi ngton.edu> wrote in message
news:O5******** *****@TK2MSFTNG P11.phx.gbl...
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247referenc...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.Metho dOne() instead of
DerivedClass.Me thodOne(), 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" <brianw@gold_de ath_2_spam_rush .com> wrote in message
news:e6******** *****@tk2msftng p13.phx.gbl...
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" <ta****@u.washi ngton.edu> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
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" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uK******** ******@TK2MSFTN GP11.phx.gbl...
> 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.IPAd dress.Parse(ipS tring);
> return x;
> }
>
> --
> Michael Culley
>
>
> "Taylor" <ta****@u.washi ngton.edu> wrote in message
news:Oz******** *****@tk2msftng p13.phx.gbl...
> > 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.IPAd dress
> > {
> > private System.Net.IPAd dress ip;
> >
> > public myIp(long l) : base(l)
> > {
> > this.ip = new System.Net.IPAd dress(l);
> > }
> >
> >
> > [STAThread]
> > static void Main(string[] args)
> > {
> > myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
> > byte[] bts = t.GetAddressByt es();
> > foreach(byte b in bts)
> > {
> > Console.WriteLi ne(b.ToString() );
> > }
> >
> > }
> > }
> >
> >
> > "Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
> > news:e2******** ******@TK2MSFTN GP11.phx.gbl...
> > > try change
> > > > private IPAddress ip;
> > > to
> > > > private System.Net.IPAd dress ip;
> > >
> > > and the same for new IPAddress(l);
> > >
> > > --
> > > Michael Culley
> > >
> > >
> > > "Taylor" <ta****@u.washi ngton.edu> wrote in message
> > news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> > > > 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.IPA ddress' to 'Inheritance_Te sting.myIp'
> > > >
> > > > Could you steer me in the right direction?
> > > >
> > > >
> > > > class myIp : System.Net.IPAd dress
> > > > {
> > > > 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.GetAddressByt es();
> > > > foreach(byte b in bts)
> > > > {
> > > > Console.WriteLi ne(b.ToString() );
> > > > }
> > > >
> > > > }
> > > > }
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #9
Hi Brian,

I tried your code, but it produced an InvalidCastExce ption.

Taylor
"Brian W" <brianw@gold_de ath_2_spam_rush .com> wrote in message
news:eD******** *****@TK2MSFTNG P10.phx.gbl...
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 ConsoleApplicat ion1
{

class myIp : System.Net.IPAd dress
{

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.Pars e("127.0.0.1" );

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


"Taylor" <ta****@u.washi ngton.edu> wrote in message
news:O5******** *****@TK2MSFTNG P11.phx.gbl...
I'm thinking that my problem is similar to this one:
http://www.dotnet247.com/247referenc...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.Metho dOne() instead of
DerivedClass.Me thodOne(), 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" <brianw@gold_de ath_2_spam_rush .com> wrote in message
news:e6******** *****@tk2msftng p13.phx.gbl...
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" <ta****@u.washi ngton.edu> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
> 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" <mc*****@NOSPAM optushome.com.a u> wrote in message
> news:uK******** ******@TK2MSFTN GP11.phx.gbl...
> > 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.IPAd dress.Parse(ipS tring);
> > return x;
> > }
> >
> > --
> > Michael Culley
> >
> >
> > "Taylor" <ta****@u.washi ngton.edu> wrote in message
> news:Oz******** *****@tk2msftng p13.phx.gbl...
> > > 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.IPAd dress
> > > {
> > > private System.Net.IPAd dress ip;
> > >
> > > public myIp(long l) : base(l)
> > > {
> > > this.ip = new System.Net.IPAd dress(l);
> > > }
> > >
> > >
> > > [STAThread]
> > > static void Main(string[] args)
> > > {
> > > myIp t = myIp.Parse("127 .0.0.1"); // ---->error here
> > > byte[] bts = t.GetAddressByt es();
> > > foreach(byte b in bts)
> > > {
> > > Console.WriteLi ne(b.ToString() );
> > > }
> > >
> > > }
> > > }
> > >
> > >
> > > "Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in

message > > > news:e2******** ******@TK2MSFTN GP11.phx.gbl...
> > > > try change
> > > > > private IPAddress ip;
> > > > to
> > > > > private System.Net.IPAd dress ip;
> > > >
> > > > and the same for new IPAddress(l);
> > > >
> > > > --
> > > > Michael Culley
> > > >
> > > >
> > > > "Taylor" <ta****@u.washi ngton.edu> wrote in message
> > > news:#e******** ******@TK2MSFTN GP12.phx.gbl...
> > > > > 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.IPA ddress' to 'Inheritance_Te sting.myIp'
> > > > >
> > > > > Could you steer me in the right direction?
> > > > >
> > > > >
> > > > > class myIp : System.Net.IPAd dress
> > > > > {
> > > > > 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.GetAddressByt es();
> > > > > foreach(byte b in bts)
> > > > > {
> > > > > Console.WriteLi ne(b.ToString() );
> > > > > }
> > > > >
> > > > > }
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3752
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2203
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
4
12822
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error below *should* happen, but my question to the community is *why* does it happen? Any answer will be appreciated, but a section and paragraph number from the C++ Standard would be especially appreciated.
8
7831
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
22
23384
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
45
6368
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
2101
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
5
2473
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1841
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.