Thread overview
Understanding question about lvalues
Feb 22, 2005
jicman
Feb 22, 2005
Dave
Feb 22, 2005
jicman
Feb 22, 2005
Derek Parnell
February 22, 2005
Let us admire this partial code,

class User
{
char [] userID = null;
char [] fname = null;
char [] lname = null;
char [] NameOut  = null;
FCount forms[] = null;
}
class FCount
{
char[] FormApp = "Language";
int    FormCnt = 0;
}
User ProcessUserForms(char[] fxml, User u)
{
int i = 0;
bit found = false;
char[][] t;
t = std.string.split(fxml,"<Application name=\"");
t = std.string.split(t[1],".Applicat");
t = std.string.split(t[0],"applications.");
if(u.forms == null) //.length < 1)
{
FCount fc = new FCount();
u.forms.length = 1;
u.forms[0] = fc;
u.forms[0].FormApp = t[1];
u.forms[0].FormCnt = 1;
found = true;
}
else
{
for(i=0; i < u.forms.length; i++)
{
if(std.string.cmp(u.forms[i].FormApp,t[1]) == 0)
{
found = true;
u.forms[i].FormCnt++;
break;
}
}
if(!found)
{
FCount fc = new FCount();
u.forms.length = u.forms.length + 1; // observe this line
u.forms[length - 1] = fc;
u.forms[length - 1].FormApp = t[1];
u.forms[length - 1].FormCnt = 1;
}
}
return u;
}

This code compiles perfecty, but if I change this line

u.forms.length = u.forms.length + 1; // observe this line

almost at the end, to

u.forms.length += 1; // observe this line

the compiler fails with

16:03:50.47>dmd DTSP1.d ws2_32.lib
DTSP1.d(740): u.forms.length is not an lvalue

Also, the same happens to,

u.forms.length++;

I just want to understand why.

thanks.

josé




February 22, 2005
The array length is considered a property of an array and so is consistent with properties as described under 'Class and Struct Properties' in http://digitalmars.com/d/property.html

(See Note at section bottom).

- Dave

In article <cvg7i9$1cmt$1@digitaldaemon.com>, jicman says...
>
>
>Let us admire this partial code,
>
>class User
>{
>char [] userID = null;
>char [] fname = null;
>char [] lname = null;
>char [] NameOut  = null;
>FCount forms[] = null;
>}
>class FCount
>{
>char[] FormApp = "Language";
>int    FormCnt = 0;
>}
>User ProcessUserForms(char[] fxml, User u)
>{
>int i = 0;
>bit found = false;
>char[][] t;
>t = std.string.split(fxml,"<Application name=\"");
>t = std.string.split(t[1],".Applicat");
>t = std.string.split(t[0],"applications.");
>if(u.forms == null) //.length < 1)
>{
>FCount fc = new FCount();
>u.forms.length = 1;
>u.forms[0] = fc;
>u.forms[0].FormApp = t[1];
>u.forms[0].FormCnt = 1;
>found = true;
>}
>else
>{
>for(i=0; i < u.forms.length; i++)
>{
>if(std.string.cmp(u.forms[i].FormApp,t[1]) == 0)
>{
>found = true;
>u.forms[i].FormCnt++;
>break;
>}
>}
>if(!found)
>{
>FCount fc = new FCount();
>u.forms.length = u.forms.length + 1; // observe this line
>u.forms[length - 1] = fc;
>u.forms[length - 1].FormApp = t[1];
>u.forms[length - 1].FormCnt = 1;
>}
>}
>return u;
>}
>
>This code compiles perfecty, but if I change this line
>
>u.forms.length = u.forms.length + 1; // observe this line
>
>almost at the end, to
>
>u.forms.length += 1; // observe this line
>
>the compiler fails with
>
>16:03:50.47>dmd DTSP1.d ws2_32.lib
>DTSP1.d(740): u.forms.length is not an lvalue
>
>Also, the same happens to,
>
>u.forms.length++;
>
>I just want to understand why.
>
>thanks.
>
>josé
>
>
>
>


February 22, 2005
Dave says...
>
>The array length is considered a property of an array and so is consistent with properties as described under 'Class and Struct Properties' in http://digitalmars.com/d/property.html
>
>(See Note at section bottom).
>
>- Dave

thanks Dave.


February 22, 2005
On Tue, 22 Feb 2005 21:18:01 +0000 (UTC), jicman wrote:

> Let us admire this partial code,
> 
> class User
> {
> char [] userID = null;
> char [] fname = null;
> char [] lname = null;
> char [] NameOut  = null;
> FCount forms[] = null;
> }
> class FCount
> {
> char[] FormApp = "Language";
> int    FormCnt = 0;
> }
> User ProcessUserForms(char[] fxml, User u)
> {
> int i = 0;
> bit found = false;
> char[][] t;
> t = std.string.split(fxml,"<Application name=\"");
> t = std.string.split(t[1],".Applicat");
> t = std.string.split(t[0],"applications.");
> if(u.forms == null) //.length < 1)
> {
> FCount fc = new FCount();
> u.forms.length = 1;
> u.forms[0] = fc;
> u.forms[0].FormApp = t[1];
> u.forms[0].FormCnt = 1;
> found = true;
> }
> else
> {
> for(i=0; i < u.forms.length; i++)
> {
> if(std.string.cmp(u.forms[i].FormApp,t[1]) == 0)
> {
> found = true;
> u.forms[i].FormCnt++;
> break;
> }
> }
> if(!found)
> {
> FCount fc = new FCount();
> u.forms.length = u.forms.length + 1; // observe this line
> u.forms[length - 1] = fc;
> u.forms[length - 1].FormApp = t[1];
> u.forms[length - 1].FormCnt = 1;
> }
> }
> return u;
> }
> 
> This code compiles perfecty, but if I change this line
> 
> u.forms.length = u.forms.length + 1; // observe this line
> 
> almost at the end, to
> 
> u.forms.length += 1; // observe this line
> 
> the compiler fails with
> 
> 16:03:50.47>dmd DTSP1.d ws2_32.lib
> DTSP1.d(740): u.forms.length is not an lvalue
> 
> Also, the same happens to,
> 
> u.forms.length++;
> 
> I just want to understand why.
> 
> thanks.
> 
> josé

The 'length' is a property (thus it is really a function call) so it is
like saying ...

   u.forms.length()++;

which is not supported because a function call is not an lvalue.

That's why 'u.forms.length += 1' also fails. But the approved method

  u.forms.length = u.form.length + 1;

is like saying ...

  u.forms.length( u.forms.length() + 1 );

which is okay to use.

-- 
Derek
Melbourne, Australia
23/02/2005 9:31:49 AM