Results 1 to 11 of 11

Thread: [RESOLVED] Reference to a non-shared member requires an object reference.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Resolved [RESOLVED] Reference to a non-shared member requires an object reference.

    I have this code but i get the error when i call this class.

    what is wrong?

    Code:
    Class Window1
    
        Public Sub tabs_AddTab()
    
        End Sub
    
    End Class
    Code:
    Imports MySystem.Window1
    
    Public Class SideBar
    
        Public Sub PassClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
            MessageBox.Show("yes")
            tabs_AddTab()
        End Sub
    
    End Class

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

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

    You have to have an instance of your Window1 class before you can access any non-shared members of it - so for example you could do:
    Code:
    Public Sub PassClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
            MessageBox.Show("yes")
            Dim NewWindow1 As New Window1
            NewWindow1.tabs_AddTab()
        End Sub
    but I'm guessing the reason you want to call something on your Window1 class is because you want it to affect the existing window that is on the screen (also I assume this is a WPF project?) so you would need to get a reference to the existing Window1 class that is visible on screen rather than creating a new instance. If Window1 is the 'main' window of your application then you can do this to get a reference to that instance:
    Code:
    Dim Window1Ref As Window1 = DirectCast(Application.Current.MainWindow, Window1)
    Then you can call your methods like so:
    Code:
    Window1Ref.tabs_AddTab()
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

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

    Thanks heaps, this works.

    If you have time can you explain the following

    1. What is direct cast and what does it do?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

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

    Oh yeah sorry I forgot to mention that bit - it is for 'casting' an object to a specific type.
    For you to be able to access methods/properties in a specific class, you have to be working with an object that is declared as that type. In your scenario, the class/type we were wanting to access methods from was your Window1 class but this code does not give us a Window1 object, it just gives us a Window object:
    Code:
    Application.Current.MainWindow
    but we know that your main window is actually of type Window1, so we can safely cast it to that type by using DirectCast:
    Code:
    DirectCast(Application.Current.MainWindow, Window1)
    If we did not do this then we would not be able to access your tabs_AddTab method because that does not exist on the standard Window class.

    That is probably not the best description but I'm trying to keep it simple - I'm sure if you google DirectCast you will find some other more detailed descriptions
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

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

    Thanks heaps. Very helpful

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    What if it is not the main window?
    Code:
    Dim Window1Ref As Window1 = DirectCast(Application.Current.MainWindow, Window1)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    Quote Originally Posted by AirlineSim View Post
    What if it is not the main window?
    Code:
    Dim Window1Ref As Window1 = DirectCast(Application.Current.MainWindow, Window1)
    If you display the default instance in the first place then you can access the default instance anywhere. You can also use the My.Application.OpenForms collection.

    Finally, you can also use the same option that C# programmers would, who don't have access to all the help that VB provides: you could just maintain your own reference to the form when you create it. That's what you'd have to do with any other objects because VB only provides special handling for forms.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    Quote Originally Posted by jmcilhinney View Post
    If you display the default instance in the first place then you can access the default instance anywhere. You can also use the My.Application.OpenForms collection.

    Finally, you can also use the same option that C# programmers would, who don't have access to all the help that VB provides: you could just maintain your own reference to the form when you create it. That's what you'd have to do with any other objects because VB only provides special handling for forms.
    Well I must say i'm really lost, before on any other project I would have hone

    tabItem.Tag = MySecondForm.MyText.Text

    Why does this not work here? on my HeaderBar.Xaml i've got textbox, i just want to get that value inside my Window1 class.

    When I do it the way about I get this error "Reference to a non-shared member requires an object reference."

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    I just realised that you're using WPF and not WinForms. In WinForms, you actually access a default instance via My.Forms.SomeForm, but you can omit the My.Forms part and use just the class name as a shortcut. WPF doesn't support that shortcut so you must use My.Windows.SomeWindow.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    I'm in Window1 class and I want to refer to the Headerbar class. My screen shot does not allow this as shown.

    What am I doing wrong?
    Last edited by AirlineSim; May 29th, 2010 at 02:03 PM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Reference to a non-shared member requires an object reference.

    Is HeaderBar a Window? If not, why would it be a member of My.Windows?

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