You’d think I should know this by now, but I haven’t tried before… and now that I am, it’s not working out so well. Say I have a class called ‘testClass’. I have a function that’s part of another class that takes an object as a parameter. How can I pass a reference of ‘testClass’ without dimming it as ‘new’ first? I want to ‘dim myClass as new [passedClassObjectReference]’ from within the function. Is this possible, and if so… could somebody please advise how? Since the object isn’t exactly constructed yet, don’t know how to do it (I get errors, as expected).
For further clarification, if I have a method/function named ‘myFunction’ and a class named ‘testClass’, and I use the following:
I get a “You cannot assign a non-value type to a value” error. The only way around this, that I’ve found, is to construct the object with a ‘dim new’ or ‘= new’ before passing it. I don’t want that. Again, I want the function to handle that (which passes the reference class to a thread, and the thread will construct a new object for each instance).
[quote=130929:@Eric Brown]For further clarification, if I have a method/function named ‘myFunction’ and a class named ‘testClass’, and I use the following:
I get a “You cannot assign a non-value type to a value” error. The only way around this, that I’ve found, is to construct the object with a ‘dim new’ or ‘= new’ before passing it. I don’t want that. Again, I want the function to handle that (which passes the reference class to a thread, and the thread will construct a new object for each instance).[/quote]
Why insist on passing an object if it shall be created later into a thread ? Why not pass the characteristics of the object that you will later create, like the name ? Then the thread will create the new object and you are fine.
How do I construct an object based just on a name? What I’m trying to accomplish here is that this is a tcpSocket handler class that I’m creating (external class) and I want to drop it into various projects. Each project has it own command reference classes (unique classes that handle internal project commands). I don’t want to have to go and edit this tcpSocket handler’s source code with each individual project, except when I construct the external object (ie. tcpSocketClass = externalTcpSocketClass(thisProjectsCommandReferenceClass).
Now that you mention ‘by name’. I wonder if what I’m trying to accomplish can be done, at the very least, via introspection?
What’s wrong with
tcpSocketClass = externalTcpSocketClass(new thisProjectsCommandReferenceClass) //added new
That’s the way I’d do it, create the thing you want it to use and pass it in.
Does externalTcpSocketClass need to make more instances or just the one?
It needs to make more/separate instances of the class object (one for each thread that is produced).
OK, what I’ve used is a shared method on each class that return new instances and those shared method are passed in. It’s sort of a factory pattern maybe.
Private Shared Function newInstance() As Object
return new Class1
Private Delegate Function delInstantiator() As Object
externalTcp(AddressOf Class1.newInstance) //pass in the instance making method
Private Sub externalTcp(classMaker As delInstantiator)
dim o As Object = classMaker.Invoke //create new instances
dim o2 As Object = classMaker.Invoke
You’d need to add the Shared Method to each class then it can be plugged into your external system, and of course choose an appropriate return type.
I am sorry, but I do not follow you. You want a method that accepts an object but yet you refuse to properly construct the object because it should be created later in a thread.
I have trouble wrapping my head around a method that uses an object that does not exist yet and will be created as the result of that method passing instructions to a thread which will result in the creation of that object. Unless you found a special formula for time travel that allows the use of a reentrance following a temporal paradox, I do not see how you can achieve that.
Since the object does not exist yet, how do you want to pass it to your method ? Dim thing as object does not create the object, it merely reserves the name. Hence the idea to simply use the name and other parameters you want to attach to it, before it is actually constructed in the thread.
Akin to ordering a birthday cake : “I want it will strawberry glaze, with the inscription ‘Happy Birthday Eric’ and big enough to accommodate 12”. The cake does not exist, it will be constructed in the pastry chef kitchen later.
All that said, what is wrong with a reusable object property ?
[code]Sub myFunction(ti As Introspection.TypeInfo)
Dim cis() As Introspection.ConstructorInfo = ti.GetConstructors()
Dim obj As Object = ci(0).Invoke()
Check out OO patterns. Perhaps you need the factory one here. Give your classes an interface. Instantiate the class that you need and then pass the interface as parameter.
I’ve still got the old implementation of patterns on my website at http://www.mothsoftware.com/content/xojo/. Nowadays there are also pattern projects in the Xojo examples folder.
Don’t pass in a class instance.
Pass in a delegate that creates whatever the kind of object it is you need.
That delegate will probably need to return a class that implements some common interface so you can properly specify the return type.
That way the class just has to know it HAS a means to create whatever class but the actual creation is done elsewhere.