Android Question Double variables

Discussion in 'Android Questions' started by welu1805, Jan 19, 2015.

  1. welu1805

    welu1805 Active Member Licensed User

    Sorry, but I'm confused: Is is possible to declare a variable twice.

    Dim i As Int : i = 1
    Dim i As Int : i = 2
    Msgbox(i, "")

    shows "2"
    No compiler error!

    In Delphi there would be a compiler error! Whats this?
     
  2. Peter Simpson

    Peter Simpson Expert Licensed User

    It is possible but why would you want to do that? You should really do the following.
    Code:
    'Like this:
         Dim i As Int = 1
    'Do something followed by
         i = 2
         
    Msgbox(i, "")

    shows 
    "2"
     
  3. udg

    udg Expert Licensed User

    Hi welu1805,
    consider that Basic is not Pascal. If you're used to Wirth's rule of declaring (in that order) labels, constants, types, variables, procedures and functions even Delphi doesn't strictly adhere to it.
    In B4A, dimming a variable is like declaring it. So dimming it twice is sort of re-declaring it where first instance is left to the garbage collector to be collected for eventual working memory reuse.

    udg
     
    Last edited: Jan 19, 2015
    RandomCoder, DonManfred and Cableguy like this.
  4. welu1805

    welu1805 Active Member Licensed User

    I found an example like this:

    'Initialize the panels we use for the pages and put them in the container
    container.Initialize
    For i = 0 To 5
    Dim pan As Panel

    Select i
    Case 0
    pan = CreatePanel(TYPE_HELLO_WORLD, "Hello World")
    container.AddPage(pan,"Page " & i)
    Case 1
    pan = CreatePanel(TYPE_SETTINGS, "Settings")
    container.AddPage(pan,"Settings")
    Case Else
    pan = CreatePanel(TYPE_LISTVIEW, "ListView " & (i - 1))
    container.AddPage(pan,"ListView " & i)
    End Select
    Next

    In this loop the variable "pan" is declared for 6 times with the same name. Why? Can I declare it one time before the loop?
     
  5. thedesolatesoul

    thedesolatesoul Expert Licensed User

    No you cant. Because you are actually creating 6 different panels. You are re-using ONE variable to reference all 6 panels. Each time you Dim it, it is creating a new panel, which is what we want. Otherwise if you put it outside the loop, you essentially have only ONE variable and ONE panel, and within the loop you keep modifying the same panel.
     
    RandomCoder and Peter Simpson like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice