dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: how to define Public/Global variable

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    how to define Public/Global variable

    [Originally posted by ASHRAF]

    sir,
    I m using VB.NET & SQL Server.

    I want to define a variable in MDI form and want to use that variable in all form modules.
    Can u pls. advise me that how to define that variable.

    Thnaks

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:how to define Public/Global variable

    [Originally posted by bobbynad]

    Create a module and in that module u can declare variable using Global keyword , Hopes U know how to crate a module , if not then in the project explorer window right click and choice option ADD in the category to add click on module.

    Global VAR_BillNo as integer˙

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:how to define Public/Global variable

    [Originally posted by Marcel Slooff]

    If I may be so rude. If I were you I would not use global variables. This is bad practice. Instead you should use a public variable like this:
    Public p_intBillNo As Integer

    In your form code you would refer to this variable in the following manner (assuming your module's name is modModule):
    modModule.p_intBillNo = modModule.p_intBillNo + 1

    All this also works when you add a public variable to the MDI parent (main) form. Since when the application is running the MDI parent is always available. You would then substitute the name of the MDI parent form for the module name and declare the public variable in the MDI parent.

    Regards

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:how to define Public/Global variable

    [Originally posted by allen]

    I don't think you really need to prefix the public variable name with the module name, as long as it is declared in a "module", not a "form".˙ I suppose you would if you used the same public name in two different modules, but why would you such a thing?

  5. #5
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:how to define Public/Global variable

    [Originally posted by Mike Lyons]

    >>All this also works when you add a public variable to the MDI parent (main) form. <<

    I'd like to make a little comment here that although VB allows this, by invisibly creating a property of the form to exose the variable, it's not the optimal programming practice.˙ If you do want to expose a variable contained in a form's module to other parts of your application you should declare the variable as Private and then explicitly create properties to access the variable.˙

    By doing it this way, you can screen an incoming value and determine if it is acceptable for that variable before allowing it to be set, and raise an Invalid Property Value error (the error code escapes me at the moment) if it is not suitable.˙ This error should be caught and dealt with in the code that sets the value (or you should catch and correct this situation during debugging).

    Mike

  6. #6
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:how to define Public/Global variable

    [Originally posted by Marcel Slooff]

    Your right, but it's just good practice to use the module name. Reading the code tells you then exactly where the variable is declared. This might come in handy when you, for some reason, need to change the type from Ineteger to Long. Offcourse you know your own code, now. But after six month and several projects later you might not exactly remember the code. Also I could think of a 'reason' why variables would have the same name, because your module is just a module in a code library, your just importing it in your project, and some code librarys may have public variables with different purposes yet the same name.

    But again your probably right in this specific case.

  7. #7
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:how to define Public/Global variable

    [Originally posted by Marcel Slooff]

    The error code for an invalid property value is 380 but this is besides the point. The point is that the application, including the MDI parent, which is always loaded and visible when your appllication is running, needs to be able to access this variable. If you really would like to do these things the right way you would not even consider using a module or form to declare this variable, instead you would create a class. Then again this is not very praticle if it concerns just one variable and non generic code. Also when there is no restiction to the variables value, except the type itself, why would you not use a public variable. No checks have to be performed.
    Forms should not have to have properties of any kind other then their native ones. Its also not very good practice to treat a form or a module like a class, eventhough they actually are.

    So, eventhough there are some issues to concern I think the question is more then answered.

    Hope you can descide.

    Regards.

  8. #8
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:Re:how to define Public/Glob

    [Originally posted by Greg DeBacker]

    I'll jump in to this lively discussion with a question and a point.

    Question: What is the difference between Public and Global variables. I always understood that Public and Global were the same except that Public was now the preferred way to declare it.

    Point: You had mention that you preface your Public variables name with the name of the module or form it was declared in so you could find it 6 months later after you had lost track of the code. Since you can just select the variable and press Shift+F2 to quickly jump to the declaration this seems like a waste of keystrokes.

    Grex

  9. #9
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:Re:Re:how to define Public/Glob

    [Originally posted by Marcel Slooff]

    Thats true. Perhaps my personal history plays a part in saying that. Programming in other environments and other languages teaches you things that are not always necesary in VB. However there is another reason for doing so, consider the following. You have two modules named modModule1 and modModule2 with the following code in it:
    modModule1
    Option Explicit
    Public intVariable As Integer

    modModule2
    Option Explicit
    Private intVariable As Integer
    Public Function Test() As Integer
    ˙ ˙ Test = intVariable
    End Sub

    Now the question: What variable is returned by the public function Test? I know in this case it will return the value of the private declared variable intVariable. When you want to make sure the function returns the value of the intVariable declared in modModule1 you should use a prefix.

    Now for the difference between Global and Public. When declaring a variable Global its name MUST be unique in the whole project. This means that you can not use the same name for a public or private variable. Why? Because the compiler can't tell them appart anymore since the scope of a Global variable is truely global. The compiler will not generate any errors but the execution of the program will become unpredictable. Now a public variable is 'bound' to the module or form it is declared in. Meaning that you can use the same name over and over again because the name of the variable is MADE unique by the module it is declared in.

    Hope this answers your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center