-1
form2 newF2;

newF2 myform = new NewF2();
myform.ShowDialog();

I have the above code. I have a button in form1 that launches form2.

I get the following error message "The type or namespace name could not be found (are you missing a using directive or an assembly reference?)

I have been trying to figure this out for about 2 hours and I have no clue why I am getting this error. Both forms have the same namespace. Any ideas?

Using the below code I also get the same error:

form2 myform = new form2();
myform.ShowDialog();
JasCav
  • 33,714
  • 19
  • 103
  • 163
Brandon
  • 151
  • 2
  • 4
  • 11
  • 1
    Probably need to provide a bit more code. However, just looking at your code, I'm a bit confused by form2 newF2 / newF2 myform. It looks to me that you are confusing newF2 for a variable name and then for a type. Which one is it? – JasCav Nov 21 '11 at 03:12

3 Answers3

1

You have a syntax error.

form2 newF2;

This is declaring newF2 to be of type form2.

newF2 is a variable name and not a type.

You could fix it by

NewF2 myform = new NewF2();

or

var myForm = new NewF2();

I would examine the naming patterning you are using. If form2 is a type the accepted C# standard is Form2, it will make it easier to tell types from variables name.

For more on C# coding guidlines please see here

David Basarab
  • 67,994
  • 42
  • 125
  • 155
1

(Based on the code you showed) the line

newF2 myform = new NewF2();

should be

newF2 = new form2();

I suspect the code you have shown us is not what you actually have. What I suggest you change it to is this:

Form2 myForm = new Form2();
myForm.ShowDialog();
slugster
  • 47,434
  • 13
  • 92
  • 138
  • @Brandon: if it is a namespace issue then check you haven't got the problem described here (referencing the wrong .Net version): [VS2010 - Getting “type or namespace name could not be found” but everything seems ok?](http://stackoverflow.com/questions/3304741/vs2010-getting-type-or-namespace-name-could-not-be-found-but-everything-seem/3304899#3304899) – slugster Nov 21 '11 at 03:21
  • And you should also ensure that Form2 is in a project or assembly that has been referenced from the project that contains the code you are writing. – slugster Nov 21 '11 at 03:30
  • How would you reference form2? – Brandon Nov 21 '11 at 03:36
  • You need to add a reference to the project/assembly that contains it, this previous SO answer will show you how: [How to add a reference to a WinForm project?](http://stackoverflow.com/questions/6082167/how-to-add-a-reference-to-a-winform-project). I can't be more specific than that because I don't have you project in front of me. – slugster Nov 21 '11 at 03:40
  • So, I should find form2 in the bin folder of the "add reference box"? I don't see a form2? – Brandon Nov 21 '11 at 03:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5181/discussion-between-brandon-and-slugster) – Brandon Nov 21 '11 at 03:45
0

I think you'd better post your form2 source code here.

Maybe the form2's namespace is different with the class where you call the form2

shenhengbin
  • 4,142
  • 1
  • 21
  • 33