namespace PROJ.Service {
public static class ExceptionDatesUpdateService {
public static ExceptionDatesUpdateService()
{
}
public static bool IsServiceRunning() {
return _updateThread != null && _updateThread.IsAlive;
}
}
}
When I try to use a static class above, it says it is not accessible. Why? The reference exists.
using PROJ.Service;
namespace PROJ.admin {
public void ProcessRequest(HttpContext context) {
bool ch = ExceptionDatesUpdateService.IsServiceRunning();
}
}
Thanks everyone. The problem was found. When I solved other errors and compile it. It dissappeared. I am new to c#, sorry for fool question. :)
Assuming you've added the appropriate project reference, one problem I see is that static type initializers cannot have access modifiers. Try this:
public static class ExceptionDatesUpdateService {
static ExceptionDatesUpdateService()
{
}
}
Of course, if there's nothing inside the initializer, you can just remove it entirely.
This is often caused by different .NET framework versions being set in the project properties. For instance, one project may be .NET 3.5 and you're trying to reference a .NET 4.0 project.
It can also be caused by a similar x64 vs x86 mismatch.
Usually the reference itself will have a warning icon next to it.
Check if Target Framework is same in the project properties for both the projects.
namespace PROJ.admin
{
public static class NewClass
{
public void ProcessRequest(HttpContext context)
{
bool ch = ExceptionDatesUpdateService.IsServiceRunning();
}
}
}
Try this is working
Related
I'm struggling with proper dependencies versioning on releases. I've read in some articles that every change to AssemblyVersion in a library will cause the need of rebuilding all assemblies referencing this library. If it's true I need to be super careful when designing release cycles of dependent assemblies, right?
I wanted to verify the thesis by myself and... well, I noticed it is not the truth at all.
I made a console app which referenced dep1.dll (assembly version 1.0.0.0) and built it. Then I changed dep1.dll to version 2.0.0.0, built it and swapped it with version 1.0.0.0. When running console application I expected error "The assembly dep1.dll with version 1.0.0.0 couldn't be found". And you know what? Nothing special happened, the app used the library like it was targetting that version since the beginning.
ConsoleApp1/Program.cs
using dep1;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var a = new Class1();
a.Prop1 = "asdf";
Console.WriteLine(a.Prop1);
}
}
public class Implementation : Interface1
{
public int Super { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
}
dep1/Class1.cs
namespace dep1
{
public class Class1
{
public string Prop1 { get; set; }
}
public interface Interface1
{
int Super { get; set; }
}
}
As you can see, it's the same with interfaces.
Could you please explain why my test didn't work? This, other SO answers (1) and article by haacked on versioning interfaces really confused me.
I have created a .cs files that contain the following:
namespace SetUp
{
class Config
{
public static object SetConfig(int code, bool print)
{
//My Code
}
}
}
Compiled it and added the reference to my main project called 'CSharp Side', for example. Added it to my project and everything is great. But my question is how do I access 'SetConfig()'? Because it doesn't recognize 'SetUp' or 'Config' in my code.
Simply make your class as public.
namespace SetUp
{
public class Config
{
public static object SetConfig(int code, bool print)
{
//My Code
}
}
}
You can reference code in a different assembly by fully qualifying:
SetUp.Config.SetConfig(1, true);
or include the namespace with a using directive:
using SetUp;
class SomeClass
{
void SomeMethod()
{
Config.SetConfig(1, true);
}
}
Also, both the class and the method in the referenced assembly need the public modifier. Otherwise they won't be visible outside the assembly where they are defined.
there are similar questions here but this one is specific. I have a solution and in it two projects. Main project and run time project, in order to use run time project i need to add reference to it in main project. In run time project i need to use static object from main project, in order to do that i need to add reference which i cant to because there would be circular dependence. I read that i could use API function how can I implement that?
Thanks.
namspace mainProject
{
public static MyClass Object;
}
public sealed class RuntimeComponentClass : IBackgroundTask
{
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
//here i need to access static object from main project
_deferral.Complete();
}
}
First thing is that you cannot declare a static object just inside the namespace. I am assuming you have a class declared inside the namespace and static object is a member of that class
if you have following
namspace mainProject
{
public class AStaticClass{
public static MyClass Object;
}
}
you can definitely access the public static member from other classes.
public sealed class RuntimeComponentClass : IBackgroundTask
{
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
AStaticClass.Object is accessible
//here i need to access static object from main project
_deferral.Complete();
}
}
However this is not a good idea to have static public member if you have any multi threading scenario.
This is in support of my comment on the original post.
I we solved the problem by moving all the classes in run time component project, in this way that one reference that exists is enough. Thanks everybody!
I have a solution that has many projects in it.
The SqlSmoke.Objects project references the SqlSmoke.Data project.
I set a reference in SqlSmoke.Objects to the solution SqlSmoke.Data. I am then able to compile the entire solution.
However,SqlSmoke.Data does not appear in the intellisense as shown below. However, when I change my namespace to SqlSmoke.Fred, I do see SqlSmoke.Data in intellisense.
I don't see any circular references or other warnings in the Output window that suggest that something else is going on.
What might I look for to understand why I cannot reference objects in the Data project from the Objects project?
using SqlSmoke.Data;
namespace SqlSmoke.Objects
{
public class Class2
{
public void Junk()
{
SqlSmoke.Data. //No intellisense
}
}
}
If I change the Namespace, I get Intellisense:
using SqlSmoke.Data;
namespace SqlSmoke.ObjectsChangedNamespace
{
public class Class2
{
public void Junk()
{
SqlSmoke.Data.CodeObjectData.AddCodeObject("Test"); //Now I see intellisense
}
}
}
You have a [sub]namespace/class name collision. Use:
public void Junk()
{
global::SqlSmoke.Data.
}
and it will work.
Question:
Is it possible to reference public static methods held within the CustomAction class, Votive generates for creating C# managed Custom Actions, from other libraries within the same solution?
I'm having trouble getting a reference to the class and method inside my C# library for the C# Custom Action when trying to create a test bed for the CA.
namespace TestInstaller.InstallCA
{
public class CustomActions
{
[CustomAction]
public static ActionResult InstallUIStart(Session session)
{
//Stuff
return Begin(<Constructed DataClass>);
}
public static ActionResult Begin(DataClass dc)
{
//Stuff I want to test
}
}
}
...
namespace TestInstaller.InstallerTest
{
static class Program
{
Static void Main()
{
//Stuff
//This line is not valid.
TestInstaller.InstallCA.CustomActions.Begin(<Constructed DataClass>);
}
}
}
Despite me adding a reference to InstallCA I cannot add a using statement for TestInstaller.InstallCA or InstallCA, and the compile time error only suggests adding a reference, which I have done.
Is this anything to do with Votive protecting its DLLs somehow?