Solved

Display correct string from Type...End Type

Posted on 1998-09-04
7
133 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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