Results 1 to 13 of 13

Thread: Adding a Variable by Clicking Button

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Adding a Variable by Clicking Button

    Hello,

    As the title indicates, I am trying to add a new variable when a button is clicked: Example - File Location, I want to provide 1 variable by default for a file to open. I would like to add other files as they are needed by clicking an "Add File Location" button so they can include additional files as needed. I have no way of knowing how many files may need to be included so I have to make the button add a generic variable name "File" then increment the additional files "File2", "File3", etc.

    Thanks in advance

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,144

    Re: Adding a Variable by Clicking Button

    It doesn't really work that way. What you want is a List(of T), where every time they click the button, you add something to the list. What you add in there isn't quite clear, but it sounds like you wanted to put the path of a file into the variable, so a List(of String) with each item being a path would make sense. It might be necessary to make up some small class that has the path and some other things associated with it. If that were the case, then you'd be making a List(of yourClass).
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Re: Adding a Variable by Clicking Button

    Shaggy,
    You are on point with what I am doing. I'm trying to allow the end user to select the appropriate .exe's from their own folder storage locations to run in sequence from within my program. Like if I was to have multiple different printers and wanted my program to run the installer for the one(s) that are at a specific site. That way when the end users setup new workstations they can run my program to install x sites printers based on the installers they have picked for those sites.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,895

    Re: Adding a Variable by Clicking Button

    Quote Originally Posted by MortMort View Post
    Shaggy,
    You are on point with what I am doing. I'm trying to allow the end user to select the appropriate .exe's from their own folder storage locations to run in sequence from within my program. Like if I was to have multiple different printers and wanted my program to run the installer for the one(s) that are at a specific site. That way when the end users setup new workstations they can run my program to install x sites printers based on the installers they have picked for those sites.
    So you know what to do then, i.e. exactly what Shaggy said. You can't add variables at run time. Variables are declared a design time. At run time, you can execute a method and assign something to a variable declared in that method, but the variable ceases to exist when the method completes. You can't magically declare additional variables at the class level because that requires writing code and, once you've compiled your app, installed it and are running it, it's a bit late to write more code.

    As Shaggy said, you only need one variable, i.e. a variable that refers to a data structure that can contain an arbitrary number of data items. As suggested, if you just need to store an arbitrary number of Strings containing file paths then a List(Of String) is what you want. It's like an array but can grow and shrink as required. If you need to be able to identify each path by a name then you can use a Dictionary(Of String, String) instead, where you have the name Strings as keys and the path Strings as values.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Re: Adding a Variable by Clicking Button

    Jmcilhinney,

    I have not worked with List(Of String) or Dictionary(Of String, String) before. Could I ask you to show me an example?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,895

    Re: Adding a Variable by Clicking Button

    There are loads of examples out there already. Have a look, have a go and then post back if you have a specific issue.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,144

    Re: Adding a Variable by Clicking Button

    A List is a wrapper on an array. If you haven't worked with arrays, they are pretty conceptually simple: It's a collection with a fixed size, kind of like an egg carton. That's a bit of a nuisance, as such, because egg cartons come in just a few sizes. If you need 13 eggs, you need two cartons, just because you have one egg more than most cartons hold. Therefore, the List was added so you have, in effect, a carton that can grow or shrink to meet your needs. There are Add, Insert, Remove, RemoveAt, and many others so that you can adjust the carton to hold not just the number you want, but also to hold them in the order you want.

    Look up List(of T) for examples. They'd be better than we would likely write, but keep in mind, it's just a carton. A dictionary would be a carton with labels for each item so that you can get the items by label as well as by order.
    My usual boring signature: Nothing

  8. #8
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    910

    Re: Adding a Variable by Clicking Button

    I used to love being able to declare variables at program run time. It was standard practice using DCL on VMS where you could create new variables by concatenating an alphabetical variable name with a numeric variable to create a new variable entirely.

    thisvar'a ! where a = numeric variable = 1.

    Would create new variable thisvar1.

    DCL was interpreted so this made it possible but it could be transpiled into fortran and the same effect obtained, perhaps using open-ended arrays to simulate it. In DCL you could dynamically create vars with named values up to 32767 I think, if memory serves me correctly. I've always bemoaned the loss of that functionality and having done so much DCL scripting in the deep and distant past, I occasionally find myself coding using this method even in VB6 where of course it does not function.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Re: Adding a Variable by Clicking Button

    Um... a bit embarrassing but I cant seem to delete this particular post.... I looked it up here: http://www.vbforums.com/showthread.p...-Delete-a-post but I don't see the delete button... so I guess this is now a permanent thing...
    Last edited by MortMort; Feb 22nd, 2020 at 01:38 PM. Reason: Deleting Post

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Re: Adding a Variable by Clicking Button

    Shaggy Hiker,

    Thank you for that. I was really struggling with the whole idea of lists, especially when it comes to setting program paths. I remember learning a bit about arrays way back in '98 in an intro to programming class, and you really clarified the list concept. Just curious though, will I be able to run .exe's from the information in the array and still have the ability to use the "Process.start (my code) .WaitForExit()" functionality? I'm thinking I would as the array would only store the path and the .exe file name that the "Process.start" would call, but since I haven't worked with arrays (and honestly I haven't even googled it yet so I am totally acknowledging my excitement in learning this and my laziness for asking now instead of looking it up to see if it's just as it sounds) I could be completely off on it.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,144

    Re: Adding a Variable by Clicking Button

    You can put anything at all into a List(of T), that's what the T part is about, as it stands for Type. When you create the list, you say what type it will hold. In your case, it sounds like you are talking about paths, which are just strings, so a List(of string). They are just strings, just like eggs in an egg carton. Just because the egg is in the carton, it is still an egg.

    As for deleting posts, that unlocks after some number of posts. I don't remember how many.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2019
    Posts
    12

    Re: Adding a Variable by Clicking Button

    So I may be doing this a bit wrong, but what I have is the following:

    Code:
        Public Sub listcam()
            Dim blist As New List(Of String)
            blist.Add("")
            blist.Add("")
            blist.Add("")
    
        End Sub
    Then I have my button to select the files to run:
    Code:
        Private Sub SelectFileABTN_Click(sender As Object, e As EventArgs) Handles SelectFileABTN.Click
            OpenFileDialog1.Filter = "Program File | *.exe"
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                listcam.blist = OpenFileDialog1.FileName
    
            End If
        End Sub
    As I am sure you have already detected, this button code does not. I know the OpenFileDialog1 is correct (Tested with another button) but I don't know how to get the information (the .exe file & path info) into the List(of String).

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,895

    Re: Adding a Variable by Clicking Button

    There's no point declaring your List(Of String) variable inside a method because that means that it only exists when that method is executing and is only accessible to code inside that method. If you think that you can access a local variable by qualifying it with a method name, you need to spend a lot more time learning the basics. To that end, there's a link to a tutorial in my signature below. I suggest that you follow it and work your way through it. That will help teach you the difference between a local variable and a field.

    Apart from that, how could you assign a String, which is the type of that FileName property, to a variable that is type List(Of String)? You appear to have done enough research to work out that you need to call Add to add an item, and then decided not to do that when you actually want to add an item. You obviously don't want three empty Strings in the list so why are you adding them? You do want the FileName selected in the OpenFileDialog added so why are you not adding that? You seem to be trying to treat it like an array by adding a fixed number of items and then setting those items. That defeats the purpose of a collection. The point is that it grows and shrinks as needed. Add an item when you need to add an item. Remove an item when you need to remove an item. Set an existing item when you need to change an item. Where an array is like an egg carton, with a specific number of elements that are all empty by default, a collection is like a bag, which just contains as many items as you add to it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width