I got a class with 2 functions. One of them needs to call the other one both in the same class. How do you do that ?
class a
{
func1(): string {
return "test";
}
func2() {
var aTest = this.func1(); // Get an undefined error ??
}
}
What you have is completely valid (although func2 does nothing with aTest)
I took what you have and tweaked a little to enable seeing output and renaming class a to class A. Take my code below and play with it here: http://www.typescriptlang.org/Playground
class A
{
func1(): string {
return "test";
}
func2() {
var aTest = this.func1();
alert(aTest);
}
}
var a = new A();
a.func2();
Related
I have a method that can take ResponseEntity as parameter.
private ResponseEntity<OfferRest> mappedOfferByImagesEnabled(
ResponseEntity<OfferRest> offerResponse) {
for (OfferDetailImageRest image :
offerResponse.getBody().getOfferDetail().getImages()) {
if (image.getDisabled()) {
return offerResponse;
}
}
return null;
}
I have the same method with another parameters: OfferEnity and I don't have need to call getBody() like the other one.
private OfferEntity mappedOfferByImagesEnabled(OfferEntity offerEntity) {
for (OfferDetailImageEntity image :
offerEntity.getOfferDetail().getImages()) {
if (image.getDisabled()) {
return offerEntity;
}
}
return null;
}
My idea is to have a method with one (generic) parameter. Basing on the settings instance I will run the convenient code.
My question, How can I do it?
You can create a method with OfferDetail parameter
public boolean isImageDisabled(OfferDetail offerdetail) {
return offerdetail.getImages().stream().anyMatch(Image::getDisabled));
}
And the use it
isImageDisabled(offerEntity.getOfferDetail());
isImageDisabled(offerResponse.getBody().getOfferDetail());
If I am declaring 2 enums inside my class this way:
public class EnumerationExample {
public enum Season {WINTER,SPRING,SUMMER,FALL}
public enum Month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC}
public List<Month> listMonths;
public Month convert (String val) {
for (Month mtObj : Month.values()) {
if (mtObj.name() == val) {
system.debug('The value passed is ' +mtObj);
}
}
return null;
}
public List<Month> seasonMonths(Season s) {
Season seasonObj = Season.SPRING;
listMonths = new List<Month>();
if(s==season.WINTER) {
listMonths.add(Month.DEC);
listMonths.add(Month.JAN);
listMonths.add(Month.FEB);
}
else if(s==season.SPRING) {
listMonths.add(Month.MAR);
listMonths.add(Month.APR);
listMonths.add(Month.MAY);
}
else if(s==season.SUMMER) {
listMonths.add(Month.JUN);
listMonths.add(Month.JUL);
listMonths.add(Month.AUG);
}
else if(s==season.FALL) {
listMonths.add(Month.SEP);
listMonths.add(Month.OCT);
listMonths.add(Month.NOV);
}
return listMonths;
}
}
how do i write test code for this ??
i tried doing this way but it says season variable does not exist at line EnumerationExampleObj.seasonMonths(Season.WINTER);...
#isTest
public class TestEnumerationExample {
public static testMethod void myUnitTest() {
EnumerationExample EnumerationExampleObj = new EnumerationExample();
EnumerationExampleObj.convert('wintery');
EnumerationExampleObj.seasonMonths(Season.WINTER);
system.assertEquals(EnumerationExampleObj.listMonths.get(0) , Month.DEC );
}}
is there any problem with the access modifier or any specific annotations.?
Your problem is not related to testing at all, but to C# basics like scope and syntax (your sample code is full of syntax errors).
To answer your specific question: if you define a public enum inside a class, you have to prefix it with the class name when used outside that class. Example:
var enumerationExampleObj = new EnumerationExample();
enumerationExampleObj.seasonMonths(EnumerationExample.Season.WINTER);
I have another question for the IL sub-community.
I am finding my way through ILArrays and similar, but I would like another clarification.
In the example reported on the ILNumerics website, we have (simplifying what is not needed)
public class Computing_Module1 : ILMath
{
public static ILRetArray<double> SimpleMethod()
{ using (ILScope.Enter() )
{
ILArray<double> C = ILSpecialData.sinc(40, 50);
return C[0, full];
}
}
}
If you run this code everything works perfectly because of the final C[0,full], from what I can understand. On converse, I have the following wrapping code around ILMath.csvread:
public static ILRetArray<double> ReadCsvIL(string aFileName)
{
using (ILScope.Enter())
{
string stringData;
try
{ stringData = getDataFromText(aFileName);
ILArray<double> myData = ILMath.csvread<double>(stringData);
return myData ;
}
catch (FileNotFoundException anExc)
{
Console.WriteLine(anExc.Message);
return null;
}
}
}
That will not work: two workarounds: either use the same trick provided in the example substituting
ILArray<double> myData = ILMath.csvread<double>(stringData);
return myData ;
with
ILArray<double> myData = ILMath.csvread<double>(stringData);
return myData[ILMath.full, ILMath.full] ;
or adopting the .a property of the ILArrays, so it should be written as:
ILArray<double> myData = ILMath.csvread<double>(stringData);
myData.a = myData;
return myData ;
Question: do we always have to use the .a property to assign an ILRetArray? Am I right in understanding that if return anArray[full, full] then some deep copy is performed behind the scenes, basically assigning to the .a property? Are there other ways to do this and what is the most efficient way?
For completeness the function to retrieve the text from the file is just a wrapping function around streamreader.ReadToEnd(), here it is the code:
public static string getDataFromText(string aFileName)
{
if (!File.Exists(aFileName))
throw new FileNotFoundException("The file does not exist.");
string stringData = String.Empty;
using (StreamReader streamReader = new StreamReader(aFileName))
{
stringData = streamReader.ReadToEnd();
}
return stringData;
}
Hope this makes sense; thanks you very much in advance.
I have a class with a property that is another class. I also have an Expression that maps each from other classes. How do I combine these two expressions into one expression without compiling the second Expression?
public class ClassA
{
public int SomeProperty { get; set; }
}
public class MappedClassA
{
public int MappedProperty { get; set; }
}
public class ClassB
{
public ClassA ClassAProperty { get; set; }
//snip...
}
public class MappedClassB
{
public MappedClassA MappedClassAProperty {get; set; }
}
public Expression<Func<ClassA, MappedClassA>> MapAExpression()
{
return a => new MappedClassA()
{
MappedProperty = a.SomeProperty
};
}
public Expression<Func<ClassB, MappedClassB>> MapBExpression()
{
return b => new MappedClassB()
{
//this should be done with the above expression
MappedClassAProperty = new MappedClassA()
{
MappedProperty = b.ClassAProperty.SomeProperty
}
};
}
This is possible without compiling the expression, but unfortunately not by using the simple lambda syntax to construct the expression.
You will have to create the expression tree "by hand", using expression methods like Expression.New() and Expression.Lambda().
This can get unreadable very quickly.
I tried assembling such an expression from memory below, but I lack practice; this is untested and there are possibly some bugs left.
public Expression<Func<ClassB, MappedClassB>> MapBExpression()
{
// generate a parameter of type ClassB.
// This is the parameter "b" our final lambda expression will accept.
var classBparam = Expression.Parameter(typeof(ClassB));
// access b.ClassAProperty; this is the property
// that we want to pass to the expression returned by MapAExpression()
var memberAccess = Expression.MakeMemberAccess(
classBparam,
typeof(ClassB).GetProperty("ClassAProperty"));
// invoke the lambda returned by MapAExpression()
//with the parameter b.ClassAProperty
var invocation = Expression.Invoke( MapAExpression(), memberAccess );
// create a new MappedClassB(), this is the object that will be returned
// by the expression we are currently creating
var ctor = Expression.New(typeof(MappedClassB));
// We want to assign something to the MappedClassB.MappedClassAProperty
var mappedClassAProperty =
typeof(MappedClassB).GetProperty("MappedClassAProperty");
// specifically, we want to assign the result of our MapAExpression(),
// when invoked with the parameter b.ClassAProperty
var mappedClassAAssignment =
Expression.Bind(mappedClassAProperty, invocation);
// Here we initialize the MappedClassAProperty,
// after creating the new MappedClassB.
// We initialize it with the assignment we just created
var memberInit = Expression.MemberInit(ctor, mappedClassAAssignment);
// finally, we construct the lambda
//that does all of the above, given a parameter of type ClassB
return Expression.Lambda<Func<ClassB, MappedClassB>>(memberInit, classBparam);
// this expression should now be equivalent to:
// return b => new MappedClassB()
// {
// MappedClassAProperty = new MappedClassA()
// {
// MappedProperty = b.ClassAProperty.SomeProperty
// }
// };
}
Is there any "automatic way" to casts an "Object" to a personalized Model Data Type in ActionScript 3?
Example:
package Example{
public class ExampleModel{
private _id:int;
private _randomAttribute:String;
public function ExampleModel(){
_id = 0;
_randomAttribute = "";
}
public function get id():int{
return _id;
}
public function set id(value:int):void{
_id = value;
}
public function get randomAttribute():String{
return _randomAttribute;
}
public function set randomAttribute(value:String):void{
_randomAttribute = value;
}
}
}
Then, in some part of my code, lets assume that I have something like this:
var _obj:Object = new Object();
_obj.id = 1;
obj.randomAttribute = "Hello World";
What I want would be something like:
var _exampleModel:ExampleModel = obj as ExampleModel;
But when I do this, the result on _exampleModel is null.
Any ideas?
Thanks.
EDIT:
According to Manish's answer, all I changed was the type of p var, which allows me to go through every kind of attribute:
public function fromObject(obj:Object):void{
//p:* includes every type of attributes.
for (var p:* in obj)
if (this.hasOwnProperty(p))
// Set private var directly.
this["_" + p] = obj[p];
}
Thanks Manish and rcdmk.
Manish's answer was enough for me and according to rcdmk's comment, the p:String isn't about the Type of data that the loop will go through, it is actually the name of the property, which makes sense because every name is a String.
The only way to automatically "cast" it is the following:
var model:Model = new Model();
var obj:Object = { id: "98123", name: "John Doe" };
for (var p:String in obj) {
if (model.hasOwnProperty(p))
model[p] = obj[p];
}
(Note: Model.id and Model.name are both type String in my example.)
A more proper way to do it, of course, is to pass the plain object to the Model object and let the Model object absorb it.
var model:Model = new Model(obj);
Or:
var model:Model = new Model();
model.fromObject(obj);
Where in the Model code you have:
public function Model(obj:Object = null)
{
if (obj != null)
fromObject(obj);
}
public function fromObject(obj:Object):void
{
for (var p:String in obj)
if (this.hasOwnProperty(p))
// Set private var directly.
this["_" + p] = obj[p];
}
This code can be in your abstract base Model class, and all your specific Model subclasses (e.g. ProductModel, CustomerModel, etc.) can use it automatically.
e.g.
public class ProductModel extends Model
{
public function ProductModel(obj:Object = null)
{
super(obj);
}
}