Link to home
Start Free TrialLog in
Avatar of sticar
sticar

asked on

Error: "C:\Label\MakeLabel.vb(149): Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

I'm getting this error and I'm not sure why, haven't used vb.net much:

"C:\Label\MakeLabel.vb(149): Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."


Class MakeLabel
    Inherits System.Windows.Forms.Form
+ windows form deseigner generated code
    Public Const projectname as String = "myProject"
    Public strLabelName As String = txtLabel.Text

Shared Sub Main()
   I can't access strLabelName here, this is where I get the error.
End Sub

Shared Sub btnUpdate_Click()
    Main()
End Sub

End Class
SOLUTION
Avatar of gregspence
gregspence

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arif_eqbal
arif_eqbal

Well the whole thing is summed up in the First Line by gregspence

A Shared Sub or Function means that it can be called without an instance of that object created in memory.

Now that it can be called without an instance in memory how can you expect it to access an instance member (property/method etc.)  So a shared member can only access another shared member...

Avatar of sticar

ASKER

Ok.  I kinda get it now.  Just didn't think I had to make a 'new' object.  After I added that, it ran, but my form never came up.  I have a form that you enter some text into, then click update to run the main section.  Not sure why?

Shared Sub Main()
   Dim M As New MakeLabel
   M.strLabelName = M.txtLabel.Text
   Application.Run(M)            'Not sure why I have this either... ?
End Sub

Shared Sub btnUpdate_Click()
    Main()    'I need main to run after I click update, but I'm not getting my form to pop up.
End Sub
Avatar of sticar

ASKER

Ok, got my form to come up this way.  Except there is always nothing in my txtLabel.  I type something in it then click update but it comes up blank.

    Shared Sub Main()
        Dim M As New MakeLabel
        Application.Run(M)
    End Sub

And put evertying from Main, under my update click event...

Shared Sub btnUpdate_Click()
        Dim M As New MakeVSSDailyLabel
        M.strLabelName = M.txtLabel.Text
End Sub
First of all,

Is the _Click actually handling the event?!  i.e. AddHandler btnUpdate.Click, AddressOf btnUpdate_Click or Shared Sub btnUpdate_Click() Handles btnUpdate.Click

You could check this by simply adding MessageBox.Show("Test") to the click event subroutine!

If that's all working ok then...

What is supposed to be displayed in the label?  If you are trying to populate it with the value of "strLabelName" then you have it the wrong way around.  You'll need it to be M.txtLabel.Text = M.strLabelName

I hope this is what you wanted and it helps.

Tim
Avatar of sticar

ASKER

This is my full btnupdate event
I now have the following, but the label is still = nothing.

Shared Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim M As New MakeVSSDailyLabel
        M.txtLabel.Text = M.strLabelName
End Sub
Avatar of sticar

ASKER

I'm stepping through the code, so the click event happens, but the label is just "" null.
Avatar of sticar

ASKER

I want the value from the form that is typed into txtLabel to be = to strLabelName
Avatar of sticar

ASKER

Ok, I think that last line was confusing.  Let me try one more time.  I need to capture the value that I type on the form into the txtLabel field.  Then I want to set strLabelName = txtLabel.text (what is on the form).
It's hard to say without seeing your entire code but I assume that "MakeVSSDailyLabel" is the current form?

i.e. the Label you are refering to exists in the same form.

If so, simply change your code to

Me.txtLabel.Text = Me.strLabelName

If you could post the code for the whole document it would help!

Thanks,
Tim
Avatar of sticar

ASKER

It doesn't like Me. because it is shared.

This is it.

Imports SourceSafeTypeLib
Imports Microsoft.VisualBasic
Imports System.Windows.Forms

