Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display correct string from Type...End Type

Posted on 1998-09-04
7
Medium Priority
?
138 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 90 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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 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…
Suggested Courses

730 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