Concatenating a Variable name

Concatenating a Variable name

brianA32ZM
Advocate Advocate
816 Views
8 Replies
Message 1 of 9

Concatenating a Variable name

brianA32ZM
Advocate
Advocate

Hello, I am inquiring if it is possible to concatenate a Variable name. In the simple example below I am looking to call oStud_Zinc and oStud_STSTL as "oStud & _Zinc" and "oStud  & _STSTL" respectively in the If statement. The end code will process several hardware types and I hoping to simplify the code by looping through the material names;  "oStud" in this case.

 

Dim oStud_Zinc as String = "insert file path and name" 

Dim oStud_STSTL as String = "insert file path and name" 


If oOccFullFileName.Contains(oStud & _Zinc)

 

Component.Replace(oOcc.Name, oOccFullFileName.Replace(oStud & _Zinc, oStud & _STSTL), True)

 

End if

 

0 Likes
Accepted solutions (1)
817 Views
8 Replies
Replies (8)
Message 2 of 9

A.Acheson
Mentor
Mentor

Hi @brianA32ZM 

Something like this work for you?

Dim oStud As String = "1"
Dim Zinc as String = "zert"
Dim STSTL As String = "St"
Dim oStud_Zinc as String = oStud & Zinc
Messagebox.Show(oStud_Zinc)
Dim oStud_STSTL as String = oStud  & STSTL
Messagebox.Show(oStud_STSTL)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 9

brianA32ZM
Advocate
Advocate
 Hello @A.Acheson 

 

Thank you, but I am looking to perform the task below:

 

Dim oStud_Zinc As String = "Zinc Stud"
Dim oStud_STSTL As String = "STSTL Stud"

Dim oStud_STSTL As String = oStud & STSTL
Dim MyStringValues = New String(){"_STSTL", "_Zinc"}

For Each MyStringValue in MyStringValues
MessageBox.Show(oStud & MyStringValue)
Next
'resulting in "Zinc Stud" and "STSTL Stud"

 

0 Likes
Message 4 of 9

jjstr8
Collaborator
Collaborator

The short answer is that you can't "dynamically" generate a variable name.  I would look at using your string array for-each loop, having the "if" statement inside the loop.  The string array would contains values for the "old" material to check for.   Is it always certain materials replaced with a single one, or are there multiple combinations?  If there are multiple combinations, you could make two string arrays, one for "old" and one for "new" material.  Your loop would change to a regular "for" loop so you can reference elements in each array using an index number.

0 Likes
Message 5 of 9

brianA32ZM
Advocate
Advocate

Hello @jjstr8,

 

Thank you! 

   I think I am getting closer with the code below. The issue is that I am not getting the value of oZincName and oSTSTLName. Is it possible to do this with a list or does it need to be a collection? Or I am attacking this wrong?

	Dim oHardwareNames = New String() {"oSTSTLSealStud_Zinc|oSTSTLSealStud_STSTL",
		"oSTSTLSealNut_Zinc|oSTSTLSealNut_STSTL",
		"o2BoltBearingNut_Zinc|o2BoltBearingNut_STSTL",
		"o2BoltBearingStud_Zinc|o2BoltBearingStud_STSTL",
		"o2BoltBearingBolt_Zinc|o2BoltBearingBolt_STSTL",
		"oCA_Washer_Zinc|oCA_Washer_STSTL",
		"oCA_Nut_Zinc|oCA_Nut_STSTL",
		"oCA_Bolt_Zinc|oCA_Bolt_STSTL",
		"oQuadLevBolt_Zinc|oQuadLevBolt_STSTL",
		"oQuadLevNut_Zinc|oQuadLevNut_STSTL",
		"oQuadLevWasher_Zinc|oQuadLevWasher_STSTL",
		"oCrossOverNut_Zinc|oCrossOverNut_STSTL",
		"oCrossOverBolt_Zinc|oCrossOverBolt_STSTL",
		"oCrossOverWasher_Zinc|oCrossOverWasher_STSTL"}
		For Each oHardwareName In oHardwareNames
oZincName = Left(oHardwareName, oHardwareName.LastIndexOf("|")) oSTSTLName = oHardwareName.Remove(0, oHardwareName.LastIndexOf("|") + 1) Logger.info(oZincName) Logger.info(oSTSTLName) If Parameter("Option_HardwareMaterial") = "STSTL" And oOccFullFileName.Contains(oZincName) Component.Replace(oOcc.Name, oOccFullFileName.Replace(oZincName, oSTSTLName), True) ElseIf Parameter("Option_HardwareMaterial") = "Zinc" And oOccFullFileName.Contains(oSTSTLName) Component.Replace(oOcc.Name, oOccFullFileName.Replace(oSTSTLName, oZincName), True) End If Next

 

0 Likes
Message 6 of 9

jjstr8
Collaborator
Collaborator

This is the output I'm getting.  I think the values in your string array are not what you need them to be.  For example, if the original filename contains "SealStud_Zinc" and you want to replace it with a similarly named file that has "SealStud_STSTL" in it instead, then your string array element would be "SealStud_Zinc|SealStud_STSTL". 

 

jjstr8_0-1685642407892.png

 

0 Likes
Message 7 of 9

brianA32ZM
Advocate
Advocate

That is the same result that I am seeing. The result I am looking for is the value of the variables. This is established earlier in the code. If place the Variables in the String separately without quotes the result produces the value of the variable that I am looking for. The issue is that I need the value of the two variables.

 

oSTSTLSealStud_STSTL = "STUD CLINCH 8-32 x .5 INCH LG STAINLESS STEEL.ipt"
oSTSTLSealStud_Zinc = "STUD CLINCH 8-32 x .5 INCH LG Zinc.ipt"

 

0 Likes
Message 8 of 9

jjstr8
Collaborator
Collaborator
Accepted solution

I see.  The string array is just storing text.  I would use a List instead and use the .Add method to build it with all of the combinations you're dealing with.  It may not be an issue for you, but when you do your string comparisons (filename, parameter, etc.), you may want to convert to uppercase so it won't get tripped up on "Zinc" versus "ZINC".

 

Dim oHardwareNames As New List(Of String)
Dim oSTSLSealStud_STSTL As String = "STUD CLINCH 8-32 x .5 INCH LG STAINLESS STEEL.ipt"
Dim oSTSTLSealStud_Zinc As String = "STUD CLINCH 8-32 x .5 INCH LG Zinc.ipt"
oHardwareNames.Add(oSTSLSealStud_STSTL & "|" & oSTSTLSealStud_Zinc)

 

0 Likes
Message 9 of 9

brianA32ZM
Advocate
Advocate

@jjstr8  Thank you! That is exactly what I needed and the extra tip is also appreciated. 

0 Likes