I've the following situation:
1) I have an internal static class where my software initialize a form
2) I would like to get the instance of this form to use for other reasons.
Example of code:
Class1:
namespace x {
internal static class Program {
private static Form mainx;
private static void Main() {
.....
.....
mainx=new Form(.....);
Application.run(mainx);
}
}
}
Class2:
I want to use one thing like this:
Form1 try=Program.mainx;
How can i do it?
If both assemblies are signed, you can use the InternalsVisibleToAttribute to expose internal members of an assembly to another.
I often use this to enable unit testing of internal classes without having to expose them as public.
You could mark the assembly with the internal class as a friend assembly on the other assembly, with the attribute InternalsVisibleTo. You will find more informations about this on the MSDN.
You need to add this line to your AssemblyInfo class (in the Properties folder), i.e. on the last line. This must be added in the project where you have declared the internal class.
[assembly:InternalsVisibleTo("NameOfOtherAssembly")]
If you with to retrieve the mainx property of your Program class, you need to make a visible (public or internal) getter on your class:
internal static class Program
{
private static Form mainx;
...
public static Form GetForm()
{
return mainx;
}
}
In your second class, you should then be able to get the form by calling GetForm():
Form1 try=Program.GetForm();
Related
I have Created one ConsoleApplication to understand Access Specifiers.
Below is my code for internal, I can access this class from outside the Assembly.
namespace Assembly_1 //This is first assembly.
{
public class Base
{
//internal class
internal class B
{
public static void fnB()
{
Console.WriteLine("fnB");
}
}
}
}
namespace Assembly_2 //This is second assembly.
{
public class Derived : Assembly_1.Base
{
public class D
{
public void fnD()
{
B.fnB();//how can I access this class?
}
}
}
}
And this is where I am Accessing it.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Assembly_2.Derived.D d = new Assembly_2.Derived.D();
d.fnD();
}
}
}
My Question
Right now I can Access Class B and it's methods like fnB() in Derived.
Everything works fine. but How?
How can I access the B Class outside Assembly_1?
As I worte in the comments:
You are confusing the namespace and assembly terms.
You can read about it here:(Assemblies and Namespace)
Many namespaces can be defined in a single assembly.
If you would like to check and understand the internal modifier,
then you would have to create a new class library project (that will compile into a different assembly), define the Base class there
and add a reference to it in your main console application.
Then you will see that you don't have access to it anymore and the code will not compile.
How can I access the B Class outside Assembly_1?
Because you're confusing namespaces and assemblies. An assembly is a collection of one or more namespaces, contained within a .dll or .exe file.
See also: MSDN: Assemblies in the Common Language Runtime and Understanding and Using Assemblies and Namespaces in .NET.
What you call Assembly_1 and Assembly_2 are namespaces within the same assembly.
Because internal members are visible within the same assembly, you can use Assembly_1.B from Assembly_2.D, because both namespaces reside in the same assembly.
I am writing windows forms application which needs to have global variable to change from one Form class and then to be used from another Form class
this is class for global variables
namespace Testi
{
public class publicVar
{
public static int kitxvisN = 0;
public static int sworipas = 0;
}
}
and this is class where I want to use it
namespace Testi
{
public partial class Form1 : Form
{
publicVar something = new publicVar();
something.kitxvisN++;
....
but it says invalid token ++ . . .
what is wrong can somebody help me?
You're trying to access static member as if it was not static. You don't need, and even can't use class instance to access static members. Use class name instead:
publicVar.kitxvisN++;
Because there is no instance variable, you access the members of a
static class by using the class name itself.
from Static Classes and Static Class Members (C# Programming Guide)
Update
Other thing is, you can't use code like that directly on class level. You need some method to put it inside it.
public void MyMethod()
{
publicVar.kitxvisN++;
}
static members can't be used with objects. They can be accessed by class name such as publicVar.kitxvisN ++;
Simply try with:
publicVar.kitxvisN++;
Because it's a static member and which does not require an instance to access that. static member should simply be accessed by class name itself.
You can't place those methods directly under class declaration. It has to be inside of a method. E.g.
namespace Testi
{
public partial class Form1 : Form
{
public static void Main(string[] args) {
publicVar.kitxvisN++;
}
I'm building a class to hold some global variables.
I'm just curios what would make the diference in using a static or a non-static class for this purpose.
Example
public class Modules
{
public static int Modul1{ get { return 1; } }
}
or
public static class Modules
{
public static int Modul1{ get { return 1; } }
}
Declaring like a static you add at least 2 architectual restrictions :
1) None can derive from that class and extend it in any way.
2) Internal members can be only static ones.
So if you want to use this class like a config base, use it like a static class definitely.
The difference is that you cannot create an instance from a static class. If you are not going to use the class for objects that is the best option so its clear that this class shouldn't be instantiated.
Edit:
As a sidenote, i think it's bad design to do this in large projects: link
I have a static Settings class with static properties, seemed to make sense, no need to create multiple instances and a singleton seemed overkill.
In instance classes, you can have mix of static and non static. But in static class, every ting is static. So no instance specific members can reside.
Plus if all on the purpose or needs, do you really require the whole class to be static than just the member of it?
I have a number of classes in a Classes file, and I want them all to be able to access the same, global method to save duplicating code. Problem is, I can't seem to access a method from another class in my file - any ideas?
So my class1.cs layout is similar to this:
public class Job1
{
public Job1()
{
}
}
public class Methods
{
public static void Method1()
{
//Want to access method here from Job1
}
}
You'll need to specify the class they are in. Like this:
public Job1()
{
Methods.Method1()
}
If the class Job1 is in a different namespace from Methods then you'll need to either add a using clause, or specify the the namespace when calling the method. Name.Space.Methods.Method1()
Actually. Public Job1(){} is a constructor and not a method. It can be called from main class by creating object form the JOB1 class. Here add the following code:
public static void method1()
{
Job1 j1=new Job1();
}
constructor can be invoked by creating a object to the corressponding class....
To access methods of other classes, the methods must be static with a public Access modifier.
static - Not bound to an instance of the class but shared by all other instances.
private - data can only be accessed from inside the same class.
public - data can be accessed from other classes but must be referenced.
In C/C++, I have a bunch of functions that I call from main(), and I want to rewrite this in C#. Can I have stand alone functions(methods) or do I have to put them in another class? I know I can have methods within the same class, but I want to have a file for each function/method.
Like this works:
using System.IO;
using System;
class Program
{
static void Main()
{
House balls = new House();
balls.said();
}
}
public class House
{
public void said()
{
Console.Write("fatty");
Console.ReadLine();
}
}
But then I have to create an instance of House and call said(), when in C I can just call said().
For reference, I want to add the using static addition of C# 6 here.
You can now use methods of a static class without having to type the name of that class over-and-over again. An example matching the question would be:
House.cs
public static class House
{
public static void Said()
{
Console.Write("fatty");
Console.ReadLine();
}
}
Program.cs
using static House;
class Program
{
static void Main()
{
Said();
}
}
No. Make them static and put them in a static utility class if they indeed don't fit within any of your existing classes.
If using C# 9 it is now kinda possible, thanks to the top-level statements feature.
In your executable project, the following syntax is now allowed:
using SomeNamespace;
// The following statements are seemingly defined without even a method,
// but will be placed inside a "Main" static method in a "$Program" static class
SayHello();
var classFromSomeNamespace = new SomeClass(); // from SomeNamespace
classFromSomeNamespace.SomeMethod();
// This function is seemingly defined without a class,
// but on compile time it will end up inside a "$Program" static class
void SayHello()
{
Console.WriteLine("Hello!");
}
// Here the "traditional" syntax may start
namespace SomeNamespace
{
public class SomeClass
{
public void SomeMethod()
{
Console.WriteLine("SomeMethod called");
}
}
}
It should be noted, that the above syntax is valid only for a single file in a project, and the compiler actually still wraps this all inside a $Program static class with static methods. This feature was introduced specifically to avoid boilerplate code for the program entry point, and make it possible to easily write "scripts" in C#, while retaining the full .NET capabilities.
There is no concept of standalone functions in C#. Everything is an object.
You can create static methods on some utility class, and call those without creating an instance of a class eg
class Program
{
static void Main()
{
House.said();
}
}
public class House
{
public static void said()
{
Console.Write("fatty");
Console.ReadLine();
}
}
You have to put them in a class, but the class can be static as others mentioned. If you REALLY want to have a separate file for each method, you can mark the class as partial to get the following:
Program.cs
----------
class Program
{
static void Main()
{
House.said();
House.saidAgain();
}
}
House-said.cs
-------------
public static partial class House
{
public static void said()
{
Console.Write("fatty");
Console.ReadLine();
}
}
House-saidAgain.cs
------------------
public static partial class House
{
public static void saidAgain()
{
Console.Write("fattyAgain");
Console.ReadLine();
}
}
I wouldn't recommend separating each one out, however. Partial classes are mostly used so that designer-generated code won't overwrite any custom code in the same class. Otherwise you can easily end up with hundreds of files and no easy way to move from one method to another. If you think you need a partial class because the number of methods is getting unmaintainable, then you probably need to separate the logic into another class instead.
Although the concept of stand-alone functions exists in .NET, C# doesn't allow you to specify such functions. You need to stick them inside a static Utils class or similar.
If you declare your method as static (that is: public static void said()) then you can just call it with House.said(), which is as close as you'll get in C#.
You could add all your methods to the Program class, but this would quickly become an unmaintainable mess, commonly referred to as the God Class or Ball of Mud anti-pattern.
Maintaining a single file for each function would similarly become a huge mess. The questions "Where do I put my methods" and "What classes should I create" are answered by Design Patterns. Classes aggregate behavior (functions) and should do one thing (Single Reponsibility.)