Can not access static class from .dll without namespace prefix - c#

I have a class library project with definition like this:
namespace MyNamespace
{
public static class MyClass
{
public static string GetString()
{
return "test";
}
}
}
When I reference the built .dll of this project in my another project/solution, I can access the class MyClass like MyNamespace.MyClass.GetString() but I can't access by referenceing the namespace on top of c# file. Ex.
using MyNamespace;
namespace ProjectNameSpace
{
public class TestClass
{
public void TestRun()
{
string result = MyClass.GetString(); // Getting error: are you missing an assembly reference?
}
}
}
It may be a silly question, but I am not getting why this is happening. Any help on this is really helpful to me.

Are you sure that the name of the namespace was different from the name of the class in the library? I have just run into a problem similar to this one, and after a few minutes of experimenting I realised that was the problem.
Here's an image showing the existance of the problem. Note that testLib is in references and is also using'd.
Here's the code of the library
namespace testLib
{
public class Class1
{
public Object getMe()
{
return this;
}
}
public static class Class2
{
public static int getRandomNumber()
{
return 7;
}
}
public class testLib
{
public static void testMe()
{
Console.WriteLine("working just fine...");
}
}
}
Here's the code of the tester class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testLib;
namespace libTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class2.getRandomNumber());
testLib.testLib.testMe();
testLib.testMe();//when i hover here, it says "The type or namespace does not exist in the namespace testLib (are you missing an assembly reference?)"
Console.ReadKey();
}
}
}

Related

C# same namespace in different files

I tried to move implementation to other file and got Error in Form1.cs "OpenPort doesn't exist in the curent context"
Any suggestions, please?
Form1.cs
namespace MyApp
{
void Form1Load(object sender, EventArgs e)
{
OpenPort();
}
}
Port.cs
namespace MyApp
{
public static void OpenPort();
}
First of all you can't declare methods or properties directly in a namespace. You have to declare a class first, in which you write your methods. Then, if you want to "spread" the same class in different files, you ought to add the partial keyword.
Form1.cs
namespace MyApp
{
public partial class MyClass
{
public void Form1Load(object sender, EventArgs e)
{
OpenPort();
}
}
}
Port.cs
namespace MyApp
{
public partial class MyClass
{
public static void OpenPort()
{
// your implementation here
}
}
}
Think of a namespace like a container. Just like any other container you can put stuff in it. An advantage of namespaces is that they can group functionality together without you needing to put everything in the same file - of particular use when programs get large. Namespaces are also a way of keeping names separate. Think of roads - both London Road and High Street may both have a number 18 but 18 London Road and 18 High Street are different and distinguishable.
For example, MyNamespace contains MyClass and MyClass contains MyMethod
namespace MyNamespace
{
class MyClass
{
void MyMethod() {}
}
}
To use MyMethod from another namespace you need to add a using statement for MyNamespace.
Taking this a step further imagine MyOtherNamespace contains MyClass and MyClass contains MyMethod
MyOtherNamespace also contains MyClass and MyClass also contains MyMethod
namespace MyOtherNamespace
{
class MyClass
{
void MyMethod() {}
}
}
As before, to use MyMethod from another namespace you need to add a using statement for MyOtherNamespace.
If you want to use MyMethod from both MyNamespace and MyOtherNamespace you must tell your program which one to use by adding the Namespace name to the call like this
MyNamespace.MyClass.MyMethod();
MyOtherNamespace.MyClass.MyMethod();
The same rule applies if you've got multiple classes with the same method in the same namespace - the difference being you don't need to include the namespace
MyFirstClass.MyMethod();
MySecondClass.MyMethod();
On a related note, you can also split a class between multiple files using the partial keyword. For example, you could turn this
namespace MyNamespace
{
class MyClass
{
void MyMethod() {}
void MyOtherMethod() {}
}
}
Into this
namespace MyNamespace
{
partial class MyClass
{
void MyMethod() {}
}
partial class MyClass
{
void MyOtherMethod() {}
}
}
Functionally they're the same and in both cases you'd call the methods like this
MyClass.MyMethod();
MyClass.MyOtherMethod();

