Results 1 to 17 of 17

Thread: declaring public varibles

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    Angry declaring public varibles

    I'm having problems with a public variable that won't hold its value between functions. I've done this before and can't figure out why it doesn't work.

    Code:
     Private Sub btnCategoryAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCategoryAdd.Click
            tbCatText.Visible = True
            btnCategoriesSubmit.Visible = True
    
    
            btnCategoriesSubmit.Enabled = True
            categoryoperation = "Add"
    End Sub
    categoryoperation is declared as
    Code:
    Public categoryoperation as string
    its placed underneath the the are where the name of the form elements are declared.

    the function that uses this variable is
    Code:
     Private Sub btnCategoriesSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCategoriesSubmit.Click
            If categoryoperation = "Add" Then
                AddCategory()
            End If
    
            CleanUPCat()
    
        End Sub
    after categoryoperation's value is set to "Add" in the first function, it goes to nothing in the second, any help would be appreciated
    Thanks
    Mike

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try accessing it with Me keyword , though it shouldn't make difference

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    What you are saying is not possible. Perhaps something is happening to categoryoperation somewhere. Try

    VB Code:
    1. Private Sub btnCategoriesSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCategoriesSubmit.Click
    2.         If trim(categoryoperation) = "Add" Then
    3.             AddCategory()
    4.         End If
    5.  
    6.         CleanUPCat()
    7.  
    8.     End Sub

    If this fails, put a breakpoint at End Sub of the
    btnCategoryAdd_Click event and check the value of categoryoperation. Also, with a long name like that, check your spelling wherever it occurs.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Don't crucify me for what I'm going to say, please, but I have not time to have a check at the moment (and my english, generally, is not enough to understand and to explain properly what I need to)

    If I well remember it's possible to choose if your variables must be explicitly declare or not, with 'option explicit' option. I use always Option Explicit on and so I need to declare everything. I'm tryng to imagine what could happen if, with Option explicit off, I assign a value to a variable, without 'FrmName.' before the variable name. Could it be created at the moment, I presume as an object variable? Our friend Micaelp99, have Option Explicit On, or not? If it was set to on and if the variable is well declared in the form, should not Micaelp99 to be able to see his variable after he has typed 'Me.' ? I don't know if the problem he is facing is related to this situation, but I promise to myself to test the situation, at least, for my curiosity..... if someone will not give me the answer before, obviously!
    Live long and prosper (Mr. Spock)

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi alextyx,

    You are quite right to mention Option Explicit. I had assumed that michaelp99 had left his application with the default setting, i.e. Option Explicit On.

    If not, then he would be advised to set it to On and see what errors that throws up.


    Hi michaelp99,

    On re-reading your post I am not quite sure what you mean by

    "its placed underneath the the are where the name of the form elements are declared."

    To make it clear, you place the cursor at the end of the Inherits line: press Enter and then type in your Public declaration

    VB Code:
    1. Public categoryoperation as string

    then press enter.

    This places the declaration BEFORE the

    #Region " Windows Form Designer generated code "
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi Taxes, Hi Michaelp99,
    I had the same doubt, regarding the description of the position of the variable declaration, but thought it was only a my difficulty, due to a language misunderstanding!
    Step by step, surely the mistery will be unvealed!

    Example of valid declaration position, if useful:

    Public Class FrmRientri
    Inherits System.Windows.Forms.Form

    Dim FlagEsci As Boolean
    Dim FlagBusy As Boolean
    Dim FlagPrimo As Boolean
    Dim FlagPrecedente As Boolean
    Dim FlagSuccessivo As Boolean
    Live long and prosper (Mr. Spock)

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I agree with taxes with this difference: I would break the program, and put a 'watch' on the variable. that way, you can monitor the variable as it get altered. Use f8 and step through the code. This will give you a more precise measurement as to when the variable is accessed.

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "Use f8 and step through the code. "


    I've been waiting for your to edit this and change F8 to F10 or F11
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    why would I do that? I use f8. I suppose you could use the others but i've never used them.

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Andy,


    Well. Are you sure? F8 certainly does NOT work in my version of VB.NET 2003. Only in my VB6.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    First of all, Thanks for all of your replies, you've suggested a lot of things i wouldn't have even looked at.


    For some reason, It still won't hold the variable's value b/w functions.

    I've tried moving the public declaration to the correct area.

    I've also turned Option Explicit on (it was probably on by default but now its definitly on )

    I've tried declaring the variable locally and transferring it via me.context.items.add to the next function.

    In case I'm missing something here's the complete code:


    Code:
    Option Explicit On 
    
    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.Drawing
    
    
    
    
    Public Class AddEditKB
    
        Inherits System.Web.UI.Page
        Dim boolcatAdd As Boolean = False
    
    
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
        Protected WithEvents Label3 As System.Web.UI.WebControls.Label
        Protected WithEvents Label4 As System.Web.UI.WebControls.Label
        Protected WithEvents Label5 As System.Web.UI.WebControls.Label
        Protected WithEvents lbSolutions As System.Web.UI.WebControls.ListBox
        Protected WithEvents lbProblems As System.Web.UI.WebControls.ListBox
        Protected WithEvents lbCategories As System.Web.UI.WebControls.ListBox
        Protected WithEvents ddMain As System.Web.UI.WebControls.DropDownList
        Protected WithEvents Button2 As System.Web.UI.WebControls.Button
        Protected WithEvents Button3 As System.Web.UI.WebControls.Button
        Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
        Protected WithEvents Textbox2 As System.Web.UI.WebControls.TextBox
        Protected WithEvents btnProblemEdit As System.Web.UI.WebControls.Button
        Protected WithEvents btnProblemDelete As System.Web.UI.WebControls.Button
        Protected WithEvents btnProblemAdd As System.Web.UI.WebControls.Button
        Protected WithEvents btnSolutionAdd As System.Web.UI.WebControls.Button
        Protected WithEvents btnSolutionEdit As System.Web.UI.WebControls.Button
        Protected WithEvents btnSolutionDelete As System.Web.UI.WebControls.Button
        Protected WithEvents btnCategoryAdd As System.Web.UI.WebControls.Button
        Protected WithEvents btnCategoryEdit As System.Web.UI.WebControls.Button
        Protected WithEvents btnCategoryDelete As System.Web.UI.WebControls.Button
        Protected WithEvents Button14 As System.Web.UI.WebControls.Button
        Protected WithEvents Button15 As System.Web.UI.WebControls.Button
        Protected WithEvents chkDisplayAll As System.Web.UI.WebControls.CheckBox
        Protected WithEvents btnCategoriesSubmit As System.Web.UI.WebControls.Button
        Protected WithEvents tbCatText As System.Web.UI.WebControls.TextBox
        Protected WithEvents lblCatError As System.Web.UI.WebControls.Label
        Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
        Protected WithEvents chkassignprob As System.Web.UI.WebControls.CheckBox
        Protected WithEvents ckAssignSol As System.Web.UI.WebControls.CheckBox
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    
    
        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
    
                Dim soft As String
    
    
                Dim conKB As SqlConnection
                Dim cmdKB As SqlCommand
                Dim dtrKB As SqlDataReader
    
                Dim cmdKnow As SqlCommand
    
    
                Dim strSQL As String
    
                Dim var As String
    
    
                conKB = New SqlConnection("Server=mpowell;UID=sa;PWD=sa;Database=WCSITServerSQL")
    
    
                strSQL = "Select SoftwareName from tblSoftware order by SoftwareName"
                cmdKB = New SqlCommand(strSQL, conKB)
                conKB.Open()
    
                dtrKB = cmdKB.ExecuteReader
                dtrKB.Read()
    
                ddMain.DataSource = dtrKB
                ddMain.DataTextField = "SoftwareName"
                ddMain.DataBind()
    
    
    
                dtrKB.Close()
            End If
    
        End Sub
    
        Private Sub ddMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddMain.SelectedIndexChanged
            FillCategories()
    
        End Sub
        Function FillCategories()
    
            Dim conKB As SqlConnection
            Dim cmdKB As SqlCommand
            Dim dtrKB As SqlDataReader
    
            Dim cmdKnow As SqlCommand
    
    
            Dim strSQL As String
    
            Dim var As String
    
    
            conKB = New SqlConnection("Server=mpowell;UID=sa;PWD=sa;Database=WCSITServerSQL")
    
            If chkDisplayAll.Checked = True Then
                strSQL = "Select CategoryName from tblKBCategory"
            Else
                strSQL = "Select tblKBCategory.CategoryName From tblKB, tblKBSoft, tblKBCategory Where tblKBCategory.CategoryID = tblKB.CategoryID And tblKB.KBID = tblKBSoft.KBID AND tblKBSoft.SoftName = @SoftName"
            End If
    
            cmdKB = New SqlCommand(strSQL, conKB)
            var = ddMain.SelectedItem.Text
            cmdKB.Parameters.Add(New SqlParameter("@SoftName", var))
            conKB.Open()
    
            dtrKB = cmdKB.ExecuteReader
    
    
            lbCategories.DataSource = dtrKB
            lbCategories.DataTextField = "CategoryName"
            lbCategories.DataBind()
            conKB.Close()
    
    
        End Function
    
    
           Private Sub btnCategoryAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCategoryAdd.Click
            'Dim boolCatAdd As Boolean = False
            tbCatText.Visible = True
            btnCategoriesSubmit.Visible = True
    
    
            btnCategoriesSubmit.Enabled = True
    
            boolCatAdd = True
            ' Me.Context.Items.Add("boolCatAdd", boolCatAdd)
    
    
    
    
    
        End Sub
    
        Private Sub chkDisplayAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDisplayAll.CheckedChanged
            FillCategories()
        End Sub
    
        Private Sub btnCategoriesSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCategoriesSubmit.Click
            'Dim boolCat As Boolean
    
            'boolcatAdd = Me.Context.Items.Item("boolCatAdd")
    
            If boolcatAdd = True Then
    
                AddCategory()
            End If
    
            CleanUPCat()
    
        End Sub
        Function AddCategory()
            Dim conKB As SqlConnection
            Dim cmdKB As SqlCommand
            Dim dtrKB As SqlDataReader
            Dim cmdAdd As SqlCommand
            Dim sqlAdd As String
    
            Dim cmdKnow As SqlCommand
            Dim boolAddError As Boolean = True
    
    
    
            Dim strSQL As String
    
            Dim var As String
            Dim prob As String
    
    
            conKB = New SqlConnection("Server=mpowell;UID=sa;PWD=sa;Database=WCSITServerSQL")
            strSQL = "Select CategoryName from tblKBCategory"
            cmdKB = New SqlCommand(strSQL, conKB)
            conKB.Open()
    
            dtrKB = cmdKB.ExecuteReader
    
    
    
    
            While dtrKB.Read
                If tbCatText.Text = "" Then
                    lblCatError.Text = "Category Entry Field Cannot Be Empty"
                    boolAddError = True
                ElseIf tbCatText.Text = dtrKB("CategoryName") Then
                    lblCatError.Text = "Category Already Exists"
                    boolAddError = True
                Else
                    boolAddError = False
    
    
                End If
    
            End While
            conKB.Close()
            If boolAddError = False Then
    
                'Add a Record
                sqlAdd = "Insert Into tblKBCategory (CategoryName) Values (@Category)"
                cmdKB = New SqlCommand(sqlAdd, conKB)
                cmdKB.Parameters.Add("@Category", tbCatText.Text)
                conKB.Open()
                cmdKB.ExecuteNonQuery()
                conKB.Close()
    
    
    
    
    
                lblCatError.Text = ""
    
    
    
                lblStatus.ForeColor = ColorTranslator.FromHtml("#009900")
                lblStatus.Text = "Records Added Successfully"
    
    
    
            Else
                lblCatError.Text = "No Records Added or Inserted"
                lblStatus.ForeColor = ColorTranslator.FromHtml("#FF0000")
            End If
        End Function
    
        Function CleanUPCat()
            tbCatText.Text = ""
            tbCatText.Visible = False
    
            btnCategoriesSubmit.Visible = False
            FillCategories()
        End Function
    
    
    End Class

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Put breakpoints on the lines

    boolcatAdd=True

    and

    if boolcatAdd = True.

    Check the value of boolcat
    move forward one line
    check the value of boolcat

    then f5

    Check the value of boolcat
    move forward one line
    check the value of boolcat

    You have to find out if and when the value is changing.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    the variables loses its value when the the program starts the new function

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Try declaring booocatadd as public, not private as you have done.

    Also, when you say it "looses it's value" do you mean it changes it's value or does it actually have no value at all?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    it was public originally, and still did the same thing, when i say it loses it i mean its like it was redeclared

  17. #17
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    What new function? Do you mean

    AddCategory()


    If that function actually starts, then the problem is NOT with boolcatadd.

    You are going to have to step through your code from a breakpoint at

    boolCatAdd = True

    moving one line at a time and checking the value of boolcatadd at each stage. You will have to use F11 to step through each line if you are crossing End Sub.

    Not that it will affect matters now, but I notice that you did not spell boolcatadd correctly in

    Private Sub btnCategoriesSubmit
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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