?
Solved

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."

Posted on 2005-03-21
15
Medium Priority
?
397 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:sticar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 1

Assisted Solution

by:gregspence
gregspence earned 400 total points
ID: 13596906
A Shared Sub or Function means that it can be called without an instance of that object created in memory.  The rule is that a shared sub can only access other shared properties or subs.

A Non-shared sub must be called like this:
Dim M as New MakeLabel
M.Main

A Shared sub can be called like this.
MakeLabel.Main

In your case, you need a Shared Sub Main as the entry point to your application. so making it NOT shared is not an option.

What you need to do is create an instance of your class.  Since you are inheriting Windows.Forms.Form, you can do it like this:

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

This will start a messaging thread with your form and stay active until your form is closed.
If you go this route, you'll need to access your label inside the Class, not inside your Sub Main, like in the New() constructor.

You can also access the members of your form before handing it off to Application.Run like this.

Shared Sub Main()
     Dim M as New MakeLabel
     M.Label1.Text = "This is my label"
     Application.Run(M)
End Sub

Hope this gives you some more direction.

Greg
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13598048
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...

0
 

Author Comment

by:sticar
ID: 13600866
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sticar
ID: 13601011
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
0
 
LVL 4

Expert Comment

by:w3tim
ID: 13601915
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
0
 

Author Comment

by:sticar
ID: 13602000
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
0
 

Author Comment

by:sticar
ID: 13602040
I'm stepping through the code, so the click event happens, but the label is just "" null.
0
 

Author Comment

by:sticar
ID: 13602055
I want the value from the form that is typed into txtLabel to be = to strLabelName
0
 

Author Comment

by:sticar
ID: 13602100
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).
0
 
LVL 4

Expert Comment

by:w3tim
ID: 13602130
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
0
 

Author Comment

by:sticar
ID: 13602160
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
0
 
LVL 4

Accepted Solution

by:
w3tim earned 600 total points
ID: 13602228
Is there any reason why you have the event as a shared object?

It shouldn't need it as the object isn't shared!

The problem is that you are declaring a new instance of the form.  If you want to see the effect of what you are currently doing add in M.Show() and it display the new form you have created probably with the text you added.

Take out the "Shared" from the "Shared Sub btnUpdate_Click" and try that.

Tim
0
 

Author Comment

by:sticar
ID: 13602267
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!
0
 
LVL 4

Expert Comment

by:w3tim
ID: 13602301
No problem, glad I could help! :)
0
 
LVL 1

Expert Comment

by:gregspence
ID: 13602478
Whew, I was just about to post some new code, but I see you got it working, great!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question