troubleshooting Question

VB.Net Alternatives to Casting from a superclass to a subclass

Avatar of apuma
apumaFlag for United States of America asked on
.NET ProgrammingVisual Basic.NET
13 Comments1 Solution1400 ViewsLast Modified:
Hello experts,

I am working with a winform and some custom classes.

What i am trying to do:

I have a class that retrieves objects from a DB and overrides the ToString Method
public class myBaseClass

private sub new()
 'made private so that a data row cannot be represented by multiple instances
end sub

 public shared function getInstances as List(of myBaseClass)
end function

public overrides function toString() as String
 return "The most normal representation of this object as a string"
end function

end class

I have a form that when it loads attempts to get the data from myBaseClass and display it in a combobox on the form
private sub displayMyBaseClasses()
for each MBC as myBaseClass in myBaseClass.getInstances
this works great at getting the objects into the control showing there base toString as the text, and maintaining the myBaseClass object as the selectedItem.(in some cases)

the problem is that for this form I do not want to use the default ToString of the myBaseClass objects.  I would like to use a different Implementation of toString for this class.

I have tried creating a superClass with a new toString Method
public class mySuperClass
   inherits myBaseClass
  public overrides function toString() as String
   return "A Specialized text representation of myBaseClass"
  end function
end class

the thought being that i could cast the objects to this class before placing them in mycombobox.items, so that the super toString() would be called, but leave the objects as a type compatible with myBaseClass

the problem is that when i try to perform the cast it fails in runtime due to downcasting (or upcasting as some would call it)
for each MBC as myBaseClass in myBaseClass.getInstances

I am looking for ways to get the object in that combobox as some representation of mybaseclass but get the combobox item renderer (internal paint method or whatever) to call a custom toString

some limitations or strategies i wish to avoid
- moving the getInstances function out of myBaseClass, or copy it somewhere else
- having to cast or manipulate the combobox.selectedItem later in the code to get a myBaseClassObject
- do not want to change the combobox control to something custom

I have a feeling that an interface may be part of a good solution but i just cant nail it down.

Thanks for your time and i look forward to hearing your questions, and solutions!
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros