3

This code I am working on is not working properly. It displays this error when hello() is invoked:

nameThisString "does not exist in the current context"

I have the overall code structure (it's simplified greatly to get to the point). What is wrong with my code?

using System.Windows.Controls;

namespace Application3
{
    public partial class MainView : UserControl
    {
        public SecondClass secondClass;
        public MainView()
        {
            InitializeComponent();
            hello();    
        }

        private void hello()
        {
            secondClass.nameThisString("hello");
        }        
    }

    public class SecondClass
    {
        public void nameThisString(string what)
        {
            what = "me";
        }    
    }    
}
Marshal
  • 6,551
  • 13
  • 55
  • 91
Sean
  • 336
  • 1
  • 3
  • 15
  • 4
    Maybe you simplified too much? ([ideone](http://ideone.com/yCCnuv)) – Uwe Keim Jan 06 '16 at 14:31
  • 2
    The code you've provided doesn't display the problem you've claimed. Please produce a [mcve]. – Jon Skeet Jan 06 '16 at 14:32
  • I'm with @UweKeim. Does this simplified version actually still reproduce your problem? I don't see anything wrong with it. – DrewJordan Jan 06 '16 at 14:32
  • 2
    The only thing this code will produce is a `NullReferenceException` - you've never instantiated `SecondClass`. – Jamiec Jan 06 '16 at 14:33
  • You declare secondClass, but you never instantiate it. Maybe if you did 'public SecondClass secondClass = new SecondClass()' – Taegost Jan 06 '16 at 14:33
  • This is literally the exact code that I am using. – Sean Jan 06 '16 at 14:34
  • Sorry it's giving me the error "Additional information: Object reference not set to an instance of an object." – Sean Jan 06 '16 at 14:35
  • Sorry I am new, but doesn't "public SecondClass secondClass;" create an instance? – Sean Jan 06 '16 at 14:35
  • 3
    Well that's a completely different error! It's an exception rather than the compile-time error you talked about before. – Jon Skeet Jan 06 '16 at 14:35
  • And no, it doesn't. It declares a field, which has a default value of `null`. – Jon Skeet Jan 06 '16 at 14:35
  • @Sean Nope, declaring the variable doesn't create an instance of that class. – ChrisF Jan 06 '16 at 14:36
  • I was getting both errors. The first was before I compiled and the next was while it was trying to compile. @ChrisF, how are declaring and creating an instance different? Thank you for the clarification. – Sean Jan 06 '16 at 14:39

3 Answers3

4

Per your posted code, there is only one way you can get the said error; if you have called your method like below instead of secondClass.nameThisString("hello");

    private void hello()
    {
        nameThisString("hello");
    }  

Per your comment you are getting a NullRef exception cause you have just defined the variable but haven't created an instance for it. change your code to be like

    public SecondClass secondClass;
    public MainView()
    {
        InitializeComponent();
        secondClass = new SecondClass(); // create an instance
        hello();    
    }
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Thanks! What is the difference between defining a variable and creating an instance? IE why would you just define it and not create it? – Sean Jan 06 '16 at 14:46
  • if you say `int a` you are just defining or declaring a int variable but creating a instance is nothing but when you assign a value for it and say `a = 100`. though INT is value type ... for a reference type like class `SecondClass secondClass` you have just declared the variable and it's null right now (or) doesn't point to any object in heap and so you will have to create a instance by using the `new` operator. Consider learning about OOP more first. – Rahul Jan 06 '16 at 14:49
  • Thank you for the explanation. – Sean Jan 06 '16 at 14:51
  • @Rahul `int` is perhaps the worst example you could have given - `int a` *does* give it a value (`0`) due to its type. Classes behave differently. – Jamiec Jan 06 '16 at 14:58
  • @Jamiec, that's kind a for simplicity. – Rahul Jan 06 '16 at 15:03
3

You have to create an instance of the SecondClass in the constructor of the MainView class first.

Maxim Fleitling
  • 542
  • 4
  • 14
0

You forgot to instantiate the class; use like this:

 public SecondClass secondClass;
 public MainView()
  {
    InitializeComponent();
    SecondClass = new SecondClass();
    hello();
  }

Or simply:

public SecondClass secondClass=new SecondClass();
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88