Class MakeVSSDailyLabel
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents CO As System.Windows.Forms.Label
    Friend WithEvents txtLabel As System.Windows.Forms.TextBox
    Friend WithEvents btnUpdate As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnUpdate = New System.Windows.Forms.Button
        Me.CO = New System.Windows.Forms.Label
        Me.txtLabel = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'btnUpdate
        '
        Me.btnUpdate.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnUpdate.Location = New System.Drawing.Point(104, 80)
        Me.btnUpdate.Name = "btnUpdate"
        Me.btnUpdate.TabIndex = 0
        Me.btnUpdate.Text = "Update"
        '
        'CO
        '
        Me.CO.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.CO.Location = New System.Drawing.Point(48, 36)
        Me.CO.Name = "CO"
        Me.CO.Size = New System.Drawing.Size(40, 23)
        Me.CO.TabIndex = 1
        Me.CO.Text = "Label:"
        '
        'txtLabel
        '
        Me.txtLabel.Location = New System.Drawing.Point(96, 32)
        Me.txtLabel.MaxLength = 20
        Me.txtLabel.Name = "txtLabel"
        Me.txtLabel.TabIndex = 2
        Me.txtLabel.Text = ""
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(8, 128)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(256, 56)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Please enter the CO# above.  When you click ""Update"", VSS will be updated with a " & _
        "label for every file ex: Labeled 'YYYY-MM-DD CO12345' The date is added automati" & _
        "cally."
        '
        'MakeVSSDailyLabel
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(272, 198)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.txtLabel)
        Me.Controls.Add(Me.CO)
        Me.Controls.Add(Me.btnUpdate)
        Me.Name = "MakeVSSDailyLabel"
        Me.Text = "Main"
        Me.ResumeLayout(False)

    End Sub

#End Region
    '———————————————————————————
    '— SourceSafe constants
    '———————————————————————————
    '— The SourceSafe root project.
    Public Const cVSSRootProject As String = "$/Dollar_Reloaded"

    '— The SourceSafe INI file.
    Public Const cSourceSafeINI As String = "c:\VSSDatabase\srcsafe.ini"

    '— The SourceSafe User ID/Password.
    Public Const cVSSUserName As String = "VSS_Labeler"
    Public Const cVSSPassword As String = "label2005"
    Public Shared strLabelName As String

    'Public Shared Property sharedLabel() As String
    '    Get
    '        If MakeVSSDailyLabel Is Nothing OrElse GlobalForm.IsDisposed Then
    '            GlobalForm = New MakeVSSDailyLabel
    '        End If
    '        Return GlobalForm.txtLabel.Text
    '    End Get
    '    Set(ByVal Value As String)
    '        GlobalForm.txtLabel.Text = Value
    '    End Set
    'End Property

    Shared Sub Main()
        Dim M As New MakeVSSDailyLabel
        Application.Run(M)
    End Sub

    Shared Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'strLabelName = MakeVSSDailyLabel.sharedLabel
        Dim M As New MakeVSSDailyLabel
        M.strLabelName = M.txtLabel.Text
        Dim objSourceSafe As SourceSafeTypeLib.VSSDatabase
        Dim objVSSProject As VSSItem
        Dim strProject As String

        '------------------------------------------------------
        '--- Create the SourceSafe ActiveX object.
        '------------------------------------------------------
        objSourceSafe = New SourceSafeTypeLib.VSSDatabase
        '------------------------------------------------------
        '--- Open a connection to the SourceSafe database
        '--- using the srcsafe.ini file and a  valid
        '--- userid/password combination.
        '------------------------------------------------------
        objSourceSafe.Open(cSourceSafeINI, cVSSUserName, cVSSPassword)
        '------------------------------------------------------
        '--- Create the project path of
        '------------------------------------------------------
        strProject = cVSSRootProject
        '------------------------------------------------------
        '--- Set the project for the database script files.
        '--- (Project is expected to be something like:
        '--- $/RootProject/SubProject)
        '------------------------------------------------------
        On Error Resume Next
        objVSSProject = objSourceSafe.VSSItem(strProject)
        '------------------------------------------------------
        '--- If the project does not exist...
        '------------------------------------------------------

        On Error GoTo 0
        '------------------------------------------------------
        '--- Check the files into SourceSafe.
        '------------------------------------------------------
        Dim sLabel As String
        sLabel = Microsoft.VisualBasic.Format(DateTime.Now, "yyyy-MM-dd") & " " & strLabelName
        objVSSProject.Label(sLabel, "CO Label Update")
        objVSSProject = Nothing
        objSourceSafe = Nothing
        MsgBox("Complete")
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sticar

ASKER

I was getting an error before when I was trying to access the Main sub so I added Shared to everyting, but I took it out. And added the Me.txtLabel.Text and it worked!

Thanks!
No problem, glad I could help! :)
Whew, I was just about to post some new code, but I see you got it working, great!