0

I've been told to think outside the box, and think of a way to create an object in VB without using the new keyword. I was told it is possible but i'm having trouble figuring it out. I know primitive data gets stored on the stack and the reference to the objects get stored there too but the actual memory space for the object is in the heap and that new does that for us. When i try it without new i mostly get null reference exception, any ideas on how this is possible?

Dim objTest as TestOne()

'some class named TestOne with empty Constructor
8
  • What happens if you evaluate an expression like (1+2).ToString()? Commented Oct 26, 2019 at 23:50
  • its for older visual basic. Commented Oct 27, 2019 at 1:00
  • 2
    Maybe you should specify what that older version is and edit your question accordingly. We shouldn't have to guess stuff like that. You need to provide a FULL and CLEAR explanation of the problem. What language it's for is obviously part of that. Commented Oct 27, 2019 at 3:38
  • I've changed the title to specify this is a VB question, not VB.NET - as per OP's comment. Commented Oct 27, 2019 at 13:51
  • OP, can you confirm whether this is VB or VBA? Commented Oct 27, 2019 at 14:24

2 Answers 2

2

In VB you can use the CreateObject function if you have an existing class defined in a COM library that you've created - or a COM library that already exists.

As an example, create a project reference to the Microsoft Scripting Runtime COM library (scrrun.dll). To do this in the VB IDE select Project, References, then pick the reference 'Microsoft Scripting Runtime'. You can then write the following code:

Dim fso As Object
fso = CreateObject("Scripting.FileSystemObject")
Sign up to request clarification or add additional context in comments.

2 Comments

do i need to import something because it does not work. also set keyword is no longer supported by VB. .
@a-exelle, I've now made my answer VB-specific.
1

I haven't done VB for quite a while now, but the Activator exists in both C# and VB. Here's couple lines of C# that you can convert to VB:

var newThing = (TestOne)Activator.CreateInstance(typeof(TestOne));
newThing.ID = 5;

The CreateInstance method returns an object, which I convert to the correct class with

(TestOne)

which is the C# syntax for type conversion. Sorry, I forgot how to do this in VB.

Where class 'TestOne' looks like:

class TestOne {
    public int ID { get; set; }
}

Note that 'Activator' is part of .NET reflection.

5 Comments

when i try this it says testone is a class type and cannot be used as an expression
i was able to get it to work, thank you very much i just had to use getType instead of type of. you were helpful so thank you again!
That's VB.Net, not Vb6. I changed your question tags to VB.Net.
@MarkJ, the OP's original code indicates the older version of VB (so not VB.NET). And in a comment on October 27, the OP says it's the older version of VB. So now I'm really confused!
I share your confusion to be honest! But VB.Net first came out in 2002 so it certainly could be described as "older" 17 years later... And the OP said that Set is no longer supported in VB in one comment - that's true, it is no longer supported in VB.Net.The original code in the question could be VB6 or VB.net. Activator.CreateInstance in this answer is certainly VB.Net and the OP says it works for them. That's why I went with VB.Net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.