function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
inbaljS2inbaljS2 

Help with apex custom controller and apex class

Hello all,

I have created a visualforce page with a custom controller calls "Xcontroller". 

on my controller I have defined a member of my class "Y" like this:

 

"private static Y y = new Y(a,b);"

 

This line is executed on page load.

 

When I click on my page button "finish" I invoke the function I created in my controller called "finish".

 

In this function I call functions from my class y, for example:

 

public class y {

 

private static string z;

private static string A;

private static string B;

 

public y(string a, string b)

{

     A=a;

     B=b;

}

 

public string getZ()

{

return z;

}

 

public static void setZ(string zz)

{

    z=zz;

}

 

}

 

 

on my controller :

public with sharing class Xcontroller{

 

private static Y y = new Y(a,b);

 

public Xcontroller()

{

}

 

public pagereference finish()

{

string w= y.getZ();

}

 

 

the problem is that when I click on "finish" the value in 'w' is null although on the page load before it got a value!

 

Can anyone help me understand what I did wrong?

Thanks a lot,

Inbal

 

 

rscottrscott

Well, I don't see where you actually call setZ. If you don't, z will always be null.

 

Other than that, I'm not sure why you are declaring y in your controller as static. 

 

From the docs:

Static variables aren't transmitted as part of the view state for a Visualforce page.

 

This would cause it not to retain its value. Try removing static from that variable declaration.

rungerrunger

Another thing to bear in mind, though I don't think it's directly related to your question, there is a subtle bug in your code.  Apex is not case sensitive.  So your constructor doesn't actually assign anything to the instance variables A and B.  You need to do:

 

 

public y(string a, string b)
{
  this.a=a;
  this.b=b;
}

 

Rich

 

mohimohi

You hve made right concept,outside scope static variable does not retain it's value.

rscottrscott

Actually, using 'this' won't work in this case due to the static modifier on the a and b variables. The original code won't compile either due to duplicate variable declaration. To fix the original, modify either the constructor variable names or the class-level variable names. If you don't need your variables static, remove the static keyword and use 'this'. 

 

I assumed that the code posted was just pseudo-code since it won't even compile as written. The question for the OP is did you really want all those static variables and methods.

rungerrunger

Yeah, I totally missed the fact that they're static.

inbaljS2inbaljS2

Thank you all, 

 

first you are right it is a pseudo code, 

I am calling setZ, at the controller constructor.

also in my code the variables names are not the same, so the problem is not in the names. 

sorry i got you confused.

 

I made all these variables as static since I want to keep their value- any other suggestions on how I will still get the value of w?

 

Also, let me fix a bit the controller constructor:

 

public with sharing class Xcontroller{

 

private static Y y = new Y(a,b);

 

public Xcontroller()

{

if (y==null)

       y= new Y(a,b)

if(w==null)

       setZ();

}

 

public pagereference finish()

{

string w= y.getZ();

}

inbaljS2inbaljS2

well small change:

 

public with sharing class Xcontroller{

 

private static Y y = new Y(a,b);

 

public Xcontroller()

{

if (y==null)

       y= new Y(a,b)

if(w==null)

       setZX();

}

 

public static void setZX()

{

     y.setZ();

}

public pagereference finish()

{

string w= y.getZ();

}

rscottrscott

As suggested in my first post, the y variable in your controller class will retain its value on the same page if it is not marked as static. Have you tried that?

 

If that doesn't work, can you post your actual code? You are checking variable w in your constructor, but it is not declared anywhere except locally in the finish() method. I don't see how this version works either. It's hard to tell you what's wrong when we only have half of your code.

inbaljS2inbaljS2

Hi

Removing the static did help...

Thanks everyone,

Inbal