Q: adding a project

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hiya

I hope somebody can help me with this relatively simple beginner question:

I have created a windows Application in C#. I have also created a project,
which includes a class. I have added the project to the main application but
I can't seem to create an instance of the class in the main application. The
class is called MyFirstClass, with namespace MyFirstClass. What steps do I
need to take to create an instance of the class within the main windows
application?

Thanks in advance

Geoff
 
You can change the namespace of your class to correspond with the namespace
of your other project so both assemblies become of one namespace.
Then, if you use myClass classOfMine = new myClass(); it should work.
 
Make sure your namespaces in both files match up. If not then you will have to
use the fully qualified name.

When you say "I have added the project to the main application" I'll assume you
meant "I have added the class to the main application".

If they are two different projects then you'll need to add a reference from one
to the other.

Try
MyFirstClass.MyFirstClass instance = new MyFirstClass.MyFirstClass();

- john
 
I get the message:

; expected

?

Geoff

Vincent said:
You can change the namespace of your class to correspond with the
namespace
of your other project so both assemblies become of one namespace.
Then, if you use myClass classOfMine = new myClass(); it should work.
 
Hi John

This is very puzzling.

They are two different projects. I have now added a reference to the main
application for the other class. I have tried your suggestion and used the
fully qualified name but I still get the same error i.e. ; expected.

Geoff
 
If you use Visual Studio you can double click the message and it will go to
where this error is located. You can then adjust it. (You probably forgot to
enter a ";" somewhere, where it was expected.)
For instance
myClass cls = new myClass() // will generate an error
myClass cls = new myClass(); // won't generate that error
 
Hi Vincent

Yep, tried that. I've written

myClass cls = new myClass();

and it still gives the same error message.

Geoff
 
AHHHHHHHHHHHHHHHH!!!

Got it! I'd written

myClass cls = New myClass; // Note capital "N"

Strange, I would have thought the environment would allow both?

D'OH!

Thanks guys
 
Geoff,

The first thing when you have added a project to your project which holds a
class libary is setting a reference to it with the references and than the
tab "projects". Than that extra project(s) should be showed there and you
can select it.

Than you can set an import to that (not necessary however easy)

I hope that this is what you are after.

Cor
 
Back
Top