Is such a thing possible?

I have a e.g. class TestClass with several properties and constructors. The class has for example the property BackgroundColor As Color.

If I now have another class MyClass which have a property Fill As TestClass, is it possible and if so, how, to use MyClass.Fill = Color.Red which then automatically creates an instance of TestClass and sets the BackgroundColor to Color.Red?

Thanks


EDIT: I’ve created a small sample Project. Had no luck until now. Where am I wrong? I think it is about the fact that I will use several subclasses and the right one has to be assigned.

Test Project.xojo_binary_project

Sure. What you’re looking for is the Operator_Convert method. Put it on the TestClass Class and have it take a color.

Var t as new TestClass
T.backgroundColor = newColor
Return t
1 Like

I almost thought so. Let’s see if I have success with it :wink:

I must be misreading the question. My thought was to make Fill a computed property.

2 Likes

I’ve created a small sample Project. Had no luck until now. Where am I wrong? I think it is about the fact that I will use several subclasses and the right one has to be assigned.

Test Project.xojo_binary_project

I’ve corrected your project to work, but I don’t think it’s what you really want to do.
OpConv.xojo_binary_project

Thanks Tim. You’re right. What I want is something like that:

I have a base class called Shading. There are Subclasses of Shading: ColorShading (with BackgroundColor As Color Property) & PictureShading (with BackgroundPicture As Picture Property). Now I simply want to do something like this for Shading instances:

Var shad As Shading ' Base Class.

shad = Color.Red ' Should return a ColorShading (Subclass of Shading) Instance.

' or

shad = MyPicture ' Should return a PictureShading (Subclass of Shading) Instance.

its something like that

Var b As BaseClass
b = SubClass.SharedMethodRed

Var c As SubClass = SubClass(b)
c.SubMethod

whereby the shared method is

Public Shared Function SharedMethodRed() as SubClass

Return New SubClass

End Function

gregs idea with Operator_Convert is better and would direct convert the Color into your class

Hi Markus,

thanks. I know, but shared methods are no option at the moment.

The conversion should be done automatically for the users behind the scenes. Kind of magic.

so have you tried this Operator_Convert ?

Yes I did, but since I have a structure with couple of subclasses, I hadn’t any success until now. Please have a look at my forth post here with more details.

I don’t really like the solution approach, but I think I’m going to implement the whole thing using shared methods first.

If someone else comes up with a different approach for the desired scenario, I would be happy to receive a post.

seems if you put the Operator_Convert in the colorshading class you can not convert it direct.

'Var b As BaseClass

'b = Color.Red

Var b As ColorShadingClass 'with Super to BaseClass

b = Color.Red

this is inside of colorshading

Public Sub Operator_Convert(col As Color)

  Self.BackgroundColor = col
  
End Sub

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.