Hi Vijay,

Yes, it's important to declare a variable before it is used.

In the example you and I were using, there are 2 declarations: one outside the for-loop, the other in the for-loop initialization. We're talking beanshell here, where a value-to-variable assignment also counts as declaration (the one in the for-loop initialization).

Both declarations are actually outside/above the for-loop, so either one will 
work.

In standard Java, you either declare the variable outside the for-loop with "double thisMonthSales = 0", or you do it in the for-loop initialization like this:

    for (int i = 0, double thisMonthSales = 0; i < whatever; i++)

Just do standard Java in beanshell, it should work. Again, methods and classes are different in beanshell, so watch out for those.

Jonathon

vijay Si wrote:
Note that removing the declaration "double thisMonthSales = 0" AND
 >> also removing the for-loop initialization "thisMonthSales = 0" will
 >> give you that error --- "Illegal use of undefined object... blah
 >> blah".


What i did was,
i defined the variable outside the loop first, and tried using it inside the
loop (without declaring it again within for(...here...) ) using super. this
gave me error. I thought that super is supposed to make it lookup to the
master copy. but that was not the case. Now again after your posting i tried
removing super keyword and it worked..only thing that was important was to
declare the variable to be used within for(...).




On 9/27/07, Jonathon -- Improov <[EMAIL PROTECTED]> wrote:
Vijay,

This was what I wrote:

Note that removing the declaration "double thisMonthSales = 0" AND
also removing the for-loop initialization "thisMonthSales = 0" will
give you that error --- "Illegal use of undefined object... blah
blah".
You mentioned that it is not possible to avoid the above-mentioned error
unless you used the
"super." keyword. I'm not using that keyword, but am not getting the
error. I would like to know
how you got the error.

Jonathon

vijay Si wrote:
Hi Johnathon,
Got the concept......thanks............

"Technically speaking, the "type" is a bsh.Primitive,
actually."

i agree.....but i did not get the bug ...can u please explain.

Regards

On 9/27/07, Jonathon -- Improov <[EMAIL PROTECTED]> wrote:
Hi Vijay,

I'm not getting this in my bsh.

Here's what I did:

     double thisMonthSales = 0;
     for (i = 0, thisMonthSales = 0; i < 10; i++) {
         thisMonthSales += 10;
     }
     Debug.logInfo("thisMonthSales: " + thisMonthSales, "");

That worked, without "super.". Omitting the declaration "double
thisMonthSales = 0" will mean the
type is defaulted to integer, not double. (Technically speaking, the
"type" is a bsh.Primitive,
actually.)

I'm really interested in catching and documenting this bug (in
beanshell?). Please help? Thanks.

Note that removing the declaration "double thisMonthSales = 0" AND also
removing the for-loop
initialization "thisMonthSales = 0" will give you that error ---
"Illegal
use of undefined
object... blah blah".

"In BeanShell using an untyped or "loosely" typed variable is also
equivalent
to declaring a local variable. That is, if you use a variable that has
not
been defined elsewhere, it defaults to the local scope:"
In my test, it is functioning as per the documentation.

In the example you and I used, there are actually 2 declarations of the
variable "thisMonthSales".
One is above the for-loop, the other is inside the for-loop
initialization.

Omitting the first declaration will mean you are using an untyped or
"loosely" typed variable
"thisMonthSales" inside the for-loop initialization. Once out of the
loop,
the reference to
variable "thisMonthSales" works because it IS "defined elsewhere",
"elsewhere" being "inside the
for-loop".

As I said, if you do proper Java in beanshell, you should be safe.
Syntax
and structures are a bit
different when you go into classes and sub-classing and methods, etc.

Jonathon

vijay Si wrote:
Hi people here's the example:

double thisMonthSales = 0;
             for( i=0,super.thisMonthSales=0; i<=(elim.size()-1); i++)
              {
                  super.thisMonthSales += elim.get
(i).getDouble("grandTotal").doubleValue();
              }
I was earlier omitting super.thisMonthSales=0      within for loop.
This
resulted in an error "Illegal use of undefined object or 'void'
literal
"
Even removing "super" keyword from : super.thisMonthSales +=
elim.get(i).getDouble("grandTotal").doubleValue();
:  did not work. Atleast this was expected to work because i had
defined
the
variable outside the loop too.

A point from the bshmanual.
"In BeanShell using an untyped or "loosely"
typed variable is also equivalent to declaring a local variable. That
is, if
you use a variable that has not been
defined elsewhere, it defaults to the local scope:"

Thanks and Regards.
Vijay

On 9/26/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
vija

"So if one has to use the same variable elsewhere than one must
declare
it
outside the loop and refer to it elsewhere as super.variable to use
the
same
version of the varible."

Gads.  I would love to see an example of this so I can avoid the same
problem.

Skip

