Why does this implementer method not see its sibling? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a class that implements an interface:
public class SQLiteHHSDBUtils : IHHSDBUtils
{
void IHHSDBUtils.SetupDB()
{
. . .
if (!TableExists("AppSettings"))
. . .
bool IHHSDBUtils.TableExists(string tableName)
{
. . .
It can't find its own brother sitting right below it (the if (!TableExists()):
The name 'TableExists' does not exist in the current context
How can it / why does it not see it?

You have an explicit interface implementation. You can't access your interface methods directly unless you cast current instance to interface type:
if (!((IHHSDBUtils)this).TableExists("AppSettings"))
From 13.4.1 Explicit interface member implementations
It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

When you explicitly implement an interface, you need to access the interface member from a variable whose type is exactly the interface (not an implementing type).
if (!TableExists("AppSettings")) is calling TableExists via the this object, whose type is SQLiteHHSDBUtils, not IHHSDBUtils.
Try:
if (!((IHHSDBUtils)this).TableExists("AppSettings"))
Alternatively, don't explicitly implement the interface:
public class SQLiteHHSDBUtils : IHHSDBUtils
{
// ..
bool TableExists(string tableName)
{
// ..

TableExists is an explicit implementation. If you want to access it, you have to cast this to IHHSDBUtils:
void IHHSDBUtils.SetupDB()
{
. . .
if (!((IHHSDBUtils)this).TableExists("AppSettings"))

Related

Casting a Generic Class<T> in C# from an Interface? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a generic class based on an Interface
public class SomeClass<T>() : ISomeClass
{...}
and in it, I have a public method, which is an implementation of the ISomeClass interface and returns a SomeClass<T> type.
public SomeClass<T> SomeMethod()
{...}
Then I declare a variable somewhere else, based on the interface
private ISomeClass _someVariable;
Now, if I instantiate the class...
_someVariable = new SomeClass<SomeType>();
... I can access my public method
var anotherVariable = _someVariable.SomeMethod();
However, it returns an ISomeClass type and not a SomeClass<T>type. Why? Is this because I declare _someVariable as ISomeClass? (I've tried casting it as SomeType<T> as well, but still get a type ISomeClass.) What am I missing? (other than a whiskey and some sleep?)
if someVariable is ISomeClass (which it is), then the relevant question is: what type does ISomeClass.SomeMethod() return, because that is the definition being used; I'm guessing it returns ISomeClass, which is why anotherVariable is typed as ISomeClass.
To use the SomeClass<T>.SomeMethod() signature that returns a SomeClass<T>, _someVariable would need to be known as a SomeClass<T>, not just an ISomeClass.
Note that your SomeClass<T>.SomeMethod() method won't actually satisfy that interface, so you probably also have an explicit interface implementation somewhere:
ISomeClass ISomeClass.SomeMethod() => SomeMethod();
(which highlights the same problem)

class.forName equivalent in c# without creating an instance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to dynamically access classes in c# in the way like Java class.forName(). I have only found things like Class.forName() equivalent in .NET? but I don't want to create instances.
In detail: I have got a simple text file containing a list of classes. I read them using file.ReadLine() (so I have got all class names as strings) and then I want to execute the same static method on each class: class1.method(); class2.method; and so on. The classes all exist and I need to access them. How can I do that?
C# doesn't support static interfaces (or static members in interfaces), so unless you want to use factory classes (the usual approach), you'll need to use reflection to invoke the static method directly.
void Main()
{
Console.WriteLine(Type.GetType("A").GetMethod("Hi").Invoke(null, new object[] {}));
}
class A
{
public static string Hi() { return "Hi!"; }
}
You might want to use a fully-qualified name for the type to make this work really well. Using just the name of the type is tricky, especially when you're trying to invoke types from other assemblies (which you probably are, otherwise there'd be no reason to use reflection - just use a dictionary of delegates or whatever).
You can use System.Reflection to load the Assembly into memory and then drill down to the type followed by getting the required method and invoke.
Refer to the documentation
GetMethod
If you have names of your desired Type then you can use Type.GetType(string) method.
Example if you have a class like this :
namespace MeProgram.BusinessLogic
{
public class MeObject {}
}
Full class name of that object should be "MeProgram.BusinessLogic.MeObject".
Now you can use that name inside of Type.GetType(string) method like such :
string className = "MeProgram.BusinessLogic.MeObject";
Type classType = Type.GetType(className);

How can I limit class not to implement all the methods of Interface in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Somewhere I have read this question. How can we handle situation like this:
I have an interface, In that I have four methods: Add, Subtract, Multiply, Divide.
I have two classes A and B.
I want A and B to implement this Interface. But I want situation like this:
A can access only Add, Subtract.
B can access only Multiply, Divide.
Please tell me how is this possible in C#?
Or by some trick if this is possible please let me know.
The point of an interface is to define a contract between two objects. If you are saying that your object only wants to implement some of the contract, then that breaks the meaning of the interface.
Why don't you use multiple interfaces, one for Add/Subtract and another for Multiply/Divide. Your class can implement any or both of these interfaces.
A class that implements an interface must implement all of its methods. The documentation says:
A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
It seems to me that what you need is two interfaces:
IAdditiveOperators which implements addition and subtraction.
IMultiplicativeOperators which implements multiplication and division.
Implementing classes can then implement one or other or both.
Split the interface to two interfaces: one with Add and Substract and name IAddSubstract and another one with Multiply and Divide with name IMultiplyDivide. Then you can add another interface (IOperation) which implements IAddSubstract and IMultiplyDivide
You have two ways to go about it:
Either split up your methods between two separate interfaces and implement one interface containing Add and Subtract in class A and the other one containing Multiply and Divide in class B. That of course means you have two interfaces instead of one, so decide if this is a problem.
OR
If you insist on having only one interface, you can declare A and B as abstract (which of course means you cannot instantiate them)
If going the abstract route, you need to mark interface methods you don't want to implement as abstract as well.
You can't avoid implementing all methods of the interface. If you inherit the interface you have to fulfil it.
In some situations some methods of an interface can't have a useful implementation for a specific class. After you have come to the conclusion that you should implement the interface despite this, there are some things that you can do:
You can implement a method as doing nothing. If the class already does what's expected without it, you can just accept the method call and silently do nothing.
You can throw a NotSupportedException, if some result is expected by calling the method, that the class can't fulfil. Naturally this should only be done if the method is not crucial for how the interface is supposed to be used.
Also, you have the choise of implementing interface members implicitly or explicilty. Implicitly is the normal way, where the member is visible both when the type of the reference is the interface and when it's the class.
To implement a member explicitly makes it only visible when the type of the reference is the interface, not when it's the class.
If the Multiply method is implemented explicitly in the class A (and the interface is named ICanCalc):
A obja = new A();
ICanCalc infa = new A();
infa.Multiply(); // works fine
obja.Multiply(); // gives a compiler error
However, the method is only hidden, you can still use it by simply casting the reference:
(ICanCalc)obja.Multiply(); // works fine

implementing interface from other assembly can be protected? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I m implementing interface which is in other assembly as projected. I have added assembly reference and name space but cannot access it ? I want to implement it in my class and of course interface and class are in different assemblies. I m getting compile time error "cant access due to protection level.."
to me it looks weried. Can't I access this interface without making it public ?
Code:
namespace DAL
{
interface IStdService
{
}
}
You cannot have anything in a namespace marked protected. Only internal and public are possible.
It is only possible to do these since the others won't make sense. As O.R. Mapper already said, this only makes sense if they are part of an class already.
If you write this code:
protected interface IInterface
{
}
It gives the error at compile time:
Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
This does compile:
internal interface IInterface
{
}
and it will make the interface available only inside the assembly, unless you make it available through the InternalsVisibleTo attribute.
The code in your latest sample:
interface IInterface
{
}
marks the interface internal which is the default for classes, interfaces, etc, but with interfaces this means they are only available inside the assembly, as explained before.

How do I create this class (involving indexer properties?) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working with C#.NET and basically have a page containing many areas.
In my code-behind, I basically want to be able to do something like:
bool result1 = MyClass.Section["area1"].Process();
bool result4 = MyClass.Section["area4"].Process();
I need to write a class that would call some kind of "Process" method and be able to have it accept a parameter like "area1" inside that method.
Any help on getting me started with this would be greatly appreciated, thank you!
Following the normal .NET naming conventions I'll assume you mean, by your example, that MyClass is being referenced statically rather than by instance (which may not be a big change). Given that assumption, it appears you have a class like:
static class MyClass
{
public static IIndexer Section { get; }
}
IIndexer in this case could be any type that implements an indexer property that takes a string and returns a type that has a method named Process which in turn returns a bool. IIndexer could theoretically look like:
interface IIndexer
{
ISomething this[string] { get; }
}
Next we'll fill in the ISomething blank above with a simple IProcess interface so we don't have to know anything about your specific implementation:
interface IProcess
{
bool Process();
}
So now the indexer above can be changed to:
IProcess this[string] { get; }
Of course, none of the above has any real executable code, but outlines the objects necessary to do what you're after. Now when you go to run your code using your fulfilled contracts the call chain is pretty simple:
bool result1 = MyClass.Section["area1"].Process();
// MyClass.Section > IIndexer.this[string] > IProcess.Process
To POC the idea, a good way to mock the IIndexer implementation might be to use Dictionary<string, IProcess> as it'll give you a usable indexer for your purposes.

Categories