Jump to content


Photo
- - - - -

OOP Question about common method in base class (probably simple)

oop network communication

  • Please log in to reply
5 replies to this topic

#1 for(imstuck)

for(imstuck)

    Very Active

  • Members
  • PipPipPip
  • 139 posts
  • Location:Texas
  • Version:LabVIEW 2012
  • Since:2008

Posted 28 July 2012 - 06:49 PM

My OOP naivety shows again, but oh well, I'll learn, albeit slowly. I have a "network connection" base class and from it I have some child classes. I want to create a public method to check if a connection is valid. It seems like this is a method that should somehow go in the base class because it will be the same for all classes (check a connection refnum whether it's tcp,udp, etc, and see if it's valid). But, I am not sure how to best accomplish getting the connection refnum which is held in the child class, into the base class. I obviously don't want some sort of public accessor to call to return the refnum to be passed to the base classes method because I don't want the connection refnum available outside the class. But, I was trying to avoid having a "is connection valid" method in every single class also, when it seems a generic method would work fine.

Suggestions?

Edited by for(imstuck), 28 July 2012 - 06:53 PM.

CLA (but still always learning)

#2 drjdpowell

drjdpowell

    The 500 club

  • Premium Member
  • 543 posts
  • Location:Oxford, UK
  • Version:LabVIEW 2011
  • Since:1999

Posted 28 July 2012 - 08:40 PM

Other than for TCP and UDP, how many different “Is Valid” methods can you possibly need?

Note that you can make a method “protected”, which would allow your base class to call child-class methods without making the methods public.

Also note this conversation and consider if you really need an “Is Valid” method.

#3 Aristos Queue

Aristos Queue

    LV R&D: I write C++/# so you don't have to.

  • Premium Member
  • 2,620 posts
  • Location:Austin, TX
  • Version:LabVIEW 2011
  • Since:2000

Posted 29 July 2012 - 10:09 AM

Add a dynamic dispatch protected scope "Get Refnum" to the base class. Make it "Must Override". Call it in the Is Valid implementation.

#4 Jeffrey Habets

Jeffrey Habets

    Very Active

  • Premium Member
  • 188 posts
  • Location:Netherlands
  • Version:LabVIEW 2011
  • Since:1997

Posted 30 July 2012 - 11:42 AM

I'd expect you need to do more then just a simple valid-refnum check to see if a connection is valid and that it would be different actions depending on the protocol that is used. So I would opt for implementing a dynamic dispatch "CheckConnection" method and make it "Must Override" so that each child has to implements it's own.

If you're really certain that you can use the same piece of code to check connection for all child implementations, I vote for AQ's solution above.
---
Jeffrey
VIT

#5 crelf

crelf

    I'm a LAVA, not a fighter.

  • V I Engineering, Inc.
  • 5,742 posts
  • Version:LabVIEW 2012
  • Since:1993

Posted 30 July 2012 - 05:35 PM

It sounds like your refnum is the same type between each of the child classes? If so, why not put it in the parent class?

post-181-1170858537.png


#6 Aristos Queue

Aristos Queue

    LV R&D: I write C++/# so you don't have to.

  • Premium Member
  • 2,620 posts
  • Location:Austin, TX
  • Version:LabVIEW 2011
  • Since:2000

Posted 31 July 2012 - 09:38 AM

It sounds like your refnum is the same type between each of the child classes?

His refnum is explicitly not the same. It's TCP or UDP or whatever.

I should note, regarding my solution, that each child class would have to cast its reference as an int32 in order to return it to parent.

Jeffery raises the point about doing more than just checking for Not A Refnum... i.e., checking if a non-zero refnum is still valid. Very important, for (imstuck): read this please:
http://lavag.org/top...ur-app-at-risk/





Also tagged with one or more of these keywords: oop, network communication