Re: Vb Global variables

From: Jim Carlock (anonymous_at_127.0.0.1)
Date: 05/16/04


Date: Sun, 16 May 2004 08:42:27 -0400

You'll want to Add a Module, then declare the variable in the
module.

<code for file named Module1.bas>
Public gsPathToData As String
'Global gsPathToData As String
Private msDataFileName As String
</code>

Is that going to be a property for the form or is it a global
variable for the whole project, to be used anywhere in the
project ?

It's been a long time since I've messed with Pascal, ie DOS
programming.

No matter what language you use these days, at least all of the
Microsoft languages that support Windows, there are:
(1) class files (which would be the files that hold the form code,
     typically named frmMain.frm, frmCustomer.frm, etc),
(2) class files (user defined classes, named Class1.cls,
     clsObject.cls, etc)
(3) modules (which are like Pascal include files or C header files
     and these are typically named Module1.bas, Module2.bas,
     modMain.bas, etc)

Then there are some other files that you can add to the project
and they are pretty much self-explanatory once you understand
what's going on with the stuff above.

Hope that helps.

-- 
Jim Carlock
http://www.microcosmotalk.com/
Post replies to the newsgroup.
"Bill" <bill@home.org> wrote in message
news:O7ML2vwOEHA.4036@TK2MSFTNGP12.phx.gbl...
Hi Jim,
    Being new to OOP and the Hungarian method I find dealing with Global
variables extreamly hard with VB6 compared to lets say Pascal 6.  By
reviewing your code snipits I like the second option of the Public variable.
However I have tried to implement this but to no avail.  I really don't know
what section of the form to declare it.  I have tried in the General, Public
Sub Form_Load, and Private Sub Command1_Click().  In my code the variable is
incremented in the Private Sub Command1_Click() section but won't carry the
value outside to...
<code>
Private Sub Label3_Click()
Print gbadcount
End Sub
</code>
for printing to a Label or a TextBox if possible.  Once I get the global
question fixed I will be looking for a way to print the variable to a
specific area of the form to indicate how many line of the logfile were read
and how many were dealt with by URL Scan
"Jim Carlock" <anonymous@127.0.0.1> wrote in message
news:OwcATYvOEHA.3944@tk2msftngp13.phx.gbl...
> Hello Bill,
>
> There are a couple of ways to declare a visible variable.
>
> 1) Using Public properties to reference a localized variable.
> 2) Using a Public variable.
>
> Both of the methods mean that you reference the variable
> via the Form object.
>
> 1) Public Properties method...
>
> <code>
> Private miIndex As Long
>
> Public Property Let Number(ByVal i As Long)
>   miIndex = i
> End Property
> Public Property Get Number() As Long
>   Number = miIndex
> End Property
> </code>
>
> In this case, if the form was named frmCustomer then you
> would reference the variable as follows:
>
> <code>
> 'create new customer-update number
> frmCustomer.Number = i + 1
> </code>
>
> or to reference the number...
>
> <code>
> 'update the customer database with new customer
> rs.Fields("id").Value = frmCustomer.Number
> </code>
>
> 2) The same thing could be implemented via a Public variable.
>
> <code>
> Option Explicit
>
> Public Number As Long
> </code>
>
> The variable would be referenced in the same manner that
> the Properties method works.
>
> The use of the Properties allows you to put some verification
> code if needed into place when you assign a new value to the
> variable.
>
> Hope that helps.
>
> -- 
> Jim Carlock
> http://www.microcosmotalk.com/
> Post replies to the newsgroup.
>
>
> "Bill" <bill@home.org> wrote in message
> news:OY3c2ZtOEHA.540@TK2MSFTNGP11.phx.gbl...
> I am trying create a global variable for a Form.  All the books I have say
> to declare it in the Form section to be used throughout the Form.  Have
also
> declared the Form as Public.
> --------------------------------------------------------------------------
> Public Sub Form_Load()
> Dim gbadcount As Integer
> gbadcount = 0
> End Sub
>
> The routine starts in the Command1_Click()    sub as below code shows.
The
> variable doesn't pass the value outside Command1_Click().  Then I'm trying
> to print the value from gbadcount to either a Label or TextBox in a
specific
> place on the form or in the textbox....how can I fix this?
>
> Bill
> -------------------------------------------------------------------------
> Private Sub Command1_Click()
> Dim InStream As TextStream
> Dim OutStream As TextStream
> Dim testfile As String
> Dim count As Integer
>
> Dim tline, txt, fs
>
>     Rem testfile = DriveSelect & File1_Click
>     testfile = "a:\bad.log"
>     outfile = "c:\test2.log"
>
>
>     Set FSys = CreateObject("scripting.filesystemobject")
>     Set InStream = FSys.OpenTextFile(testfile, 1, False, False)
>     Set OutStream = FSys.OpenTextFile(outfile, 2, True, False)
>     count = 0
>     'badcount = 0
>
>     While InStream.AtEndOfStream = False
>        count = count + 1
>        tline = InStream.ReadLine
>        txt = tline
>        'If InStr(1, txt, "#", vbTextCompare) Then OutStream.WriteLine
(txt)
>        If InStr(1, txt, "/<Rejected-By-UrlScan>", 0) Then
>        OutStream.WriteLine (txt)
>        gbadcount = gbadcount + 1
>        REM print routine here
>        Else
>        End If
>        Set txt = Nothing
>
>     Wend
>     MsgBox "Your logfile has been processed.", vbExclamation, "LogFile
> Status"
>
> End Sub
> --------------------------------------------------------------------------
--
> ---
>
>
>


Relevant Pages

  • MAPI Emails from Access
    ... I realize this code is quite long, but could someone take a look at the sub ... Private Const mcERR_DOH = vbObjectError + 10000 ... Private mstStatus As String ... Dim db As Database, rs As Recordset ...
    (microsoft.public.access.formscoding)
  • Re: set fontsize with variable
    ... Public Function MsgBoxDLL(Optional strPrompt As String, ... Private m_Prompt As String ... Public Property Let Prompt ... Private Sub cmdButton1_Click ...
    (microsoft.public.vb.general.discussion)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: FileSystemWatcher advice required please
    ... Private ArchiveImport As String ... Private FilesToProcess As ProcessFiles ... Public Sub Main ... Dim NoVersion As New Collection ...
    (microsoft.public.dotnet.framework)
  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)

Loading