Solved

Display correct string from Type...End Type

Posted on 1998-09-04
7
130 Views
Last Modified: 2010-04-30
I'm using Type...End Type with a ton of strings and would like to be able to call the right one using a changing variable.  Like below:

Type ThisIsATest
  Temp as String
End Type

Dim Test as ThisIsATest

'This will give you an idea of how I want to use it

Temp2 = "Temp" 'temp2 will always be different

'temp will of course have a value from somewhere else in the 'program

text1.Text = test.temp2
--------------------------------------------

I've tried it as many different ways that I could think of and none of them worked.  I'm trying to display the text that is in a string when a user selects a specific one.
0
Comment
Question by:dokken
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1433028
This will not work:
  text1.Text = test.temp2
Because temp2 isn't described in then ThisIsATest structure.

From the what I gather in your question, what you want is an array.
0
 

Author Comment

by:dokken
ID: 1433029
Right I knew Temp2 wouldn't work, it was just given to make things easier to understand.  How would I use an Array?  I don't have a lot of experience working with them.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1433030
You would dimension an array as such:

Dim MyArray(10) As String

Then you would assign and read values just like any other variable, except you must include the index:

MyArray(1) = "Value 1"
MyArray(2) = "Value 2"

Text1.Text = MyArray(2)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:dokken
ID: 1433031
Not quite what I want to do.  I'm reading in over 300 strings and using the treeview component.  I want to be able to use the treeview click event to display the right string which is named the same as the treeview's key.  That's why I'm using the Type...End Type.  I was just doing:
If tree.Nodes("string1").Selected Then txtInfo.Text = temp.string1

I was doing it over and over again changing the key for each one, so I was ending up with a ton of "if" lines and a lot of typing.  So I was hoping their would be a quicker way of doing it.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1433032
Not necessarily quicker, but less coding:

Using the type and array:
Type MyType
  Key As String
  Value As String
End Type
Dim MyArray(10) As MyType
MyArray(1).Key = "String 1"
MyArray(1).Value = "Text 1"
MyArray(2).Key = "String 2"
MyArray(2).Value = "Text 2"
'etc

Then to select the correct array element:
For nCnt1 = 0 To tree.Nodes.Count - 1
  If tree.Nodes(nCnt1).Selected Then
    For nCnt2 = LBound(MyArray) to UBound(MyArray)
      If MyArray(nCnt2).Key = tree.Nodes(nCnt1).Key Then
        txtInfo.Text = MyArray(nCnt2).Value
        Exit For
      End If
    Next nCnt2
  End If
Next nCnt1
0
 

Author Comment

by:dokken
ID: 1433033
Cool, that worked... a lot less typing too :)  post something as an answer so I can give you credit.
0
 
LVL 6

Accepted Solution

by:
clifABB earned 30 total points
ID: 1433034
Thanks.  :)


Using the type and array:
Type MyType
  Key As String
  Value As String
End Type
Dim MyArray(10) As MyType
MyArray(1).Key = "String 1"
MyArray(1).Value = "Text 1"
MyArray(2).Key = "String 2"
MyArray(2).Value = "Text 2"
'etc

Then to select the correct array element:
For nCnt1 = 0 To tree.Nodes.Count - 1
  If tree.Nodes(nCnt1).Selected Then
    For nCnt2 = LBound(MyArray) to UBound(MyArray)
      If MyArray(nCnt2).Key = tree.Nodes(nCnt1).Key Then
        txtInfo.Text = MyArray(nCnt2).Value
        Exit For
      End If
    Next nCnt2
  End If
Next nCnt1
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now