Results 1 to 9 of 9

Thread: Variable/String assignment

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Variable/String assignment

    How can I save/assign a string so that I can recall it in different windows? Dim doesn't work and I can't find anything else. Thanks

    Second part of thread split into its own thread here
    Last edited by Hack; Jul 20th, 2009 at 06:23 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable/String assignment

    Quote Originally Posted by crazymao View Post
    How can I save/assign a string so that I can recall it in different windows? Dim doesn't work and I can't find anything else.
    Dim does what it does and nothing else. Dim declares a variable. Passing data to a form is just like passing data to any other object. You would generally need the data assigned to a variable first, and that variable would need to be declared with the Dim key word.

    There's a link in my signature that provides information on passing data between forms.
    Quote Originally Posted by crazymao View Post
    PS: If someone knows a good tutorial on how to make a chat can you please show me? (this has nothing to do with the original question)
    And therefore there's absolutely no reason to post it in this thread. One thread per topic and one topic per thread please. In future please take the extra 10 seconds required to create a new thread for a new question.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member
    Join Date
    Jul 2009
    Posts
    6

    Re: Variable/String assignment

    Use Public in place of Dim.

    For instance, use Public MyString As String

    And if that was called in Form1, you'd refer to it in other forms as Form1.MyString.

    Beaten to it... :P

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Re: Variable/String assignment

    Quote Originally Posted by jmcilhinney View Post
    In future please take the extra 10 seconds required to create a new thread for a new question.
    i'm sorry. i know i should have done that but i thought that people would say that i post too much

    and thanks for your help

    Quote Originally Posted by CaptainDingo View Post
    Use Public in place of Dim.

    For instance, use Public MyString As String

    And if that was called in Form1, you'd refer to it in other forms as Form1.MyString.

    Beaten to it... :P
    thanks but i get an error saying that public is not valid on a local variable declaration
    Last edited by crazymao; Jul 19th, 2009 at 09:53 PM.

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Variable/String assignment

    In a procedure you can only use Dim or Static to declare a variable. If you're wanting to 'save' it's value then you would have to declare it at the class level.

    vb.net Code:
    1. Public Class Form1
    2. 'Declare here
    3. Public s As String
    4.  
    5.    Public Sub SomeMethod()
    6.  
    7.     End Sub
    8.  
    9.  
    10. End Class

    Edit: here's a link to Access Modifiers since they tie together.
    Last edited by ForumAccount; Jul 19th, 2009 at 11:20 PM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Re: Variable/String assignment

    Quote Originally Posted by ForumAccount View Post
    In a procedure you can only use Dim or Static to declare a variable. If you're wanting to 'save' it's value then you would have to declare it at the class level.

    vb.net Code:
    1. Public Class Form1
    2. 'Declare here
    3. Public s As String
    4.  
    5.    Public Sub SomeMethod()
    6.  
    7.     End Sub
    8.  
    9.  
    10. End Class

    Edit: here's a link to Access Modifiers since they tie together.
    ok thanks, but is there a command like that which i can enter into a private sub?

    EDIT: The error i keep getting is: '...' is not valid on local variable declaration. so what i'm looking for is a valid local variable declaration besides Dim which I can use/recall in multiple forms
    Last edited by crazymao; Jul 20th, 2009 at 03:29 PM.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Variable/String assignment

    you can't ... it's a LOCAL variable... if you need to use it outside the sub, then it needs to be declared out side the sub.... further, if it is needed outside the form,, then you need to use Public so that it can be used outside the form....

    -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??? *

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Re: Variable/String assignment

    Quote Originally Posted by techgnome View Post
    you can't ... it's a LOCAL variable... if you need to use it outside the sub, then it needs to be declared out side the sub.... further, if it is needed outside the form,, then you need to use Public so that it can be used outside the form....

    -tg
    i tried that but it doesnt work for what i want to do. i want to make it so that when the user presses a button it saves whatever is in textbox1.text to a variable or string which can be recalled in another form. but i cant do what youre proposing for an action of a button

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Variable/String assignment

    Did you read the link I gave you?


    vb.net Code:
    1. Public Class Form1
    2.       Public MyAccessibleVariable As String = String.Empty
    3.       Public Sub ButtonClick(sender blah blah...) Handles blah
    4.           MyAccessibleVariable = MyButton1.Text
    5.       End Sub
    6. End Class
    7.  
    8. Public Class Form1
    9.       Public Sub SomeMethod()
    10.           'Assuming Form1 is instantiated
    11.           MessageBox.Show(Form1.MyAccessibleVariable)
    12.       End Sub
    13. End Class

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