VB.Net and access via a variable of an interface type - vb.net

How do I make the properties of a class available in an inheriting class, for a variable that is declared to be the type of one of the interfaces implemented by that class?
What I have done so far is to create an abstract class MyAbstract with the keyword MustInherit and in the inheriting class MyInheritingClass I have added inherits and then the name of the abstract class. Now this is all fine, but in my inheriting class, if I create an interface on that class MyInterface and use that interface elsewhere in my code, I have then found that I cannot see the properties from my abstract class, on the variable declared with that interface.
Am I doing something wrong here, or is there something else that I need to do?
An example would be as follows:
Public MustInherit Class MyAbstract
Private _myString as String
Public Property CommonString as String
Get
Return _myString
End Get
Set (value as String)
_myString = value
End Set
End Property
End Class
Public Class MyInheritingClass
Inherits MyAbstract
Implements MyInterface
Sub MySub(myParameter As MyInterface)
myParameter.CommonString = "abc" ' compiler error - CommonString is not a member of MyInterface.
End Sub
'Other properties and methods go here!'
End Class
So, this is what I am doing, but when I use MyInterface, I cannot see the properties of my Abstract Class!

Unless I've completely misunderstood your question, I'm not sure why you are confused by this behavior. Not only is that how it should work, but that is also how it works in c#. For instance:
class Program
{
private abstract class MyAbstract
{
private string _myString;
public string CommonString
{
get { return _myString; }
set { _myString = value; }
}
}
private interface MyInterface
{
string UncommonString { get; set; }
}
private class MyInheritedClass : MyAbstract, MyInterface
{
private string _uncommonString;
public string UncommonString
{
get { return _uncommonString; }
set { _uncommonString = value; }
}
}
static void Main(string[] args)
{
MyInterface test = new MyInheritedClass();
string compile = test.UncommonString;
string doesntCompile = test.CommonString; // This line fails to compile
}
}
When you access an object through any interface or base class, you will only ever have access to the members that are exposed by that interface or base class. If you need to access a member of MyAbstract, you need to cast the object as either MyAbstract or MyInheritedClass. This is true in both languages.

Related

JAXB -EclipseLink MOXy Object type not getting marshalled correctly

public class ResposneMessage {
private int status;
private String code = "";
private String message = "";
private Object data;
}
The "DictType" is not marshaled:
{
"code": "",
"data": "com.testapp.model.DictType#7c9a897 com.testapp.model.DictType#43581423 com.testapp.model.DictType#217adb02 com.testapp.model.DictType#6ff992bb com.testapp.model.DictType#253e12c3 com.testapp.model.DictType#2644f34b com.testapp.model.DictType#51919e4a com.testapp.model.DictType#72deb289 com.testapp.model.DictType#27231e1b com.testapp.model.DictType#26fc6f1f com.testapp.model.DictType#7b42c644 com.testapp.model.DictType#7c8f695f com.testapp.model.DictType#43637313",
"message": "",
"status": 200
}
Default is not marshaling the Object type.
An Object can not be marshalled to any meaningful JSON representation because the library responsible for marshalling the object uses reflection to do so. It seems to only look at the top-most class in the hierarchy you defined and marshalls whatever it finds in there.
Since there is nothing for Object, you just get its .toString() representation.
FWIW: The same happens when you try to marshall an interface or (abstract) superclass. You only see the properties the interface/superclass itself defines, in your marshalled output - regardless of what the descendant classes declare/define.
Take this example:
public class SuperClass {
}
public class OtherClass extends SuperClass {
public String someProperty = "test";
}
public class MarshallMe {
public SuperClass classTest = new OtherClass();
}
This would marshall to just "classTest":{} because SuperClass doesn't have any properties of its own.

check that property setter was called

I have a class I am unit testing and all I want to do is to verify that the public setter gets called on the property. Any ideas on how to do this?
I don't want to check that a value was set to prove that it was called. I only want to ensure that the constructor is using the public setter . Note that this property data type is a primitive string
This is not the sort of scenario that mocking is designed for because you are trying to test an implementation detail. Now if this property was on a different class that the original class accessed via an interface, you would mock that interface and set an expectation with the IgnoreArguments syntax:
public interface IMyInterface
{
string MyString { get; set; }
}
public class MyClass
{
public MyClass(IMyInterface argument)
{
argument.MyString = "foo";
}
}
[TestClass]
public class Tests
{
[TestMethod]
public void Test()
{
var mock = MockRepository.GenerateMock<IMyInterface>();
mock.Expect(m => m.MyString = "anything").IgnoreArguments();
new MyClass(mock);
mock.VerifyAllExpectations();
}
}
There are 2 problems with what you are trying to do. The first is that you are trying to mock a concrete class, so you can only set expectations if the properties are virtual.
The second problem is the fact that the event that you want to test occurs in the constructor, and therefore occurs when you create the mock, and so occurs before you can set any expectations.
If the class is not sealed, and the property is virtual, you can test this without mocks by creating your own derived class to test with such as this:
public class RealClass
{
public virtual string RealString { get; set; }
public RealClass()
{
RealString = "blah";
}
}
[TestClass]
public class Tests
{
private class MockClass : RealClass
{
public bool WasStringSet;
public override string RealString
{
set { WasStringSet = true; }
}
}
[TestMethod]
public void Test()
{
MockClass mockClass = new MockClass();
Assert.IsTrue(mockClass.WasStringSet);
}
}

