Call static method in another class library - c#

How to call static method in another class library? Because I created 2 class library Common and another call DAL, and I already add Commmon dll reference in my DAL class library, after that I cannot find out my method name. Isn't got other method to solve it?
Class library : Common
using System;
namespace Common
{
public class Log
{
public static bool Error(Exception ex)
{
return true;
}
}
}
Class library : DAL
using Common
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class MenuD
{
Log.;
}
}

You can't call the Log.Error() method from that location in the class, outside of a method.
Call it from within a method in the MenuD class.
public class MenuD
{
public void SomeMethod()
{
try
{
// do something that could throw an exception
}
catch (Exception ex)
{
var isLogged = Log.Error(ex);
}
}
}

Related

Where in my static class can I initialize a Trace Listener?

I want to add Debug and Trace to a class that I've created. However, I don't know where in my code to initialize a Trace Listener.
Here is the code I want to add to my static class:
Trace.Listeners.Add(
new TextWriterTraceListener(
File.CreateText("log.txt")
)
);
Trace.AutoFlush = true;
Here is the class to which I want to add the Trace Listener:
using System;
using System.Diagnostics;
using System.IO;
namespace Example.Shared
{
public static class ExampleClass
{
public static string SaySomething()
{
Trace.WriteLine($"I'm gonna say something");
return "Something";
}
// etc.
}
}
Apart from this class, I have only created some unit tests using Xunit. I have not yet created the application that will use this class.
This is what the unit test code looks like:
using Example.Shared;
using Xunit;
namespace ClassLibTests
{
public class ExampleClassTests
{
[Fact]
public void TestSaySomething()
{
string expected = "Something";
string actual = ExampleClass.SaySomething();
Assert.Equal(expected, actual);
}
// etc.
}
}
I execute the above tests at the command line with the command dotnet test.
In order to initialize any static class or static members of a class you can use static constructors. So, you can update your code as follow:
using System;
using System.Diagnostics;
using System.IO;
namespace Example.Shared;
public static class ExampleClass
{
static ExampleClass()
{
Trace.Listeners.Add(
new TextWriterTraceListener(File.CreateText("log.txt"))
);
Trace.AutoFlush = true;
}
public static string SaySomething()
{
Trace.WriteLine($"I'm gonna say something");
return "Something";
}
// etc.
}
For more details check:
C# static class constructor
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
BTW, I've also updated code to use the newly introduced file scoped namespace. Less indentation.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces

Do derived classes still depend on the imports of the parent class, even if the method referencing another assembly is being overridden?

I have a parent class that references an outside assembly (AutoCAD)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.EditorInput;
namespace WBPlugin
{
public class DoubleInputRetriever : IUserInputRetriever<Double>
{
public double getUserInput(string prompt)
{
return getUserInput(prompt, 16);
}
public virtual double getUserInput(String prompt, Double defaultValue)
{
Editor ed = Active.Editor;
PromptDoubleOptions pdo = new PromptDoubleOptions(prompt);
pdo.DefaultValue = defaultValue;
pdo.AllowNone = true;
pdo.AllowNegative = false;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n*Cancel*");
return 0;
}
if (pdr.Status == PromptStatus.None)
{
return defaultValue;
}
return pdr.Value;
}
}
}
And then a child class which I made in an attempt to be able to feed in "fake" data to my tool to be able to unit test it outside of AutoCAD.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WBPlugin;
namespace WBPluginTests.Fakes
{
public class FakeDoubleInputRetriever : DoubleInputRetriever
{
public double ReturnValue { get; set; }
public FakeDoubleInputRetriever()
{
}
public FakeDoubleInputRetriever(Double value)
{
ReturnValue = value;
}
public override double getUserInput(string prompt, double defaultValue)
{
return ReturnValue;
}
}
}
I am unable to run the unit tests because it can't find a certain AutoCAD assembly, which makes sense since I'm trying to do tests outside of AutoCAD so the assembly isn't available.
Is the problem that the parent class is trying to run but can't since the required assembly can't be found? Even though it's the child class that I'm using in the test, and the overridden method that would be used?
Inheritence is the strongest kind of dependency. Creating a fake implementing IUserInputRetriever<Double> decouples you from the concrete implementation that is depending on AutoCAD.

PreApplicationStartMethod cannot be resolved

I'm trying to use Unity.Webforms in a web application without success. As per the documentation, I put the following line in the AssemblyInfo.cs to tell the runtime to invoke a method before the application starts:
[assembly: PreApplicationStartMethod(typeof(Unity.WebForms.PreApplicationStart), "PreStart")]
The error I'm getting says:
The method specified by the PreApplicationStartMethodAttribute on assembly ... cannot be resolved
And this is the class holding the method (within App_Start folder):
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System;
using System.Collections.Generic;
using System.Web;
namespace Unity.WebForms
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
Is there anything I'm missing or misunderstanding?
Thanks
Change Unity.Webforms namespace in your custom file with PreApplicationStart, because this namespace already exists.
My solution, that works:
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
namespace TestApp.WebUi
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
And:
[assembly: PreApplicationStartMethod(typeof(TestApp.WebUi.PreApplicationStart), "PreStart")]

accessing a method in a dll

okay, so to start with i have set up the references in the project that i am useing the dll in.
what i am trying to do is access the method "haha" in my utils dll
code for dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
public class kb
{
public class yes {
public void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
}
}
and in the project im trying to access haha in i have just "Utils.kb.yes" but there is no method in that.. all i can do is Utils.kb.yes.equals and Utils.kb.yes.ReferenceEquals.
Since haha() is an instance method, you need to create an instance of the Utils.kb.yes class first:
Utils.kb.yes kb = new Utils.kb.yes();
kb.haha("nextproblem");
Or you also can make the method static:
public class yes {
public static void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
then you can call it like this:
Utils.kb.yes.haha("I am static!");
Your classes do not have a constructor, and besides that, you simply CAN'T do much with a class before instantiating an object out of it. So you should reference your dll, and then create a new object first. From within that object, you can then reference your method(s).

How to derive a class from a nested interface?

The following code is not compilable:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) { }
}
class Class : Class.Interface
{
internal interface Interface
{
}
}
}
The error message is:
error CS0146: Circular base class dependency involving 'ConsoleApplication1.Class' and 'ConsoleApplication1.Class.Interface'
Don't understand this.
Update:
This is probably more "motivating" (-;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) { }
}
class Container : Container.Interface
{
// Everything, that is of type "Container.Interface" can be used as child here.
// ... including the container itself.
Interface[] _children;
// Is nested to keep the naming consistent.
internal interface Interface
{}
}
}
Wenn I put the interface outside of class "Container", it should be named somthing like "ContainerChildInterface". In my project I will have several classes like this, and thus several interfaces. And I think, using nested interfaces would be much better style in this case.

Categories

Resources