Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Protected property access

Status
Not open for further replies.

SpankySpanky

Programmer
May 25, 2005
21
AU
I am using the TCPClient class in a project, and it has a protected property 'Client' which I would like to access to get properties of the underlying socket. As I understand it, a protected property can only be accessed from within the base class and derived classes, therefore I cannot access it as a normal property.

I tried making another SpecialTCPClient class which inherits TCPClient with the intention of exposing the protected property publicly as a readonly property. But when I receive a TCPClient object from the TCPListener and try to store it into my SpecialTCPClient variable, I get an error saying the cast is invalid. I thought this would work because it was derived from the TCPClient.

What am I doing wrong and how do I access the protected property if not this way? Why would it have been made protected in the first place?

Thanks for any insight.
 
ask yourself this

a chimpansee is an ape
is an ape a chimpansee?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
That's not a very accurate analogy. If a chimpansee (SpecialTCPClient) is an ape (TCPClient), then the function should be able to work with the chimpansee when it asks for an ape:

Code:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MsgBox(Test(New Panel))
    MsgBox(Test(New xPanel))
  End Sub

  Private Function Test(ByRef P As Panel) As String
    Return P.ToString
  End Function

  Friend Class xPanel
    Inherits Panel

  End Class

The above code works, because xPanel is derived from Panel. Inherited classes can always be converted back to their base class. Spanky's code therefore should work. He probably has to implements some interface to handle the cast manually. I'm looking into it ;)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
To Spanky: can't you use the AcceptSocket function to get to the underlying socket immediately???

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Why is my analogy false? It's not because you make the wrong conclusions that the analogy is false.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Because you state a chimpansee is an ape.. and the function asks for an ape.. not specifically a chimpansee, but any ape. And the chimpansee is just that.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
a SpecialTCPClient is a TCPClient
is a TCPClient a SpecialTCPClient ?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Its an issue of absolutes. Replace the word 'a' with 'all' and see if Chrissie's analogy makes sence.

all SpecialTCPClient are TCPClients
are all TCPClients SpecialTCPClients?

all chimpansee are apes
are all apes chimpansees?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thank you both for your comments. I think I can see what you are both saying.

Chrissie1, my response is that the SpecialTCPClient does everything that the TCPClient does plus the extra functions I add. Therefore if a TCPClient object is received you don't believe I should be able to put it into the same space where a SpecialTCPCLient object would go because it doesnt have the extra functionality required to qualify as one. But assigning a SpecialTCPClient object to a TCPClient variable would be acceptable because all the functions avaliable to the variable would be available in the assigned object. Yes?

My question as before is, how do I take a TCPClient object with a protected property that I cannot access, and make it accessible? I believed that if I derive a class from the TCPClient, because its property is protected then I can access it from within the derived class and assign it to a public property.

Ruffnekk, Thanks for the sample, I'm interested on your thoughts about 'implementing an interface to cast manually' - I'm not strong with interfaces yet and I'm not sure what this would entail.

I am working with TCPClient objects on both ends, If I use AcceptSocket then I get a socket not a TCPClient and AcceptTCPClient shields these properties again. Socket objects themselves have the endpoints available but sockets are a bit harder to drive properly and I fear losing the robustness that I currently have from the working TCPClient solution.

Thanks
 
I'm not sure this would work, but...

define your special class:
Code:
Public Class MyTCPClient
  Inherits TCPClient

  Public Readonly Property MyProperty As String
    Get
      return mybase.property
    end get
  end property
end class

Declare your client as MyTCPClient, but anywheres you are interacting with another object that requires a TCPClient, cast your object to a TCPClient.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
BTW ruffnekk

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MsgBox(Test(New Panel))
    MsgBox(Test(New xPanel))
  End Sub

  Private Function Test(ByRef P As Panel) As String
    Return P.ToString
  End Function

  Friend Class xPanel
    Inherits Panel

  End Class

is completely correct, but what spankyspanky is trying to is this

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MsgBox(Test(New Panel))
    MsgBox(Test(New xPanel))
  End Sub

  Private Function Test(ByRef P As xPanel) As String
    Return P.ToString
  End Function

  Friend Class xPanel
    Inherits Panel

  End Class

wich won't work for the first test "MsgBox(Test(New Panel))"

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
to spankyspanky

Chrissie1, my response is that the SpecialTCPClient does everything that the TCPClient does plus the extra functions I add. Therefore if a TCPClient object is received you don't believe I should be able to put it into the same space where a SpecialTCPCLient object would go because it doesnt have the extra functionality required to qualify as one. But assigning a SpecialTCPClient object to a TCPClient variable would be acceptable because all the functions avaliable to the variable would be available in the assigned object. Yes?

yep you got it.

and since you are getting your object from tcplistener I think you have a problem unless you make a new tcplistener wich returns a specialtcpclient. But perhaps you should take ruffneks appraoch and search for a function that gives you what you want. Since the tcpclient has what you want you should e able to find it somwhere else.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I agree now. It works one way but not the other way. I tried some more stuff and I was inaccurate myself =P please flame me now *G*

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
No problem.

[curse] [flame]

Glad I could help.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top