Dim oNewPart vs oNewPart

Dim oNewPart vs oNewPart

tomislav.peran
Advocate Advocate
812 Views
6 Replies
Message 1 of 7

Dim oNewPart vs oNewPart

tomislav.peran
Advocate
Advocate

Hello,

 

I have a question about the variable definition. When using "Dim" in front of a variable name I am not able to use that variable in different blocks (Select Case example). 

 

When I do not use "Dim"  in front of the name I can use the same variable everywhere like in the following example:

 

Dim oOptions As New List (Of String)

oOptions.Add("1.Choice number 1")
oOptions.Add("2.Choice number 2")
Dim oChosenOption As String = InputListBox("Choose option", oOptions, "", "Railing type", "List of Options") 

Select Case oChosenOption ']
	
Case "1.Choice number 1"
	
oNewPart = Components.Add(oPartPrefix1 & i, oPartFile1, part_pos, grounded := True)

Case "2.1.Choice number 2"

oNewPart = Components.Add(oPartPrefix2 & j, oPartFile1, part_pos, grounded := True)

End Select

If I used "Dim oNewPart" in place of "oPart" I would get an error when saving.

 

Can somebody please explain why is that? 

 

Tom

0 Likes
Accepted solutions (1)
813 Views
6 Replies
Replies (6)
Message 2 of 7

tomislav.peran
Advocate
Advocate

And is it maybe possible to define Dim oNewPart As an empty variable and afterward get it inside the block and define it because I can not use variable oNewPart outside of its block?

0 Likes
Message 3 of 7

Frederick_Law
Mentor
Mentor

The craziness of VB.

Dim redefine a new variable.

Dim oNewPart create a new reference to oNewPart

Dim oNewPart = create a new reference to oNewPart and assign a value to it.

oNewPart = assign a value to oNewPart using same reference (memory address)

 

So Dim oNewPart in sub will make it a new variable in the sub with nothing to do with oNewPart outside the sub.

 

Confess yet?

 

0 Likes
Message 4 of 7

Curtis_W
Consultant
Consultant
Accepted solution

Hi @tomislav.peran,

 

Give this a try.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oNewPart As ManagedComponentOccurrence

Select Case oChosenOption ']

	Case "1.Choice number 1"

		oNewPart = Components.Add(oPartPrefix1 & i, oPartFile1, part_pos, grounded := True)

	Case "2.Choice number 2"

		oNewPart = Components.Add(oPartPrefix2 & j, oPartFile1, part_pos, grounded := True)

End Select

 

 EESignature

Message 6 of 7

tomislav.peran
Advocate
Advocate

God bless you, Curtis. This just might cut my code by a lot. 

 

Thank you for helping on this forum. 

 

Tom

 

 

Message 7 of 7

tomislav.peran
Advocate
Advocate

Thank you. I will definitely look into it. 🙂

 

Tom 

0 Likes