Interface reference problems in c# - c#

i'm trying to learn interfaces, and got stuck on this problem..
i have 3 libraries..
calculateLibrary
arguments.cs ,
calculator.cs ,
calculatorMain.cs ,
commandTypes.cs ,
inputParser.cs ,
setInput.cs
InterfacesLibrary
Iarguments.cs ,
Icalculator.cs ,
IcalculatorMain.cs ,
IinputParser.cs ,
IsetInput.cs
typescript(web)
index.aspx
so my problem is some of my interfaces methods is not working, or it says "the type or namespace name 'commandTypes cound not be found(are you missing a using directive or an assembly reference?)
the commandTypes in my IinputParser is what the error is pointing.
code for my interface
namespace calculateLibrary
{
public interface IinputParser
{
commandTypes parseCommand(string command);
}
}
and code for the method that inherits the interface
namespace calculateLibrary
{
public class inputParser : IinputParser
{
public commandTypes parseCommand(string command)
{
return ((commandTypes)Enum.Parse(typeof(commandTypes), command));
}
}
}
this is the code for my commandType class
namespace calculateLibrary
{
public enum commandTypes
{
add,
sub,
mul,
div
}
}
i think the problem is not about reference..because some of my Interfaces is working fine..i mean there is no error.. i saw some related post but those didn't help me.
thanks.

Since both IinputParser and inputParser need to use commandTypes, I would suggest moving commandTypes.cs to InterfacesLibrary project to avoid circular reference. The namespace needs to be changed as well. Here's what the code of commandTypes should look like after being moved to InterfacesLibrary project:
namespace InterfacesLibrary
{
public enum commandTypes
{
add,
sub,
mul,
div
}
}
The code of IinputParser should have InterfacesLibrary as the namespace
namespace InterfacesLibrary
{
public interface IinputParser
{
commandTypes parseCommand(string command);
}
}
and here's the code of inputParser class:
using InterfacesLibrary;
namespace calculateLibrary
{
public class inputParser : IinputParser
{
public commandTypes parseCommand(string command)
{
return ((commandTypes)Enum.Parse(typeof(commandTypes), command));
}
}
}

It seems the problem is Circular Reference as you have declared commandTypes in your CalculateLibrary and use it in IinputParser Interface in public commandTypes parseCommand(string command) which is in InterfaceLibrary, then your want to reference InterfaceLibrary from CalculateLibrary.
CommandTypes (CalculateLibrary) -> IinputParser (InterfaceLibrary) -> inputParser (CalculateLibrary)
you should either put these classes used by your Interfaces in InterfaceLibrary, or declare them in a lower level library as somthing like Core and then reference it from both InterfaceLibrary and CalculateLibrary

Related

Xamarin DependencyService

I followed other questions in stack overflow and made sure my register in the assembly registers the android implementation and not the base interface and also that all classes are public. Anyhow I still get the System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor'message.
I declare my base interface in the common project at Foodies/Views/VisualEffects/iStatusBarColor.cs, like this:
namespace Foodies.Views.VisualEffects
{
public interface IStatusBarColor
{
void MakeMe(string color);
}
}
Then in my android project I add StatusBarColor_Android, looking like:
using Android.OS;
using Foodies.Droid;
using Foodies.Views.VisualEffects;
using Xamarin.Forms.Platform.Android;
[assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]
namespace Foodies.Droid
{
public class StatusBarColor_Android : IStatusBarColor
{
public void MakeMe(string color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var c = MainActivity.context as FormsAppCompatActivity;
c?.RunOnUiThread(() => c.Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(color)));
}
}
}
}
and them from my main page I call:
var dp = DependencyService.Get<IStatusBarColor>();
dp?.MakeMe(Color.Blue.ToHex());
And there, i the DependencyService.Get I do get the System.MissingMethodException: 'Default constructor not found for type Foodies.VisualEffects.IStatusBarColor
This is my android project settings
Im new to Xamarin, could someone help me finding the error ??
Found out in addition to all of this, in the MainActivity, in the OnCreate I had
DependencyService.Register<IStatusBarColor>();
Just remove this sentence and it works nice.
Additionally, changing the registered class too StatusBarColor_Android, and not the base interface also works. I found it just cleaner to remove the whole thing and let the [assembly...] do the job
The namespace in the code above is confusing,as you said
I declare my base interface in the common project at
Foodies/Views/iStatusBarColor.cs
Normally if you don't customize namepace, it should be:
namespace Foodies.Views
{
public interface IStatusBarColor
{
void MakeMe(string color);
}
}
and in you Android project,why you have two different reference using Foodies.Droid; and using Foodies.VisualEffects.Droid;,and you didn't reference the namepace Foodies.Views when you implement IStatusBarColor
Try to change like :
using Foodies.Droid;
using Foodies.Views;
using Android.OS;
using Xamarin.Forms.Platform.Android;
[assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]
namespace Foodies.Droid
{
public class StatusBarColor_Android : IStatusBarColor
{
public void MakeMe(string color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var c = MainActivity.context as FormsAppCompatActivity;
c?.RunOnUiThread(() => c.Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(color)));
}
}
}
}
Update :
The cause of this problem is the wrong registration method.There are two ways to register.
1.Register in your implementation class directly,like the codes above
using [assembly: Xamarin.Forms.Dependency(typeof(StatusBarColor_Android))]
2.call DependencyService.Register<StatusBarColor_Android >(); in your MainActivity OnCreate method.(Note:here you should use the class name which you implement the interface,not the name of interface).

