Byref in a shared method?

I am creating save state/restore state functions for some custom classes. Since downcasting a type from a superclass to a subclass is a no go, I need a parameter in a shared method to be passed byref so that I can edit the properties of an instance of the subclass in the superclass. Unfortunately it seems an instance of a subclass cannot be passed byref to a parameter of the superclass type, while it can be passed if the parameter is not byref, or if the instance is the same type as the superclass. Here is an example project. Is this a bug or is there another way that this can be done? I would prefer to not have to overload the method for each of the subclasses (and then each of the subclasses of the subclasses, etc) since the functionality of the method is expanded upon in each subclass in order to account for new variables present. Thanks

Use events instead ?

In addition, if it’s an object, why do you need ByRef at all? Are you reassigning the variable in the caller to another object?

The shared methods are used to recreate the state of the classes from a JSONItem representing the data in the class. For each subclass, I wanted to pass the JSONItem to the superclass so that it can fill its properties from the JSON with a call to “Super.FromJSON” which returns an instance of the superclass to be used in the subclass which would fill its own data. Since classes cannot be downcasted from super to subclass, I am trying to do this with the subclass shared method passing an instance byref to the superclass to use so that I do not have to overload the superclass method to return different subclasses (and subclasses of subclasses). Does that make sense?

I’m mostly confused by the fact that a shared method defined without the byref works as expected and will accept an instance of the subclass while a shared method with the byref will not and yields a “Parameters are not compatible with this function” compiler error. Shouldn’t there be no difference?

I was thrown by this too but it makes sense once you follow it through. First, the Sharedness doesn’t matter, this happens with instance methods too.

Consider this method

Sub foo(byref oval As OvalShape) oval = new OvalShape End
Now if you try to call it with an ArcShape (a subclass of OvalShape) then foo() will try to put a new OvalShape instance into the byref parameter oval but that’s a Super class of the actual passed in type. You can’t have arc reference an instance of just OvalShape, it must be ArcShape or a subclass.

[code]dim arc As new ArcShape


//what is arc here?[/code]

That makes sense, thanks Will. I guess I will have to rethink how I do this.