# .NET and More > WPF, WCF, WF >  [RESOLVED] reference to non-shared member requires object reference

## ssampson

OK - Firsly, I read a lot of posts on this... but I am missing something...

I am playing writing a test WPF application in ExplressionBlend 4/VS2010

I have a ComboBox defined on the first form  (Main.XAML)- I can, of course, manipulate the ComboBox in VB.NET (V2010 as editor) within Main.XAML.VB.


I have another Public Class  (newclass.VB) - 

Imports MyApplication.Main   - has been declared in newclass.vb - 

But ANY reference to ComboBox in newclass.vb gives me:

'reference to non-shared member requires object reference'


I never have had this issue before with a forms object... I would have looked in the generated VB code before.... but in WPF this is in XAML...So... I am lost, confused or suffering age related illness - hehe

I put this here because I am using VB.NET behind and that is where the issue is occuring...

Cheers

Scott

----------


## jmcilhinney

First up, WPF questions belong in the WPF forum.  I have asked the mods to move this thread.

As for the question, if you're getting that error message then you're trying to access a member of a ComboBox object without an object.  You have to access instance members via a ComboBox object, not the ComboBox class.  To say more than that we'd have to see an actual example of your code that's causing this error.

----------


## ssampson

Cheers !! - fair enough on the move....

Here is a subset of the code.

The following is generated by Expression Blend 4

Main.Xaml



```
<Grid HorizontalAlignment="Left" Margin="4,170,0,55" x:Name="SideBar" Width="56" Cursor="Arrow">
	<ComboBox IsSynchronizedWithCurrentItem="True" x:Name="cbUnits" IsReadOnly="True" FontWeight="Bold" SelectionChanged="UnitChange" SelectedIndex="1" Background="{x:Null}" Foreground="#FF1EC609" VerticalAlignment="Top" Height="22" Style="{DynamicResource ComboBarelyVisible}" ToolTip="Set Units for Depth Scale" HorizontalContentAlignment="Stretch"/>
</Grid>
```



In Main.Xaml.VB I can access  'cbUnits' without issue of course

However, in the following I get the error for cbUnits - 



```

Imports TextApp.Main


Public Class Labeling


    Shared Sub SetLabels()


        '
        ' Units ComboBox Settings
        '
        cbUnits.Items.Add("Feet")
        cbUnits.Items.Add("Meters")
    End Sub

End Class
```


Of course adding in the Imports for the controls (as is in Main.xaml.vb)won't make a difference.... just to limit reponses !!!

I am assuming that it has to do with the instance not being shared..... I just can't figure out where to fix or declare this here because of my lack of experience interactin with XAML (or so I beleive my problem is - hehe)

----------


## jmcilhinney

What exactly do you expect 'cbUnits' to refer to in that code?  You're inside a the Labeling class, which doesn't know anything specifically about your Window.  'cbUnits' is a variable in your Window and it refers to a specific ComboBox object in each Window object.  Outside your Window, 'cbUnits' means nothing.  If your code is in a class other than your Window, you would first have to have a reference to an instance of your Window class.  You can then access the 'cbUnits' variable of that Window to get that ComboBox object.

Let's say that I cam over to your house and told you to "scratch it's head".  Would you be able to do that?  You wouldn't know what "it" is.  What if I told you that "it" was a cat?  Would be able to do it then?  No, because you still don;t know which cat I'm talking about and, even if you did, you probably don't have that cat in front of you to scratch its head.  This is basically the same.  Your SetLabels class doesn't know anything about your Window.  You have imported the Main class (which you should absolutely not have done) so you've told the Labeling class about the Main class but you still haven't given the SetLabels method a reference to a Main object.  Only from a Main object can it get the 'cbUnits' field of a Main object.

----------


## ssampson

LOL - I feel like I just got called into the 'office' - hehe

RE: The Imports - yes - that was added when I was messing around - trying everything before I posted

Also - I DO understand that Labeling.Setlabels cannot see the object... I KNOW that is the reason for the error... 

I was thinking along the line  'PROTECTED FRIEND' .... but as the generated code is XAML here and not VB, I didn't kow how to even 'try' this

However....To clarify what i THINK you are saying...I CANNOT do this whatsoever?????

As I said - I have never seen this before, so I am assuming I have never tried it (in regular VB).... but I seem to recall changing object colors (eg. label forecolor) between forms and their respective classes...

I certainly can move this code without issue (I stripped out 99&#37; of the code from Sub SetLabels) - I was just consolidating !!!

I will go back and sit in the corner - hehe

Cheers (and thanks !)

Scott

----------


## jmcilhinney

As is always the case, to access an instance member, i.e. any member not declared Shared, you must have an instance to start with.  Any code that wants to access the cbUnits field of a Main object needs to have a Main object to start with.  If you want to be able to access members of a Main object in your Labeling class then you need to pass a reference to a Main object into that class somehow.  That could be done in a variety of ways.  Presumably your Main object is the app's startup Window, in which case it will be the default instance of its class.  In that case, the easiest options is to use the default instance:
vb.net Code:
My.Windows.Main.cbUnits.Items.Add("Feet")
I'm not 100&#37; sure that that's possible in Blend and it's not the way I would choose to do it regardless.

That said, it really doesn't seem appropriate to be adding items to a ComboBox on a Window from outside that Window anyway.  If you need to get the data from elsewhere then the Main Window should call a method that returns the data as an array or the like, then add that to its own ComboBox.  That's far better practice.

----------


## ssampson

Thanks - I will change my thinking....

The Labeling Class handles a bunch of 'related' stuff.... I had put these items here more for neatness than anything else.... They are defined in code rather than as a item collection set when creating the control for consistancy (other items lists are dynamic).... and this was only 2 items...so...

Regardless.... Thanks for the input - I will close this one out

----------


## mendhak

A bit late it seems, but moved to WPF.  :Big Grin:

----------