Namespaces Scope

I have a class named AuditLog inside Domain.AuditLog namespace. I want to use AuditLog class inside another class with namespace ApplicationServices.AuditLog. like:
using Domain.AuditLog;
namespace ApplicationServices.AuditLog
{
public interface IAuditLogService
{
List<AuditLog> GetAuditLogs();
}
}
It says 'ApplicationServices.AuditLog' is a 'namespace' but is used like a 'type'. I know I can solve this using like:
namespace ApplicationServices.AuditLog
{
using Domain.AuditLog;
public interface IAuditLogService
{
List<AuditLog> GetAuditLogs();
}
}
Is there another way of referencing Domain.AuditLog ?
Maybe this could help you:
using AL = Domain.AuditLog.AuditLog;
namespace ApplicationServices.AuditLog
{
public interface IAuditLogService
{
List<AL> GetAuditLogs();
}
}

How to declare and use the namespace of one class into another class

I Have created two .cs files with namespaces ,classes and methods . I want to call the classes of one .cs file into another .cs file. Can u help me how to declare namespace and use the namespace so that i can call the classes of the preceding .cs file.
Please forgive if my explanation is not correct.
Suppose i have the following code.
ClassFile1
using system
namespace namespace1
{
class c1
{
Methods()
}
}
ClassFile2
using system
//here i need to declare the namespace1 .Can u help me how to declare namespace1 in this ClassFile2//
namespace namespace2
{
class c2
{
Methods()
}
}
You can reference the fully-qualified name of the class:
namespace SecondNamespace
{
public class SecondClass
{
private FirstNamespace.FirstClass someObject;
}
}
Or you can add a using directive to the file (note, this is at the file level, not the class level) to include a specific namespace when resolving type names:
using FirstNamespace;
namespace SecondNamespace
{
public class SecondClass
{
private FirstClass someObject;
}
}
Taken from here:
namespace SampleNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
// Create a nested namespace, and define another class.
namespace NestedNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside NestedNamespace");
}
}
}
class Program
{
static void Main(string[] args)
{
// Displays "SampleMethod inside SampleNamespace."
SampleClass outer = new SampleClass();
outer.SampleMethod();
// Displays "SampleMethod inside SampleNamespace."
SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
outer2.SampleMethod();
// Displays "SampleMethod inside NestedNamespace."
NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
inner.SampleMethod();
}
}
}
Note also that sometimes in addition to the "using" entry (I'm not quite clear on how you app is structured, if it's all one project this is probably moot) you may also need to add the reference. Also not sure what environment you're using. From VSExpress while in the project/file that's the recipient click on Project - Add Reference, select solution and then select your namespace.

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

COM / .Net interoperability: tlbexp prefixes conflicted names with 'namespace_', how to workaround?

I want to implement a .Net wrapper around a Com Coclass (defined in a C++ dll), that has the exact same name and interface name as the COM object, but within in a different namespace. When I try to do that, I find that tlbexp adds a prefix to the name of the class (the exported type becomes namespace.namespace_classname). This is annoying because then the tlb file cannot be used easily in VBA for instance.
Here is an example to make things clearer
The C++ code is something like:
namespace CPP
{
public interface IComObjectTest
{ ... }
public class ComObjectTest : IComObjectTest
{ ... }
}
The C# code is something like:
namespace CS
{
public interface IComObjectTest : CPP.IComObjectTest
{
...
}
public class ComObjectTest : CS.IComObjectTest
{
private CPP.ComObjectTest _test;
public ComObjectTest(CPP.ComObjectTest test) { _test = test; }
public CPP.ComObjectTest GetComObjectTest { get { return _test; } }
...
}
}
When I compile the C# project and run tlbexp.exe on it. I get the following types exported:
CS.CS_ComObjectTest
CS.CS_IComObjectTest
while I would like to see:
CS.ComObjectTest
CS.IComObjectTest
Is this a limitation that can be overcome?
Any help appreciated,
Thanks

Categories

Resources