Question Variable name is the textbox.text

hans_cellc

Member
Joined
Mar 23, 2012
Messages
18
Programming Experience
Beginner
I have gloabalVar.otion1 as a variable.
User needs to input his option into a textbox from a dropdown input.
I want to use that input as the variable name and assign some other value to that. Below what I thought might work but does not seem to.

First try:
VB.NET:
globalVar.(textbox1.text).add(textbox5.text)

and the I tried to assign the textbox to a variable and then calling it:
VB.NET:
varName = textbox1.text
globalVar.varName.add(textbox5.text)

This I can understand why because I dont have that name in globalVar.

How can I use the texbox1.text as the variables name?
 
Can you explain what you are trying to achieve overall, I can't see why you would want a user to name your variables

Sent from my GT-I9100 using Tapatalk 2
 
Can you explain what you are trying to achieve overall, I can't see why you would want a user to name your variables

Sent from my GT-I9100 using Tapatalk 2


I have globalVar with variables option1 through 5.
the user selects a option from a new window using a dropdown. This option the is used in the textbox saying you have chosen option1, I then use that to add items to the specific option.
I am new and might get to complicated but would like if there is a diffirent way and even would like how to do this as well.
 
I am unsure what you intend to do when you say "I then use that to add items to the specific option."
If I am understanding you, you simply want a dropdownlist showing 5 options, each has an underlying value. Say for arguments sake you select "five" from the dropdownlist, the selectedvalue could be "5" which could be applied to a textbox?

If so look at using a DataTable as a DataSource for the dropdownlist. Setting the DisplayMember and ValueMember Properties will do as described.
If I misunderstand, please attempt to elaborate
 
Sorry for that let be try again.
I have a booking system, on the reservations window you enter details like: guestname, contactNumber, roomNumber, adults, children, checkInDate, checkOutDate, period and totalDue. When clicking roomNumber it opens a window with buttons of the available rooms, upon clicking the button the details are captured into romNumber and the pricing used to calculate totalDue depending on adults, children and period. That all works. Now to capture this form detail without a text file or database I want to save the details to an array file for the specific room number so when clicking reports and the room number it will tell you the room is empty or a person with all their details are booked in that room. Therefore without using pages of conditional statements I just want to grab the .text from the roomNumber as that is the same as the arrays I created ie: room1, room2 or room3.
All is set up and I only want to grab the text and use it as he variable name not value.
I hope you understand it this way, I know writing to files or DB would be much simpler but texercisesise is for variables and arrays.

Thanks
 
OK I (think I) understand. My only warning is, without a file or database, if your application crashes then you lose all of your data. If this is just an exercise fine, otherwise have a rethink.
I would probably handle this with a custom class for rooms, one of which properties would be a custom class for guest. If you then have a List of rooms you can query that List to see if it it has a guest assigned. If guest is null, handle room empty, else show guest information.



Public Class Rooms
    Public Property Number() As Integer
    Public Property Occupied() As Boolean
    Public Property GuestDetails() As Guest
End Class

Public Class Guest
    Public Property Name() As String
    Public Property CheckInDate() As DateTime
End Class

Public Class Form1

    Public listRooms As List(Of Rooms)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Instantiate List
        listRooms = New List(Of Rooms)()

        'add some rooms
        For i = 0 To 4
            Dim newRoom As New Rooms
            newRoom.Occupied = False
            newRoom.Number = i
            listRooms.Add(newRoom)
        Next

    End Sub
End Class



So on Form1 I created a list of 5 rooms at class level. I can then store guest info against each room as please by adding details

'checking someone into room 1
        listRooms(0).Occupied = True

        Dim newGuest As New Guest()
        newGuest.Name = "Mr Smith"
        newGuest.CheckInDate = DateTime.Today
        listRooms(0).GuestDetails = newGuest

'clear guest details - checkout
listRooms(0).GuestDetails = Nothing
listRooms(0).Occupied = false
 
Back
Top