using and assemblies

missing using directive or assembly directive

Page 1 of 1

7 Replies - 4428 Views - Last Post: 25 June 2010 - 08:24 AM Rate Topic: -----

#1 tynman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-June 10

using and assemblies

Posted 24 June 2010 - 08:39 AM

The one thing in C# programming I seem to spend the most time spinning my wheels over is the compiler error "The type or namespace x could not be found (are you missing a using directive or an assembly reference?)".

The problem always starts with my attempt to add some new functionality to a program, but I don't know how to do it, so I search the web for a solution. I find a promising code snippet, but it often does not include mention of using directives and/or assembly references that might be required by the snippet. I plug it into my program, and get this error on compile.

Occasionally, the problem is caused by a typing error on my part. But 95% of the time, the problem is actually caused by a missing using directive or a missing assembly reference, just like the message says.

I understand that the using directives allow me to use class and type references in my code without having to specify the whole object hierarchy for the class/type. And I understand (sort of) that the assembly references list the dll files that contain the classes that I reference from within my program but aren't actually part of my program (what I tend to think of as "system functions" from my life before objects).

I typically search the error message using a web search engine until I find an article that says something like "you need to add System.Drawing to your References in Visual C#". I add it, and the compile error disappears.

I am missing some fundamental pieces of understanding regarding C# programming. I should be able to figure out the solution to these problems without always asking for help on the web. For example, today I added
throw new Win32Exception(Marshal.GetLastWin32Error());
to a program, and received the error about missing usings/assemblies. I found the answer quickly enough: add
using System.ComponentModel;
That's great, but what do I need to learn in order to have been able to figure this out myself?

Since all of these "missing" classes and types are somewhere on my workstation, is there not a way to search for them there?

Thanks,
Ben

Is This A Good Question/Topic? 0
  • +

Replies To: using and assemblies

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: using and assemblies

Posted 24 June 2010 - 08:46 AM

MSDN Entry for Win32Exception.

Look at the topAttached Image
and the bottomAttached Image
Was This Post Helpful? 0
  • +
  • -

#3 tynman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-June 10

Re: using and assemblies

Posted 24 June 2010 - 10:16 AM

Thanks much JackOfAllTrades, I think that will help a lot. ...Ben
Was This Post Helpful? 0
  • +
  • -

#4 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: using and assemblies

Posted 24 June 2010 - 01:29 PM

A lot of the time Visual Studio can do it for you. You could have right-clicked on the text "Win32Exception", and from the "Resolve" menu there's an option to add the missing namespace, or to add the fully-qualified name instead:

Posted Image

VS can't always work it out though - I think if you're not referencing the DLL in your project then it won't be able to help you with that. In that case, like JackOfAllTrades said, MSDN is your friend.

This post has been edited by Charles:): 24 June 2010 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: using and assemblies

Posted 24 June 2010 - 01:47 PM

A very useful keyboard shortcut: Alt+Shift+F10 (awkward, I know).

Any time you see a blue line under the beginning of a word you type, you can use this shortcut to bring up the dropdown dialog.

Posted Image
Posted Image
Posted Image

This is useful for the "resolve" dialog, as well as several others. For instance, you can type a method name you haven't created yet, and use the shortcut to create the skeleton method for you. Or if you're creating a class that implements an interface, or extends another class, you can use it to fill out the skeletons of all the required virtual methods and properties.

It's a handy dandy little shortcut.
Was This Post Helpful? 2
  • +
  • -

#6 EvLSnoopY   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: using and assemblies

Posted 25 June 2010 - 05:57 AM

View PostinsertAlias, on 24 June 2010 - 12:47 PM, said:

A very useful keyboard shortcut: Alt+Shift+F10 (awkward, I know).

Any time you see a blue line under the beginning of a word you type, you can use this shortcut to bring up the dropdown dialog.

Posted Image
Posted Image
Posted Image

This is useful for the "resolve" dialog, as well as several others. For instance, you can type a method name you haven't created yet, and use the shortcut to create the skeleton method for you. Or if you're creating a class that implements an interface, or extends another class, you can use it to fill out the skeletons of all the required virtual methods and properties.

It's a handy dandy little shortcut.


Or you could use "Ctrl + ." if you see that blue line.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: using and assemblies

Posted 25 June 2010 - 06:13 AM

View PostEvLSnoopY, on 25 June 2010 - 04:57 AM, said:

Or you could use "Ctrl + ." if you see that blue line.


Hmm, that might be a bit less awkward. We should start a thread of our favorite keyboard shortcuts, we might all learn something new!
Was This Post Helpful? 1
  • +
  • -

#8 EvLSnoopY   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: using and assemblies

Posted 25 June 2010 - 08:24 AM

Yes I would have to agree, seems like a good idea to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1