-----Original Message-----
From: vijay Si [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 26, 2007 12:49 AM
To: [email protected]
Subject: Re: TypeCasting GenericValue object.


Thanks....Skip.
  I got this point  after some expeimentation "  Typically, the
GenericValue
contains the name/value
pairs of some row in a table or view."

I changed my code accordingly... to use  elim.get
(i).getDouble("grandTotal").doubleValue();

thanks for the support.

One point that i would like to share is about beanshell.
Beanshell localizes the scope of the variable used within the loop to
that
loop.
So if one has to use the same variable elsewhere than one must
declare
it
outside the loop and refer to it elsewhere as super.variable to use
the
same
version of the varible.

This actually took up lot of my time and was also one the reasons for
"void"
!!



On 9/26/07, Skip <[EMAIL PROTECTED]> wrote:
vijay

I don't understand some of the code in your example.  But, if you
look
at
the source for GenericEntity (which GenericValue is a subclass of),
you
will
see that at its heart, there is a Map of name-value pairs, i.e. a
String
which is the field name mapped to (in this case) the value(s)
retrieved
by
the GenericDelegator.  Typically, the GenericValue contains the
name/value
pairs of some row in a table or view.

But, here is some code that will do what you want (assuming that
"grandTotal" is really a field in OrderHeader which I don't remember
for
sure.)  I have not included the required try/catch error trapping
for
clarity.

//Get a list of orders matching our conditions.  In place of the
ellipse,
add your conditions
List orders = delegator.findByAnd("OrderHeader", ...);
Iterator i = orders.iterator();

double total = 0.0;
//Go through the list and add up all the "grandTotal" values.
while (i.hasNext())
{
    GenericValue orderHeader = (GenericValue) i.next();
    String orderId = orderHeader.getString("orderId");
    Double totalThisOrder = orderHeader.getDouble("grandTotal");
    System.out.println("Order id is " + orderId + "\t\tAmount is " +
totalThisOrder);
    if(totalThisOrder != null)
         total += totalThisOrder.doubleValue();
}
System.out.println("                      \t\t Grand Total " +
total);

In the above example, "orders" is a Java List of GenericValue
objects
returned by the delegator.  We have to iterate through them and
extract
the
"grandTotal" values to add up.

This is such a common task that maybe I'll write a

double GenericValue.sumDouble(List <GenericValue>values, String
field)
and
long GenericValue.sumLong(List <GenericValue>values, String field)

Give it a try and see if this is not what you want.

Skip

-----Original Message-----
From: vijay Si [mailto:[EMAIL PROTECTED]
Sent: Tuesday, September 25, 2007 2:32 AM
To: [email protected]
Subject: Re: TypeCasting GenericValue object.


Hello Skip,

My piece of code is :

itelim = elim.iterator();               //where elim =
findByCond(........);
  elimList = new ArrayList();

          while(itelim.hasNext())
          {
              elimList = itelim.next();
              elimList.toString();

              String a1 =  elimList.getString("grandTotal");
              a =  Double.parseDouble(a1);

          }
Now this does give me some result at the o/p : which is last entry
in
the
grandTotal column(a double value) say for eg: 350.30

I also used the piece of code that you provided.....it works for
sure
but
gives "void" at the o/p.......

Also wanted to ask as to how can i iterate over the grandTotal
entries.
for
eg: if i have to get grandTotal for all products and sum it up.

Thanks

On 9/25/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
You can't type cast the GenericValue.  You possibly can type cast
one
of
the
objects it holds depending on the type.  Each GenericValue holds
different
types if information depending on what it represents.  For example,

GenericValue existingOrderHeader = delegator.findByPrimaryKey
("OrderHeader",
UtilMisc.toMap("orderId", orderId));
String customerId = externalOrderHeader.getString
("customerPartyId");
if OrderHeader had a Double value in it, say "grandTotal", you
could
double grandTotal =
externalOrderHeader.getDouble("grandTotal").doubleValue();


or

double grandTotal =
((Double)externalOrderHeader.get("grandTotal")).doubleValue();

Both of these will throw a ClassCastException if "grandTotal" is
not
a
double.

Everything in a GenericValue object is an object itself, i.e double
is
wrapped in Double, etc.

Hope that helps

Skip

-----Original Message-----
From: vijay Si [mailto:[EMAIL PROTECTED]
Sent: Monday, September 24, 2007 11:12 PM
To: [email protected]
Subject: TypeCasting GenericValue object.


Hi,
I have been trying to type cast a generic value object to double
and
string,
but i get an error

"java.lang.ClassCastException: Cannot cast
org.ofbiz.entity.GenericValueto
java.lang.xx"

Is there any particular way in ofbiz to work around this?

Regards



------------------------------------------------------------------------
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.13.32/1032 - Release Date:
9/26/2007 8:20 PM



------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.13.32/1032 - Release Date:
9/26/2007 8:20 PM




------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition. Version: 7.5.488 / Virus Database: 269.13.32/1032 - Release Date: 9/26/2007 8:20 PM

Reply via email to