'namespace used like a type' error

60,122

Solution 1

Confusion is arising because the actual type is MarkupConverter.MarkupConverter, the compiler seems to think your new MarkupConverter is an attempt to use a namespace as a type, rather than an attempt to instantiate a type inside your using namespace.

Simply change your problem line to:

markupConverter = new MarkupConverter.MarkupConverter(); /*SOLUTION HERE!*/

..and you should be fine.

Solution 2

In your case, you have a namespace MarkupConverter and a class with the same name (MarkupConverter again).

In the line markupConverter = new MarkupConverter(); /*PROBLEM IS HERE*/ the compiler is unable to tell that you intent to use the class. Since a namespace with the same name is present, the compiler picks it instead, because namespaces are linked with higher priority by the compiler.

You can resolve this by using the complete name of the class:

// supposedly MarkupConverter is the namespace of the MarkupConverter class
markupConverter = new MarkupConverter.MarkupConverter();

An alternative way to providing the fully-qualified name of the class is to use an alias, which takes the form of using {ALIAS} = {Fully qualified name of Type| Namespace}. Note that the {ALIAS} part can be any valid identifier.

The alias you can place either in your usings:

using System.Net.Mail;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using MarkupConverter;

using MarkupConverter = MarkupConverter.MarkupConverter; // this is aliasing

or after the namespace declaration:

using System.Data.SqlClient;
using MarkupConverter;

namespace AspPersonalWebsite
{
    using MarkupConverter = MarkupConverter.MarkupConverter;

    ....

and you're good to go! At this point, if aliases are present, the line

markupConverter = new MarkupConverter()

will correctly pick the MarkupConverter class, because explicit aliasing has higher priority than the automatic binding done by the compiler.

Solution 3

That is pretty much self explanatory,

MarkupConverter is a namespace ,so shouldn't be used as a class to create an object

Solution 4

Can you show the MarkupConverter class you use please? The error is maybe in its declaration. In Where namespace is it? What is your file structure?

Maybe you have created a MarkupConverter namespace?

Share:
60,122
Eyad
Author by

Eyad

Updated on July 09, 2022

Comments

  • Eyad
    Eyad almost 2 years

    Coders, I am trying to convert a XAML string to HTML using a library I found here , but I have a problem with creating a new instance of the object that would let me use the library. I already added a reference to the library in my Asp.net project and I would like to use it in a WCF file.

    The problem is that whenever I try to instantiate a new object with the new keyword, I get an error that says:

    'MarkupConverter' is a 'namespace' but is used like a 'type'.

    Here is my code, notice that I am creating a new object just like the example shown in the library link above, please help:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.Web.Services;
    using System.Net.Mail;
    using System.ServiceModel.Activation;
    using System.Data.SqlClient;
    using MarkupConverter;
    
    namespace AspPersonalWebsite
    {
        [ServiceContract(Namespace = "")]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1 //: IService1
        {
            private string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            private IMarkupConverter markupConverter;        
    
            [OperationContract]
            public string convertXAMLToHTML(string XAMLtext)
            {
                string htmlText = "";
                markupConverter = new MarkupConverter(); /*PROBLEM IS HERE*/
                htmlText = markupConverter.ConvertXamlToHtml(XAMLtext);
                return htmlText;
            }
        }
    }
    
  • Eyad
    Eyad over 13 years
    I did but the user abatishchev has removed it when he edited my post!!
  • Nicolas Raoul
    Nicolas Raoul over 8 years
    abatishchev has reverted their removal, it seems. So this answer is kind of obsolete now, I would say?
  • Nicolas Raoul
    Nicolas Raoul over 8 years
    The second solution sounds very elegant, but unfortunately I get Namespace 'Topns' contains a definition conflicting with alias 'MarkupConverter'. (Topns is the top namespace above the MarkupConverter namespace).
  • Ivaylo Slavov
    Ivaylo Slavov over 8 years
    When aliasing inside a namespace, you must still use the full type name in the alias. For example using MarkupConverter = Topns.MarkupConverter.MarkupConverter, where Topns.MarkupConverter is a namespace containing the MarkupConverter class. To resolve further confusion of the compiler, you may also use using MarkupConverter = global::Topns.MarkupConverter.MarkupConverter In general the global:: prefix should be rarely used, but it is useful if your own namespaces conflict with the ones from a referenced assembly
  • Nicolas Raoul
    Nicolas Raoul over 8 years
    I wrote using MarkupConverter = global::Topns.MarkupConverter.MarkupConverter; but then protected MarkupConverter foo; fails to compile, with this error: Namespace 'Topns' contains a definition conflicting with alias 'MarkupConverter'.
  • Ivaylo Slavov
    Ivaylo Slavov over 8 years
    This is hard to gasp into without the source code at a hand, but what comes to my mind is that you change the MarkupConverter alias to something different and unique and use it instead.
  • Scott Weaver
    Scott Weaver over 7 years
    maybe that could get you some sort of weird badge.