dcsimg
Results 1 to 7 of 7

Thread: variable not updating when concatenated

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    9

    variable not updating when concatenated

    Hi,

    I'm just learning visual basic.

    I have a variable concatenated here:

    Code:
    Dim strFullUrlDownload As String = strMainUrl & dtDate & strSlash & strPdf & strSlash & strPage & strPageCounter & strDot & strPdf
    Then i have a variable that gets a 1 added to it in a loop

    Code:
    strPageCounter = strPageCounter + 1
    When debugging I noticed that the variable that is concatenated "strFullUrlDownload" the variable within "strPageCounter" is not getting updated. What am I doing wrong and how to fix it?


    Thank you!

  2. #2
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    88

    Re: variable not updating when concatenated

    strFullUrlDownload will show a string value taken from the other variables only at the time it is concatenated. Changing any of those variables after that point will have no effect, unless the process is performed again.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    9

    Re: variable not updating when concatenated

    Quote Originally Posted by bmwpete View Post
    strFullUrlDownload will show a string value taken from the other variables only at the time it is concatenated. Changing any of those variables after that point will have no effect, unless the process is performed again.
    Ok good just as i suspected. What do you do in this instance then as I need strFullUrlDownload variable updated in a loop?

    Also using Visual Studio Express 2013 i noticed one time today a new window appeared on the left side below the pinned toolbox window and it showed the values of all variables during the debugging. I thought this is great. But then it never appeared again after that one time.


    Thanks for any help!

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

    Re: variable not updating when concatenated

    Quote Originally Posted by willyG View Post
    Ok good just as i suspected. What do you do in this instance then as I need strFullUrlDownload variable updated in a loop?
    If you're saying that you want to be able modify the constituent parts in a loop and have the value of strFullUrlDownload update as well then it's just not going to happen. If you want a variable to change then you have to change it. What you could do is use a property instead of a variable. A property is effectively a variable from the outside but a method from the inside, so you can add the code you already have to do the concatenation and put it inside the property. That means that, every time you get the property value, the concatenation will be performed using the current values of the other variables.
    Quote Originally Posted by willyG View Post
    Also using Visual Studio Express 2013 i noticed one time today a new window appeared on the left side below the pinned toolbox window and it showed the values of all variables during the debugging. I thought this is great. But then it never appeared again after that one time.
    The Autos, Locals and Watch windows are usually displayed automatically and by default when you debug, but you can show or hide them yourself whenever you like. The next time you debug, use the Debug menu to select those windows to open. The Autos window will show what the debugger thinks is most relevant at the current time, the Locals window displays local variables and the Watch window will show whatever you specifically choose to watch.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,293

    Re: variable not updating when concatenated

    Quote Originally Posted by jmcilhinney View Post
    If you're saying that you want to be able modify the constituent parts in a loop and have the value of strFullUrlDownload update as well then it's just not going to happen. If you want a variable to change then you have to change it. What you could do is use a property instead of a variable. A property is effectively a variable from the outside but a method from the inside, so you can add the code you already have to do the concatenation and put it inside the property. That means that, every time you get the property value, the concatenation will be performed using the current values of the other variables.
    I usually just do the concatenation in the loop.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: variable not updating when concatenated

    Quote Originally Posted by techgnome View Post
    I usually just do the concatenation in the loop.

    -tg
    I would expect that I would do the same thing but, as the OP decided it was a bad idea to show us the context in which this code was being executed, it's hard to say what the best course of action would be. That said, whether the concatenation is in the loop itself or in a property getter, you're still going to be writing it once and executing it multiple times. The best option really depends on context, which really ought to have been provided from the outset.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    9

    Re: variable not updating when concatenated

    Quote Originally Posted by jmcilhinney View Post
    I would expect that I would do the same thing but, as the OP decided it was a bad idea to show us the context in which this code was being executed, it's hard to say what the best course of action would be. That said, whether the concatenation is in the loop itself or in a property getter, you're still going to be writing it once and executing it multiple times. The best option really depends on context, which really ought to have been provided from the outset.
    Sorry I didn't think it was a bad idea to list the code rather my thinking was it is just a simple thing to make it work. After all a variable is a variable which means it should change right?

    Anyways I've found a solution.


    Code:
    Dim intCounter As Integer = 1
    Dim strPageCounter As String = String.Format("{0:000}", intCounter)
    now i can concatenate the above into a file save path by using

    Code:
    &strPageCounter
    Now when the loop is running and updating the strPageCounter I am able to update the concatenated fields using:

    Code:
    strPageCounter = String.Format("{0:000}", intCounter)
    Thanks to everyone's input

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width