Solved

Display correct string from Type...End Type

Posted on 1998-09-04
7
136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

751 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