Solved

Display correct string from Type...End Type

Posted on 1998-09-04
7
137 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

636 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