C#: Can't include class from annother namespace [duplicate] - c#

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!
}
}

Related

CRTP implementation in c# [duplicate]

This question already has answers here:
New keyword: why is the derived method not called?
(2 answers)
C# & generics - why is method in base class called instead of new method in derived class?
(4 answers)
Closed last month.
I'm trying to implement Curiously recurring template pattern(CRTP) in c#.
here is some code i wrote.
using System;
using System.Collections;
using System.Collections.Generic;
// Curiously recurring template pattern in c#
namespace MyApp
{
public class Program
{
public static void Main (string[] arg)
{
new Child().CallChildMethod();
}
}
public abstract class Base <T> where T: Base<T>, new ()
{
// Game loop
void Upate ()
{
Method ();
}
public void CallChildMethod ()
{
T t = (T)this;
t?.Method ();
}
public void Method ()
{
Console.WriteLine ("Base Method!");
}
}
public class Child: Base <Child>
{
public new void Method ()
{
Console.WriteLine ("Child Method!");
}
}
}
In output i'm getting
Base Method!
but my code should print
Child Method!
any idea?
Expected
I want to access child class object in base class instead of overriding base methods and please suggest is there any other way to do the same?.

C#10: A namespace-scoped Access modifier [duplicate]

This question already has answers here:
C# access modifier for exposing class only within namespace
(3 answers)
Closed 12 months ago.
As in the title, I would love to see this in C#.
namespace PrivateStuff; //note: filescope namespace here!
public class PartiallyVisibleOutside
{
public int A;
namespace int B;
}
public class Test
{
public PartiallyVisibleOutside NC {get; private set;}
public void DoSmth()
{
NC.A = 100000 + NC.B; //OKAY til now!
Console.Write(NC.A);
}
}
//
namespace OUTSIDE;
public class OutsideClass
{
public PartiallyVisibleOutside Test;
void DoSmth()
{
Test.A = 100; //OKAY
Test.B = 100; //Compiletime-Error!
}
}
Does smth like this exist? Or shall i open up a feature-request at C#
There is no such feature and I think it is very niche thing to be implemented in near feature. Though you can simulate it by moving Test and PartiallyVisibleOutside into separate project (assembly) and marking B as internal.

How do i access the same variable in multiple classes? (C#) [duplicate]

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.

How to use method from one file on other file [duplicate]

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.

Accessing a Nested Class [duplicate]

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();

Categories

Resources