Re: c2 classes

2018-04-06 Thread Uknown via Digitalmars-d-learn

On Friday, 6 April 2018 at 14:43:25 UTC, Ali wrote:

On Friday, 6 April 2018 at 14:31:49 UTC, Alex wrote:

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

[...]

A question from me, since I am also still learning D
what is the difference between those following two declarations

UUsers[int] uid; UUsers[] uid;


T[U] declares an Associative Array but T[] declares a Dynamic 
Array. Some examples will help:


---
void main()
{
char[int] s1;
char[] s2;
s1[1] = 'c'; //allowed, it is an Associative array. key 1 
stores value 'c'

s2[0] = 1; //error: out of bounds of array s2
}
---

You can check out the spec[0] and the tour[1]

[0]: https://dlang.org/spec/hash-map.html
[1]: https://tour.dlang.org/tour/en/basics/associative-arrays


Re: c2 classes

2018-04-06 Thread Ali via Digitalmars-d-learn

On Friday, 6 April 2018 at 14:31:49 UTC, Alex wrote:

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

its possible to make this work ??

import std.stdio;


class UUsers
{
public:
int age;
}


class users
{
public:
int[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0].age = 23;
writeln(newuser.uid[0].age);

}


It depends on what you want to achieve...
This is runnable:
```
import std.stdio;


class UUsers
{
this(int val)
{
age = val;
}
public:
int age;
}


class users
{
public:
UUsers[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0] = new UUsers(23);
writeln(newuser.uid[0].age);

}
```


A question from me, since I am also still learning D
what is the difference between those following two declarations

UUsers[int] uid; UUsers[] uid;




Re: c2 classes

2018-04-06 Thread Ali via Digitalmars-d-learn

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

its possible to make this work ??

import std.stdio;


class UUsers
{
public:
int age;
}


class users
{
public:
int[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0].age = 23;
writeln(newuser.uid[0].age);

}


This will work
import std.stdio;

class UUsers
{
public:
int age;

}


class users
{
public:
UUsers[] uid;

}


void main() {

users userList = new users();

userList.uid ~=  new UUsers();

userList.uid[0].age = 24 ;

writeln(userList.uid[0].age);
}

Let me try to explain why
Basically, what you wanted to do is have two classes
one will hold user's information, and another holding a user list

For the second class to hold user list
this line

int[int] uid;


became this line

UUsers[] uid;


In you code, there was no relation between the two classes
Then in your main

You instantiate your users list object

users userList = new users();


Then you instantiate a user object and append it to the list (~= 
is the append operator)



userList.uid ~=  new UUsers();


Finally you assign a value to your user object age and print it

userList.uid[0].age = 24 ;
writeln(userList.uid[0].age);


Re: c2 classes

2018-04-06 Thread Alex via Digitalmars-d-learn

On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:

its possible to make this work ??

import std.stdio;


class UUsers
{
public:
int age;
}


class users
{
public:
int[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0].age = 23;
writeln(newuser.uid[0].age);

}


It depends on what you want to achieve...
This is runnable:
```
import std.stdio;


class UUsers
{
this(int val)
{
age = val;
}
public:
int age;
}


class users
{
public:
UUsers[int] uid;

}


void main() {

users newuser = new users();
newuser.uid[0] = new UUsers(23);
writeln(newuser.uid[0].age);

}
```