Results 1 to 21 of 21

Thread: [RESOLVED] Embarass to Ask but Here Goes

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Resolved [RESOLVED] Embarass to Ask but Here Goes

    I have a variable that I set in a text change procedure. How can I use this same variable and its value in a different procedure?

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Embarass to Ask but Here Goes

    Declare it modular, at the top, not inside the Sub.

    Code:
    Option Explicit
    
    Private YourVarName as Something
    
    ...
    Another possible way would be sending the variable as parameter, but that's only possible if you're calling the other Sub from inside that TextBox Change event.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    Thanks jcis for the response. By using the option explicit do I have to initially declare all variables?

  4. #4
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,960

    Re: Embarass to Ask but Here Goes

    Quote Originally Posted by papafrancisco
    Thanks jcis for the response. By using the option explicit do I have to initially declare all variables?
    yes... or visual basic will give you the right error...
    if you don't use it the variables used will be variant type...
    VB6 2D Sprite control

    To live is difficult, but we do it.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    Maybe I'm dreaming but didn't the older quick basic allowed a variable to be used or tested anywhere?

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Embarass to Ask but Here Goes

    Don't know bout that, but anyway that's not the case in VB.
    It prevents infliction of variables.
    Basically a feature to give you at least one less possible error in your code.

    Also it's a little step closer to the scoping of variables in more advanced languages.
    Last edited by TheBigB; May 16th, 2008 at 05:30 PM.
    Delete it. They just clutter threads anyway.

  7. #7
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Embarass to Ask but Here Goes

    'Option Explicit' can be "put" there by default by ticking the Tools >> Options... >> Require Variable Declaration option.
    Quote Originally Posted by joaquim
    ... if you don't use it the variables used will be variant type...
    ???

    Variables are ALWAYS treated as Variants if declared liked this (if data type is not specified):
    Code:
    Dim x
    On the other hand:
    Code:
    Dim y As someType 'Integer, Long, String...
    That's got nothing to do with 'Option Explicit'. It just means that this:
    Code:
    Private Sub Form_Load()
        x = 105
    End Sub
    ... will throw an error (if 'Option Explicit' is used and 'x' is not declared).

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Embarass to Ask but Here Goes

    Btw... making your var Public and putting it into a Module will allow you to access it from any point in your Project.

  9. #9
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,960

    Re: Embarass to Ask but Here Goes

    Quote Originally Posted by papafrancisco
    Maybe I'm dreaming but didn't the older quick basic allowed a variable to be used or tested anywhere?
    look these:
    if you declare a variable in declare position like this:
    Code:
    dim varname as vartype
    these variable will use in every place in that form, module, class... nothing more...
    if you declare in these way in same position:
    Code:
    public varname as vartype
    these variable will be used in every project...
    but if you use in a procedure or a function you can only use it there...
    with other words:
    the dim keyword is use for local variable, but public is for global variable.
    i hope help you
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,960

    Re: Embarass to Ask but Here Goes

    Quote Originally Posted by gavio
    'Option Explicit' can be "put" there by default by ticking the Tools >> Options... >> Require Variable Declaration option. ???

    Variables are ALWAYS treated as Variants if declared liked this (if data type is not specified):
    Code:
    Dim x
    On the other hand:
    Code:
    Dim y As someType 'Integer, Long, String...
    That's got nothing to do with 'Option Explicit'. It just means that this:
    Code:
    Private Sub Form_Load()
        x = 105
    End Sub
    ... will throw an error (if 'Option Explicit' is used and 'x' is not declared).
    the "Require Variable Declaration option" or "'Option Explicit" is for detect if the variable was declared or not and if it's not then gives you an error...
    if you don't give a vartype to a variable, then the vartype will be variant
    VB6 2D Sprite control

    To live is difficult, but we do it.

  11. #11
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Embarass to Ask but Here Goes

    Quote Originally Posted by gavio
    ???
    I don't know if it's true, but I think what was meant is;
    If you don't use Option Explicit, and you run your code, you will note that x is declared as variant

    exactly, what you said, joaquim ^
    Delete it. They just clutter threads anyway.

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    Look at this simple code:

    Public Sub Command1_Click()
    If test = "1" Then
    x = 1
    End If
    End Sub

    Public Sub Text1_Change()
    Dim test As String
    test = Text1.Text
    End Sub

    x will always = 0 because test = empty in the sub command_click. What am I missing here for test to equal what was entered via the textbox?

  13. #13
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Embarass to Ask but Here Goes

    you should define test above the rest of the code
    example:
    Code:
    Public test As String          '// Might as well be dim if you only us it in this form
    
    Private Sub Command1_Click()
    If test = "1" Then
    x = 1
    End If
    End Sub
    
    Private Sub Text1_Change()
    test = Text1.Text
    End Sub
    But a more efficient version of this achievement is:
    Code:
    Private Sub Command1_Click()
    If Text1.Text = "1" Then
    x = 1
    End If
    End Sub
    Delete it. They just clutter threads anyway.

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    uhmm... I added the line of code you suggested however now the variabe test = "" under Sub Command_Click . Guess I still haven't grasp this yet...

    Public test As String

    Public Sub Command1_Click()
    rer:
    If test = "1" Then
    x = 0
    End If
    End Sub

    Public Sub Text1_Change()
    Dim test As String
    test = Text1.Text
    End Sub

  15. #15
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Embarass to Ask but Here Goes

    If I've missed this point somewhere in this thread then I apologize for repeating it.
    Somehow, the paramount benefit of using Option Explicit was not stated! Consider the following code as a classic example of how Option Explicit will save you much aggravation, especially as your projects grow!:
    Code:
    ' This Option Explicit demo contains an intentional error used to demonstrate the
    ' advantage of explicit declarations. Put two CommandButtons & two TextBoxes on your
    ' Form and run the code and click Command1 and then Command2. You should receive an
    ' error "Variable not defined". VB will highlight the error object for you. Now Rem
    ' Option Explicit and repeat the previous steps. When you click Command2, nothing
    ' will happen, demonstrating that Option Explicit IS our friend! In a large app a typo can
    ' take forever to find.
    
    Option Explicit                      ' Rem (') this and see what happens!
    Dim strMyText As String              ' Declare variable
    
    Private Sub Command1_Click()
       Text1.Text = strMyText
    End Sub
    
    Private Sub Form_Load()
       strMyText = "Option Explicit is our friend"     'Initialize variable
    End Sub
    
    Private Sub Command2_Click()
       Text2.Text = stMyText              ' Variable has a typo!
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  16. #16

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    Let me try a different approach. Entering a 1 into the procedure Sub Text1_Change how can I have the sub Command1_Click see the variable test as being equal to 1? Or in other words how can I make a variable available to all procedures?

    Public Sub Command1_Click()
    If test = "1" Then
    x = 1
    End If
    End Sub

    Public Sub Text1_Change()
    Dim test As String
    test = Text1.Text
    End Sub

  17. #17
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Embarass to Ask but Here Goes

    [QUOTE=papafrancisco] how can I make a variable available to all procedures?

    Declare it in the Declarations section as I did in my Option Explicit demo (post 15) .
    Last edited by CDRIVE; May 16th, 2008 at 09:22 PM. Reason: Correction
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  18. #18
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Embarass to Ask but Here Goes

    Code:
    Option Explicit
    Dim strTest As String
    Dim intX As Integer
    
    Private Sub Command1_Click()
       strTest = Text1.Text
          If strTest = "1" Then
             intX = 1
             MsgBox "intX = " & intX
          End If
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  19. #19
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Embarass to Ask but Here Goes

    Quote Originally Posted by papafrancisco
    Let me try a different approach. Entering a 1 into the procedure Sub Text1_Change how can I have the sub Command1_Click see the variable test as being equal to 1? Or in other words how can I make a variable available to all procedures?

    Public Sub Command1_Click()
    If test = "1" Then
    x = 1
    End If
    End Sub

    Public Sub Text1_Change()
    Dim test As String
    test = Text1.Text
    End Sub
    Code:
    Dim test As String              '// It's supposed to be here, above all functions and subs
    Public Sub Command1_Click()
    If test = "1" Then
    x = 1
    End If
    End Sub
    
    Public Sub Text1_Change()
    Dim test As String                    '// This line doesn't go here!
    test = Text1.Text
    End Sub
    Delete it. They just clutter threads anyway.

  20. #20
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Embarass to Ask but Here Goes

    Why keep the Text1_Change sub? In the poster's code it really doesn't do anything. I refer to post 18 where it's eliminated altogether.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    63

    Re: Embarass to Ask but Here Goes

    You're right CDrive. That line of code does nothing. I just created a simple code to express/convey what I was asking.

    TheBigB... That's the ticket! Thanks to all for the help. This is one of the best forums!

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