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();
Related
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)
{
}
}
}
I have the follwoing code which I don't understand where is my mistake?
Here's the code:
using System;
using System.Collections;
namespace ConsoleApplication5
{
static void Main(string[] args)
{
class Program
{
public class Animal
{
public virtual void Greet()
{
Console.WriteLine("Hello, I'm some sort of animal!");
}
}
public class Dog : Animal
{
public override void Greet()
{
Console.WriteLine("Hello, I'm a dog!");
}
}
}
It doesn't get compiled, I get the following errors:
On Line 24 Type or namespace definition, or end-of-file expected
and on Line 6 } is expected
I don't understand what did I do wrong, can you help me?
Thanks.
I compiled this in Visual Studio 2015, if it matters.
BTW, I got part of this code from this tutorial:
http://csharp.net-tutorials.com/classes/inheritance/
I believe you have a confusion about scopes. You defined a class in a method which is not applicable, you should define classes under namespace scope.
It should be this way (This is a pseudo code)
namespace YourNamespace
...class Program
.... public class animal
.... public class dog : animal
.... static void main
You cannot define a class within a method body, which is what happens here:
static void Main(string[] args)
{
class Program
{
Instead you need to place the code for your Main() method within the Program class:
class Program
{ // This brace marks the beginning of the Program class
static void Main(string[] args) // This is a method defined WITHIN Program class
{ // The brace marks the beginning of the Main method
....
} // This brace marks the end of the Main method
} // This brace closes the Program class
There must be something simple that I'm missing. I have two partial classes, in two different assemblies, and I'm not able to reference a private static method in one partial class from the other partial class.
For example, in the FirstHalf assembly, I have the following class:
namespace FirstHalf
{
public partial class TestPartialClass
{
private static void PrintFoo()
{
Console.WriteLine("Foo");
}
}
}
Then, in the SecondHalf assembly, I have the following class:
namespace FirstHalf
{
public partial class TestPartialClass
{
public static void Main(string[] args)
{
Console.WriteLine(PrintFoo());
}
}
}
However, when I try to invoke PrintFoo() from the SecondHalf assembly, I get the following error:
CS0103: The name 'PrintFoo' does not exist in the current context.
What's going on, here? I have a reference from SecondHalf to FirstHalf, so Visual Studio does know that there is a dependency between the two.
You can't split a partial class between two assemblies; they are actually being compiled as two different classes.
If you really want to accomplish something like this across assemblies, with sharing of 'private' members, you can get something similar by creating a base class and inheriting it:
namespace FirstHalf
{
public class Base
{
protected static void PrintFoo()
{
Console.WriteLine("Foo");
}
}
}
namespace SecondHalf
{
public class Derived : FirstHalf.Base
{
public static void Main(string[] args)
{
PrintFoo();
}
}
}
That said, there is probably a cleaner way to accomplish what you're trying to do using some form of composition; the details depend on your particular application.
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;
}
}
I have a name space Company.Controls, which contains several controls. I also have a class called "Common" which contains enums/structures/static methods that I use throughout the controls.
Is there a way to make these "Common" peices belong to the Company.Controls namespace this way I don't have to keep typing "Common.Structure"? Essentially having he "Common" both a namespace and a class.
Just seems messy and confusing when reading the code.
example (all the other controls are in the Blah.Controls.Common namespace)
namespace Blah.Controls
{
public enum ControlTouchState
{
Down = 0x00,
Up = 0x01,
}
public Common()
{
//Stuff here
}
}
Thanks.
You can't get exactly what you want; in C# all methods have to be in a class.
Depending on what is in your Common class, you might be able to find something a slightly more satisfying by using extension methods:
namespace Blah.Controls
{
public class CommonControl { }
public static class Common
{
public static void Foo(this CommonControl cc) { }
}
public class Control1 : CommonControl
{
public void Bar()
{
this.Foo();
}
}
}
Another thing you might consider is using partial classes which would let you write simple wrappers elsewhere:
namespace Blop.Controls
{
public static class Common
{
public static void Foo() { }
}
public partial class Control1
{
public void Bar()
{
Foo();
}
}
public partial class Control1
{
public void Foo()
{
Common.Foo();
}
}
}
Obviously, introducing some inheritence could eliminate some of the duplication; I'm assuming you don't want to do that.
Is there some reason that the nested types in Common MUST be nested? Why not separate them out into their own namespace?
namespace Common
{
public struct Structure
{
// ...
}
public enum Enumeration
{
// ...
}
public class Common
{
// ...
}
}
You could then use the Common namespace as such:
namespace Blah.Controls
{
using Common;
class Control
{
Struct myStruct;
Enumeration myEnum;
Common myCommon; // references the class, not the namespace
}
}