Reference to a non-shared member requires an object reference.

A windows update has put paid to my VB6 installation, so I am blundering about trying convert a project to VB.NET
This code
        Dim ch As ColumnHeader

        With ListView1
            ch = .ColumnHeaderCollection.Add("Letter", 1500)
            ch = .ColumnHeaderCollection.Add("Vol Name", 1500)
            ch = .ColumnHeaderCollection.Add("Type", 1500)

Open in new window

gives this error on the last three lines.

Error      BC30469      Reference to a non-shared member requires an object reference.      

I have no idea what is supposed to be shared.

Googling shows some answers, but they all seem to refer to user-written classes, not standard controls.
LVL 77
GrahamSkanRetiredAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
Take a look at this sample
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim lst1 As New ColumnHeader
        Dim lst2 As New ColumnHeader
        Dim lst3 As New ColumnHeader
        Dim ltm1 As New ListViewItem(New String() {"A", "A1"}, -1)
        Dim ltm2 As New ListViewItem(New String() {"", "", "B"})
        Dim ltm3 As New ListViewItem({"", "C", ""})

        lst1.Text = "header1"
        lst2.Text = "header2"
        lst3.Text = "header3"

        Me.ListView1.Columns.AddRange(New ColumnHeader() {lst1, lst2, lst3})
        Me.ListView1.Items.AddRange(New ListViewItem() {ltm1, ltm2, ltm3})
    End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredAuthor Commented:
Hi John
Thanks for that . I am trying to specialise your code to fit my situation
      '...      
        Dim ch1 As New ColumnHeader
        Dim ch2 As New ColumnHeader
        Dim ch3 As New ColumnHeader
        With ListView1
            ch1.Text = "Letter"
            ch1.Width = 1500
            ch2.Text = "Vol Name"
            ch2.Width = 1500
            ch3.Text = "Type"
            ch3.Width = 1500

            .Columns.AddRange(New ColumnHeader(), ch1, ch2, ch3)
 '...

Open in new window

Unfortunately it finds an error on  'New ColumnHeader()':

Error      BC30311      Value of type 'ColumnHeader' cannot be converted to 'ColumnHeader()'.
John TsioumprisSoftware & Systems EngineerCommented:
if you mean here:
.Columns.AddRange(New ColumnHeader(), ch1, ch2, ch3)

Open in new window

then it should be
.Columns.AddRange(New ColumnHeader(), {ch1, ch2, ch3})

Open in new window

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

GrahamSkanRetiredAuthor Commented:
Thank you again, John. Unfortunately, that does not make any difference.
John TsioumprisSoftware & Systems EngineerCommented:
The comma was the issue ...missed it
.Columns.AddRange(New ColumnHeader() {ch1, ch2, ch3})

Open in new window


In order to be clear the offending code is :
Columns.AddRange(New ColumnHeader(), {ch1, ch2, ch3})

Also .NET handles width differently ...so for start just divide the width by a factor of 10
 Dim ch1 As New ColumnHeader
        Dim ch2 As New ColumnHeader
        Dim ch3 As New ColumnHeader
        With ListView1
            ch1.Text = "Letter"
            ch1.Width = 150
            ch2.Text = "Vol Name"
            ch2.Width = 150
            ch3.Text = "Type"
            ch3.Width = 150

            '.Columns.AddRange(New ColumnHeader(), {ch1, ch2, ch3})
            .Columns.AddRange(New ColumnHeader() {ch1, ch2, ch3})
        End With

Open in new window

GrahamSkanRetiredAuthor Commented:
Thanks John. I must get some new glasses.
John TsioumprisSoftware & Systems EngineerCommented:
Probably my glasses also carry the same issue...:) ... i had to carry the code to VS to locate it.
GrahamSkanRetiredAuthor Commented:
Thanks for your very prompt help. I can see my conversion project going to take me a lot longer than I first thought
John TsioumprisSoftware & Systems EngineerCommented:
Also about the issue of twips ...you need to convert them to pixels..here is a post to get your started
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.