Results 1 to 6 of 6

Thread: Global variables

  1. #1

    Thread Starter
    Lively Member ahmedcrow's Avatar
    Join Date
    Jul 2017
    Posts
    82

    Global variables

    How can I declare Global and Public variables like in VB6 in VB.Net ?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Global variables

    The same way you do in VB6.

    If you want a variable to be available everywhere in the application without having to refer to a class, declare it in a module (using either Public or Global).

    Often it makes sense to declare it in a class (such as your main form), in which case declare it there (using Public).


    Note however that while it is sometimes useful to have variables available everywhere, there tend to be better alternatives in most cases, but as the list is long it is best to ask about specific situations rather than us try to list them all.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Global variables

    As an example of what si is talking about, many beginners will tend to use a global variable so that they can set it in one form and get it in another. That will get the job done but it's not best practice. If you want to get data from one for to another then the one that knows about the other should either push data to the other one or pull data from the other one.

    It might be a good idea for you to follow the Blog link in my signature below and check out my three-part post on Data Among Multiple Forms. It starts with global variables and works up to the proper way to do it.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Global variables

    Hi JMC and sorry ahmed,

    if I jump with a question into this thread, but it might belong here.

    I haven't got around to converting some VB6 Code to .Net.
    the VB6 code (see at the end) I use starts in a MDI Form
    is executes 3 Forms:
    a) start the Search (Form for entering criteria)
    b) return the results from that criteria (Form with Listview), and allow the user to pick one
    c) finally open the last Form with the selected item from the Listview

    VB6 Code
    Code:
    Private Sub mnuSeSuchen_Click()
      Dim frmSearch As ServiceSuche 'Form for search criteria
      Dim frmList As ServiceListe 'Form results of the criteria entered
      Dim frmKunde As ServiceEdit 'finally the one i picked from the Form -Serviceliste-
      
      Dim objclsKunden As clsServiceDisplayn 'Class for the results returned
      Dim objclsKunde As clsService ' class for
      Set frmSearch = New ServiceSuche
      
      With frmSearch
      'show the criteria Form
        .Show vbModal
        If .OK Then
          Set frmList = New ServiceListe
          Set objclsKunden = New clsServiceDisplayn
          'execute the Class for search
          objclsKunden.Load .ResultBANr, .ResultBAVom, .ResultRepNr, .ResultRepVom, .ResultMaNr, .ResultFirma, .ResultPLZ
          With frmList
          'show the results
            .Component objclsKunden
            .Show vbModal
            If .SE_LfdNr > 0 Then
            'select one from the SearchList
              Set frmKunde = New ServiceEdit
              Set objclsKunde = New clsService
              objclsKunde.Load .SE_LfdNr
              frmKunde.Component objclsKunde
              'open the one I picked
              frmKunde.Show
              Set objclsKunde = Nothing
            End If
          End With
          'close the Form frmList
        Unload frmList
    Set frmList = Nothing
    Set objclsKunde = Nothing
        End If
      End With
      'close the form frmSearch
      Unload frmSearch
      Set frmSearch = Nothing
    End Sub
    would you work the same way in .Net to interact with 3 Forms to get the final result?


    regards
    Chris
    Last edited by ChrisE; Feb 23rd, 2018 at 06:18 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Lively Member ahmedcrow's Avatar
    Join Date
    Jul 2017
    Posts
    82

    Re: Global variables

    Quote Originally Posted by si_the_geek View Post
    The same way you do in VB6.

    If you want a variable to be available everywhere in the application without having to refer to a class, declare it in a module (using either Public or Global).

    Often it makes sense to declare it in a class (such as your main form), in which case declare it there (using Public).


    Note however that while it is sometimes useful to have variables available everywhere, there tend to be better alternatives in most cases, but as the list is long it is best to ask about specific situations rather than us try to list them all.
    I've no particular situation, the reason of my question is that I stopped learning programming with QBasic, I want to learn modern tools to show others my programs, I tried to learn VB6 as a modern tool instead of QBasic but in the end I decide to learn more modern one, VB 2010 is work fine on my PC, I've old background in programming in DOS, it's not so simple to me to be familiar with the new visual programming, programs I know in QBasic are using public variables, it's common something to have public variables, I thought it's simple in VB.Net, so that I asked, I want to have all old features I know to start practice in VB.Net.

  6. #6

    Thread Starter
    Lively Member ahmedcrow's Avatar
    Join Date
    Jul 2017
    Posts
    82

    Re: Global variables

    Quote Originally Posted by jmcilhinney View Post
    As an example of what si is talking about, many beginners will tend to use a global variable so that they can set it in one form and get it in another. That will get the job done but it's not best practice. If you want to get data from one for to another then the one that knows about the other should either push data to the other one or pull data from the other one.

    It might be a good idea for you to follow the Blog link in my signature below and check out my three-part post on Data Among Multiple Forms. It starts with global variables and works up to the proper way to do it.
    I'll read your blog, thanks.

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