Reference of returned object? - salesforce

If I have a class as follows:
class MyClass
{
List<String> list{get;set;}
...
}
And then execute:
MyClass instance = new MyClass();
instance.add('string') ;
Will that new entry in the list be added to the member variable instance?

No. What you trying to do should be more like
class MyClass{
public List<String> list{get;set;}
}
and then execute
MyClass instance = new MyClass();
instance.list.add('string');

Related

How do i write test class for below apex code

Please help me in writing test class for below apex code,i wrote a test class which shows only 66% coverage,i am looking for 100%
public class PickListHandler {
#AuraEnabled
public static List<String> getLevel1(string strName) {
List<String> tempLst = new List<String>
for(AggregateResult ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c group by Level_1__c])
{
tempLst.add('Level 1 data is'+ar.get('Level_1__c'));
return tempLst;
}
}
}
Here is test class
#isTest
public class testGetLevel1 {
static testMethod void testGetLevel1() {
List<String> s = PickListHandler.getLevel1('test');
//System.assert(....);
}
}
You need need to create test data for the object Case_Type_Data__c. If you don't create data, the logic inside for loop will not execute.

Storing objects in array (Haxe)

How would I go about sorting an new instance of an object into an array in Haxe?
For example I have a class called weapon and in the player class I gave an array inventory.
So how would I store this?
private void gun:Weapon
gun = new Weapon; //into the array
I think you are looking for this:
private var inventory:Array<Weapon>;
This is an array of type Weapon.
To add stuff to it use push(), as seen in this example:
class Test {
static function main() new Test();
// create new array
private var inventory:Array<Weapon> = [];
public function new() {
var weapon1 = new Weapon("minigun");
inventory.push(weapon1);
var weapon2 = new Weapon("rocket");
inventory.push(weapon2);
trace('inventory has ${inventory.length} weapons!');
trace('inventory:', inventory);
}
}
class Weapon {
public var name:String;
public function new(name:String) {
this.name = name;
}
}
Demo: http://try.haxe.org/#815bD
Found the answer must be writen like this
private var inventory:Array;
With Weapon being the class name.

How to use properties class object in child class

I have created a class 'a' with in package name 'pack1'
#Beforetest
public class a {
public properties prop;
public propLoad(){
Webdriver driver= new firefoxdriver();
prop = new properties();
prop.load(driver);
}
also i have created a new package 'pack2' and new class 'b' and i want to use the prop object in b class
i have written the code as
#Test
public class b extends a{
prop.getproperties(keyname);
}
(Keys and values are defined in .properties file)
But when i have ran the code system gives me null pointer exception
Pls help me in this issue
Define prop = new properties(); at class level
#Beforetest
public class a {
public properties prop=new properties();
public propLoad(){
Webdriver driver= new firefoxdriver();
prop.load(driver);
}
I am not sure for what reason you need to pass driver object here? prop.load(driver); Anyways, instantiating prop class object at class level should do a trick. Let me know.

Add items from ArrayList to Array and show in JList

I am trying to add all the items from ArrayList<String> to String[] array which is in another class but i am fail to do that. I have tried many of the ways but to not avail. The String[] array in another class does not display any item. Hence, I was fail to add the array to the JList. I have no idea to deal with it. Here is my code:
public class bookbook extends JFrame{
java.util.List<String> timeList = new ArrayList<String>();
public bookbook(){
selectTime.timeArr = new String[ timeList.size() ];
timeList.toArray(selectTime.timeArr);
}
}
public class SelectTime extends JFrame{
String[] timeArr = {};
private JList jlstTime = new JList(timeArr);
public SelectTime(){
jpTicket.add(jlstTime);
}
}
I really need your help. Thanks!
You are modifying the JList array directly, which, as the documentation states, will result in an undefined behaviour. Try this code: Added the function updateList to the class SelectTime to do it using the appropiate methods
public class bookbook extends JFrame{
java.util.List<String> timeList = new ArrayList<String>();
public bookbook(){
String [] timeArr = new String[ timeList.size() ];
timeList.toArray(selectTime.timeArr);
selectTime.updateList(timeArr);
}
}
-
public class SelectTime extends JFrame{
String[] timeArr = {};
private JList jlstTime = new JList(timeArr);
public SelectTime(){
jpTicket.add(jlstTime);
}
public void updateList(String [] array)
{
jlstTime.setListData(array);
jlstTime.invalidate();
}
}

how to call methods within a class which is in a controller extension class?

I am trying to write a test class for a controller extension. This controller extension has another class in it . And this class has a few methods.
public class Extension_Account
{
public Extension_Account(ApexPages.StandardController controller)
{
public class Class1
{
public Class1()
{
/ * code here*/
}
public String getMethod()
{
/* code here */
}
}
}
}
How can i access the method getMethod in my test class?
In my test class i am able to access the contructor for Class1 but not sure how to get to the other method
public with sharing class TestExtension_Account
{
static testMethod void TestPrint()
{
Account a = new Account();
//a.Name='Test Account';
a.FirstName='TestFirst Name';
a.LastName='Test Last Name';
a.BillingStreet='Test billing Street';
a.BillingCity='Test Billing City';
a.BillingState='Test Billing State';
a.BillingCountry='Test Billing country';
a.BillingPostalCode='Test PostCode';
insert a;
PageReference pageRef = Page.printaddressaccount;
pageRef .getParameters().put('id',a.id);
Test.setCurrentPageReference(pageRef);
ApexPages.StandardController controller = new ApexPages.StandardController(a);
Extension_Account ae = new Extension_Account(controller);
ae.getClass1();
ae.getMethod()// gives a compiler error Method does not exist or incorrect signature
}
}
If your extension class has a getClass1() method that returns an instance of Class1, then you can access the methods from that instance, e.g. ae.getClass1().getMethod();

Resources