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)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm currently working on a filter for a LINQ query in ASP.NET 4.5. I retrieve 2 different IQueryable of different table types.
I would call my filter method this way:
var result = new Dictionary<DateTime, MyData>();
if(x = 1)
{
var base = (from t in ctx.OldProjctStatistic
select t);
result = GetProjectData(base);
}
else {
var base = (from t in ctx.NewProjectStatistic
select t);
result = GetProjectData(base);
}
I would need to write a method to filter this result independant of it's type. My current implementation looks like this:
private static Dictionary<DateTime, MyData> GetProjectData(IQueryable<T> base) where T : class
{
// this method stub doesn't work unfortunately
return null;
}
Unfortunately I'm getting the following error on declaration of my filter method:
Constraints are not allowed on non-generic constraints
Do you know how to write a generic method, to filter results of 2 different types?
Thank you!
For the parameter T to be recognized as a generic parameter it has to be enclosed in < > parentheses which denote a generic parameter. Further more this has to be included in the signature of the method if you want to apply contraints in this method on the generic parameter:
private static Dictionary<DateTime, MyData> GetProjectData<T>(...) where T: ...
if the compiler cannot find the declaration in the method it will search on class level, whether it can find a generic class
public class MyClass<T>{...}
If it cannot find either cases it will assume that T is simply some object type (may be you own invention) and tell you that you cannot constrain a normal type because it is already defined...
For more information on generic methods an constrains follow the link to the Microsoft programming-guide
Generic constraints are only possible in generic methods. So you have to make your method generic:
private static Dictionary<DateTime, MyData> GetProjectData<T>(IQueryable<T> base) where T : class
{
// this method stub doesn't work unfortunately
return null;
}
You don't have to change your call, since the type argument can be inferred.
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 6 years ago.
Improve this question
I have the following method:
IResponseToModelConverter<U, IEntity> CreateConverter()
{
return new ResponseToItemConverter() as IResponseToModelConverter<U, IEntity>;
}
ResponseToItemConverter inherits from a base class that implements IResponseToModelConverter.
Item is type of IEntity, and I can get the type of U.
Is there a way for this to work?
I replaced IEntity with type parameter T (with IEntity constraint) so I can pass a concrete class when I cast to the interface. That solved my problem.
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 7 years ago.
Improve this question
In C#, why doesn't field initializer syntax not throw compilation errors, however, method calls do? For example,
class SomeOtherClass {
void SomeMethod() { }
}
class SomeClass {
SomeOtherClass someOtherObject = new SomeOtherClass();
someOtherObject.SomeMethod();
}
Why does the someOtherObject initialization, which essentially is a constructor call work? My understanding is that a constructor is also a method. However the second line, where there is a method call, does not work. Is this a language design choice?
SomeOtherClass someOtherObject = new SomeOtherClass();
is syntactic sugar for this anyway:
SomeOtherClass someOtherObject;
public SomeClass() {
someOtherObject = new SomeOtherClass()
}
So nothing is executed outside of the constructor. The compiler will create an empty constructor if it doesn't exist and initialize someOtherObject.
A Class contains attributes and methods declarations and definitions only. Attributes can be initialized right there as you're doing
SomeOtherClass someOtherObject = new SomeOtherClass();
and in the constructors.
Doing
someOtherObject.SomeMethod();
is neither declaration/definition nor initialization. Which is not allowed.
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"))
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.