This question already has an answer here:
How to make method call another one in classes?
(1 answer)
Closed 3 years ago.
I want to use a method from one file (class) in another one. You can see
// first class file
namespace AutomatskI
{
class Method{
public void client()
{
// some code
}
// second class file
namespace AutomatskaIA
{
class AutomaticTestRun
public void login()
{
// Here i want to use that code
client();
}
You should reference your first library in the second one
// Reference your first library like this
using AutomatskI;
namespace AutomatskaIA
{
class AutomaticTestRun
{
public void login()
{
// Then reach this class method like this
Method method = new Method();
method.client();
// Or use a static class instead by putting a 'static' tag in class name.
// So you don't have to create an instance of this class in order
// to use its methods. Then you can do it like this:
// Method.client();
}
// ...
And please format your code before posting so people would have an easier time reading your code.
Related
This question already has answers here:
Call method in class from another class in C#
(5 answers)
Closed 1 year ago.
I have a strange problem.
I created two classes AdvertisementHelper(namespace AdvertisementHelper) and Doof (namespace blabla), now I want to use the method bad_code from class Doof in class AdvertisementHelper.
AdvertisementHelper.cs:
using blabla;
namespace AdvertisementHelper
{
class AdvertisementHelper
{
Doof d = new Doof();
d.bad_code();
}
}
Doof.cs:
namespace blabla
{
class Doof
{
public void bad_code()
{
}
}
}
This is not my first C# program, I have done this many times and I never had such problems.
blabla and AdvertisementHelper are part of the same Visual Studio project.
d.bad_code and bad_code is not defined in this context
.NET Framework 4.7.2
You cannot have code (a method call, that is) floating around just anywhere in a class.
class AdvertisementHelper
{
Doof d = new Doof(); // <= OK, because interpreted as class field
d.bad_code(); // <= doesn't work!
}
You need to put it into a method, for example.
class AdvertisementHelper
{
Doof d = new Doof(); // <= OK, because interpreted as class field
public void Execute()
{
d.bad_code(); // <= Inside a method = OK!
}
}
This question already has answers here:
Share a variable between two classes
(2 answers)
Closed 3 years ago.
I want to modify a variable in one class and iT to be accessed in another class in my program. How do i do this? and if there is, will the variable be updated in one class when i change it in another.
Most simple solution: define it as public static in Program class. Then access it from any class with Program.var_name.
You can follow the dependency injection pattern, there are some libraries to help you if you have a big project but if you just want to do something small you can hand craft it.
Create a class that will contain the shared variable
class SharedClass{
public int commonVar{get;set;} //not threadsafe
}
Every class that needs to have access to it it must get a reference to it through constructor.
class ConsumerOne{
SharedClass shared;
public ConsumerOne(SharedClass shared)
{
this.shared = shared;
}
public IncreaseThat(){
shared.commonVar++;
}
}
class ConsumerTwo{
SharedClass shared;
public ConsumerTwo(SharedClass shared)
{
this.shared = shared;
}
public DecreaseThat(){
shared.commonVar--;
}
}
And at your Program main you make the binding.
main(){
var shared = new SharedClass();
var one = new ConsumerOne(shared);
var two = new ConsumerTwo(shared);
one.IncreaseThat();
Console.WriteLine(shared.commonVar);
two.DecreaseThat();
Console.WriteLine(shared.commonVar);
}
That way you can tell what your classes are using and you will skip global variables.
This question already has answers here:
Organizing c# code into different files
(4 answers)
Closed 8 years ago.
I recently inherited some code that is several thousand lines long and extremely disorganized.
I'm trying to re-factor it so that code is at least easier to find, but since it was built in visual studio, everything is contained within a single "form" class, and the way it's written makes it difficult to separate code without breaking something.
Is there a way that I could have the code live in a different file, but still keep it in the same class?
Yes. Just use the partial keyword for each "part" of the class in the class' files.
// example class A in file: A1.cs
public partial class A { }
// example class A in file: A2.cs
public partial class A { }
More information can be found at MSDN and a thousand other sites and blogs.
Also consider using #region directive to split the code into more navigable chunks. This will also be easier to navigate to since the one file can keep the name with the class, and no one has to dig for the various pieces of this same class in different files.
Yes, it is possible. This is the way Visual Stuido generates code for things like Win Forms. It enable to split the implementation into two separate files, and enables the user to alter just one and thus reducing the probability to screw something up.
The concept is called partial classes. You define the same class in two different files, and but class definitions need to have the word partial in font. It is also possible for structs and interfaces.
Read more here:
http://msdn.microsoft.com/en-us/library/wa80x488.aspx
Use partial classes. For example:
If you have a class in Example.cs as such:
public class Example
{
public void Func1()
{
}
public void Func2()
{
}
public void Func3()
{
}
public void Func4()
{
}
}
You can easily change it to:
Example.cs
public partial class Example
{
public void Func2()
{
}
public void Func4()
{
}
}
RestOfTheExample.cs
public partial class Example
{
public void Func1()
{
}
public void Func3()
{
}
}
Hope this helps!!!
By using the partial classes as for ex
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why Would I Ever Need to Use C# Nested Classes
I'm doing it shorty, I have a class which looks like this:
namespace Blub {
public class ClassTest {
public void teest() {
return "test";
}
public class AnotherTest {
public void blub() {
return "test";
}
}
}
}
I can access to the function called "teest" like this, but how can I access to the function "blub" without doing another "new ClassTest.AnotherTest()"?
Accessing to the function teest:
Blub.ClassTest = new Blub.ClassTest();
ClassTest.teest(); //test will be returned
My try (and how I want it to, to access on AnotherTest is this:
Blub.ClassTest = new Blub.ClassTest();
ClassTest.blub(); //test will be returned
Which don't work, I can just access to AnotherTest like this, how I dont want it:
Blub.ClassTest2 = new Blub.ClassTest.AnotherTest();
ClassTest.blub(); //test will be returned
Does someone know a solutions for this?
You're declaring AnotherTest inside ClassTest, that's why you have to browse for it using namespace.class.2ndClass.
However, I suppose that you're not much aware of OO concepts, are you? If you declare a method inside a class, it will only be available for objects of that class, unless you declare it as static, what means that it would be a class method rather than a instance method.
If you want ClassTest to have 2 methods (teest and blub) simply declare both at the body of the class, like:
public class ClassTest
{
public string teest()
{
return "test";
}
public string blub()
{
return "test";
}
}
Also, note that if a method is declared as void it won't return anything (in fact, I think that your original code wouldn't even compile at all).
I'd recommend you to study OO a little deeper before trying to figure things out at your own.
If you need access to another class you have to make it a property in the first class.
namespace Blub {
public class AnotherTest {
public void blub() {
return "test";
}
}
public class ClassTest {
public AnotherTest at = new AnotherTest();
public void teest() {
return "test";
}
}
}
Then access it like this:
ClassTest x = new ClassTest();
x.at.blub();
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.)