Accessible from only one class

I have a class and a method in it. The method's access modifier is now private but it can be changed. Now i just want the method to be seen only one another class.
the other class and my class are in same directory by the way.
The only way to allow a method in a class to be available to only one other class is to use a nested private class.
public class Enclosing
{
private class InnerClass
{
public void MyMethodThatCanOnlyBeUsedByEnclosingClass()
{}
}
}

kernel.Get to get singleton instance

I have ConverterTest class where I need to access ValidateTest class. I can't pass ValidateTest using constructor because ConverterTest is abstract class. If I introduce second constructor to bind ValidateTest I will get numerous problems in derived classes and many things will need to change. So I have tried to pass ValidateTest to ConverterTest using property injection(decorated with inject attribute) but that also do not work because ConverterTest is not created by Ninject and inject properties are ignored. So I decided to create Instance property directly in ValidateTest class and bind instance of itself. To get instance of ValidateTest in ConverterTest class I use kernel.Get<ValidateTest>().Instance. Everything works fine but is it good idea to use kernel.Get to access instance class? Is there any other solution?
public class ValidateTest
{
private readonly ISettingsRepository _settingsRepository;
[Inject]
public ValidateTest Instance { get; set; }
public ValidateTest(ISettingsRepository settingsRepository)
{
_settingsRepository = settingsRepository;
}
}
Binding
kernel.Bind<ISettingsRepository>().To<SettingsRepository>();
kernel.Bind<ValidateAbuse>().ToSelf().InSingletonScope();
Getting instance of ValidateTest using kernel.Get in abstract class where constructor binding is not possible and property binding is not working.
public abstract class ConverterTest
{
public void Execute()
{
NinjectHelper.kernel.Get<ValidateTest>().Instance
}
}
Why not have your subclasses of ConverterTest set the ValidateTest either via an exposed property in ConverterTest or constructor's of their own?
public abstract class ConverterTest
{
protected ValidateTest ValidateTest{get;set;}
public void Execute()
{
ValidateTest.ValidateStuff();
}
}
public class ConcreteConverter : ConverterTest
{
[Inject]
public ConcreteConverter(ValidateTest validateTest)
{
base.ValidateTest = validateTest;
}
}
Or, I think that you could make the property public public ValidateTest ValidateTest{get;set;} and it should work for property injection if you add the appropriate attribute.
public abstract class ConverterTest
{
[Inject]
public ValidateTest ValidateTest{get;set;}
public void Execute()
{
ValidateTest.ValidateStuff();
}
}

ReportViewer 2010 struggling with polymorphism

I have a rdlc report that takes as a ReportDataSource a List<BaseClass>.
BaseClass has two derived classes A and B.
In the report, I group based on a property of the base class. As long as the list only contains objects of A or B, all works fine. However if I mix instances from A and B, then the report creation fails with the following message:
The Group expression used in grouping '[Group Name]' references a dataset field which contains an Error: FieldValueException
The property returns for both classes a simple string literal, backed by a constant of the classes, there is nothing that could be wrong with this. I also checked all other used properties, but there is nothing wrong with them.
Has anybody else seen this behaviour or has someone an explantion for this behaviour? It seems to me that report viewer don't likes polymorphism! Could that be?
Example
public abstract class BaseClass{
public abstract string GroupKey{get;}
}
public class A : BaseClass{
public override string GroupKey{
get{
return ...
}
}
}
public class B : BaseClass{
public override string GroupKey{
get{
return ...
}
}
}
It turned out that this is another limitation of Report Viewer. As a solution I have created a class C that derives also from BaseClass and wrapps an instance of BaseClass.
Before providing my List<BaseClass> as a DataSource for Report Viewer, I wrap all contained instances of A and B with an instance of C and give then the list of C to Report Viewer. So all instances are of the same type and Report Viewer is happy.
Here an example. I hope this helps someone in the same situation:
public abstract class BaseClass{
public string GroupKey{get;}
public virtual C GetWorkaroundWrapper(){
return new C(this);
}
}
public class A : BaseClass{
public override string GroupKey{
get{
return ...
}
}
}
public class B : BaseClass{
public override string GroupKey{
get{
return ...
}
}
}
public class C : BaseClass{
BaseClass m_baseClass;
public C(BaseClass baseClass){
if(null == baseClass){
throw new ArgumentNullException("baseClass");
}
m_baseClass=baseClass;
}
public override string GroupKey{
get{
return m_baseCLass.GroupKey;
}
}
public override C GetWorkaroundWrapper(){
return this;
}
}
The GetWorkaroundWrapper-Methodis only for convenience. With this, the creation of the wrapper is simplified:
List<C> workaroundList=new List<C>();
foreach(BaseClass item in sourceList){
workaroundList.Add(item.GetWorkaroundWrapper());
}
dataSource.Value=workaroundList;
Please note that it is not important that the list is of C. It works also with a list of BaseClass, but its more clean to use a list of C.