-
May 28th, 2010, 03:53 AM
#1
Thread Starter
Fanatic Member
[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
-
May 28th, 2010, 04:43 AM
#2
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()
-
May 28th, 2010, 02:58 PM
#3
Thread Starter
Fanatic Member
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?
-
May 28th, 2010, 03:44 PM
#4
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
-
May 28th, 2010, 03:47 PM
#5
Thread Starter
Fanatic Member
Re: Reference to a non-shared member requires an object reference.
Thanks heaps. Very helpful
-
May 29th, 2010, 12:59 AM
#6
Thread Starter
Fanatic Member
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)
-
May 29th, 2010, 01:36 AM
#7
Re: [RESOLVED] Reference to a non-shared member requires an object reference.
Originally Posted by AirlineSim
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.
-
May 29th, 2010, 01:50 AM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Reference to a non-shared member requires an object reference.
Originally Posted by jmcilhinney
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."
-
May 29th, 2010, 02:29 AM
#9
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.
-
May 29th, 2010, 03:32 AM
#10
Thread Starter
Fanatic Member
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.
-
May 29th, 2010, 04:22 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|