internal class access c#

This maybe a very simple question! But I have been scratching my head for an hour now! I have two files as below:
Assembly1.cs
Program.cs
I thought when I use internal keyword before a class name I won't be able to instantiate it in other classes, right?
https://msdn.microsoft.com/en-us/library/7c5ka91b.aspx
But why am I not getting an error message for this here? I maybe missing something very obvious here.
// Assembly1.cs
namespace InternalTest
{
internal sealed class BaseClass
{
public static int intM = 0;
}
}
// Program.cs
using System;
namespace InternalTest
{
class Program
{
static void Main(string[] args)
{
var myBase = new BaseClass();
Console.ReadKey();
}
}
}
Internal means class is accessible within the assembly.Above class is in same assembly hence no error.If you want to see the error then follow below step
1) Create Library project
2) Create Base Class in that project
3) Create Console project
4) Add reference of first project dll
5) Now try to create instance, you will see the error
Additional Info
If you want to access internal members in your console project then add below attribute in AssemblyInfo.cs of Library project
[assembly:InternalsVisibleTo("[AssemblyNameOfConsoleProject]")]
Because both classes are on the same namespace.
//InternalTest
namespace InternalTest
{
internal sealed class BaseClass
{
public static int intM = 0;
}
}
//the same here InternalTest
namespace InternalTest
{
class Program
{
static void Main(string[] args)
{
}
}
}

C# error, member with same name

I writed this code, but, when I try to build, the compiler returns:
1>code.cs(16,16,16,44): error CS0542: 'DataGridViewPercentageColumn': member names cannot be the same as their enclosing type
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
namespace TestSample
{
internal static class DataGridViewPercentageColumn
{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
}
How I can solve this, and, why this error happens??
Thanks
You cannot nest two classes with the same names. Your inner class DataGridViewPercentageColumn has the same name like the outer class. You have to rename one of these classes like:
internal class DataGridViewClasses{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
Btw: You cannot have any other members or properties inside a class named like the class itself.
Your wrapper class has the same name as one of the internal classes.
The issue is DataGridViewPercentageColumn.
The problem here is that you've defined a class within another class, both of which have the same name. Give the internal static class a different name.

namespace being shadowed

so I have a library Mine.SuperFun which calls stuff in the library SuperFun whose main namespace is SuperFun. The problem i'm having is that i can't address classes or basically anything in the SuperFun library inside classes in the Mine.SuperFun.XyZFoo namespaces
The only way to address them i have is doing stuff like:
using SuperFun_NiceClass = SuperFun.NiceClass;
using Mine.SuperFun {
...
SuperFun_NiceClass.DoStuff()
is there something i can do (besides changing the namespace in Mine library) to be able to address those classes directly?
You can use the global contextual keyword
What is the usage of global:: keyword in C#?
http://msdn.microsoft.com/en-us/library/cc713620.aspx
namespace Mine.SuperFun
{
public class My { public int a; }
}
namespace SuperFun
{
public class Theirs { public int a; }
}
namespace SomeProgram
{
public class Program
{
SuperFun.Theirs theirs;
global::Mine.SuperFun.My mine;
}
}

C# namespace questions

what's the difference for the following two ways to define namespace?
namespace A.B.C {
public class AA{
}
}
namespace A {
namespace B{
namesapce C{
public class AA{
}
}
}
}
in some where I may have
namespace A{
//some classes
}
namespace A.B {
//some classes
}
namespace A {
namespace B {
//some classes
}
}
Both need to do the same to use class AA by using A.B.C; Can I use C.AA a; to specify the AA class in C namespace or I have to use the fall namespace convention: A.B.C.AA a; to avoid possbile confliction?
They're the same. If you look at this code in .NET Reflector:
namespace A {
namespace B{
namespace C{
public class AA{
}
}
}
}
you get this:
namespace A.B.C
{
public class AA
{
// Methods
public AA();
}
}
Both methods are compiled to exactly the same intermediate language code.

Categories