Jon
Jon

Reputation: 3203

Extensions found in intellisense but not at compilation

This is REALLY frustrating! I have a common library of code I use across projects. However for some reason my extensions have stopped working in this project. I have used them in plenty of other projects and I have no idea what's stopping them working now! Grr!

Here's an example extension so you can see how they're set up.

 public static string ToAlphaNumeric(this string InputString)
 {
        string OutputString = string.Empty;
        OutputString = Regex.Replace(InputString, @"[^a-zA-Z_0-9]+", "-");
        OutputString = Regex.Replace(OutputString, "[_]+", "-");
        OutputString = OutputString.Trim(new char[] { '-' });
        return OutputString;
 }

My current project is v4.0 and my code library is 3.5, though I have tried upgrading it to 4.0 but that dosn't fix it. I add a reference to the top of every page because I can't get defining it in the web.config to work.

It is definitely referenced on my page and found in the intellisense. However when I build the project I get an error which says 'string' does not contain a definition for 'ToAlphaNumeric'. But it does! Why do I get this error?

I apologise if I've missed any needed info, tried to include everything I can think of.

Thanks!

Upvotes: 3

Views: 1792

Answers (5)

Saad Farooq
Saad Farooq

Reputation: 997

My problem was similar that, the intellisense was not showing any error, nor any build errors but during runtime it will throw method not found exception.

Luckily I checked the warning tab and it had a warning like, click this warning to fix the bindings in the configuration file or it will use default bindings. I clicked it and viola, everything worked.

Visual Studio 2015 - ASP.Net MVC 5 on .Net 4.6.1

Upvotes: 0

Kaido
Kaido

Reputation: 3961

Arrived here by google searching similar symptoms.

I had this issue when someone had updated a project reference to use framework 4.5 but the referencing assembly was still 4.0.

Fixed by updating all assemblies to same version.

Upvotes: 0

user355289
user355289

Reputation: 1170

Beside the obvious answers (no reference\ no using)

When trying to "Rebuild" a solution or a project within a solution, when the solution has project dependency that is incorrect or not full :

Objects appear as known by the intelisence, but in rebuild may fail and leave an error, claiming that they are not recognized.

In this case just, clean solution and "Build" each project by the order of the dependancy, or fix solution project dependancy.

Upvotes: 1

Jon
Jon

Reputation: 3203

Set both projects to 3.5 - no real answer to this issue..

Upvotes: 2

JaredPar
JaredPar

Reputation: 754893

The two most likely causes of this error are

  • You forgot to add a reference to the project in which the extension is defined
  • You forgot to add a using for the namespace where the extension is defined

The easiest way to track down which was is at fault is to remove the extension syntax and do a straight static call.

TheNamespace.TheExtensionType.ToAlphaNumeric(myString)

Now the compilation will either work or complain that TheExtensionType is not available. If it works then your using is incorrect, if it fails then you're missing a reference to the assembly.

Upvotes: 3

Related Questions