Accessing class/attributes from string

classes use records with variables by name,
and tables have column names BUT can also be called by number.

for me the name thing of classes was actually a disadvantage,
compared to the handling of [ ][ ]

i must admit that i did not get what you wanted to do with having a string for a named class record!

while @GoToLoop is absolutely right about the “save/load file” handling with using “table”
i play my idea about a better “array of class” handling,
take a look at:

// multi dim array are possible, but only of one type like: int[][]
// classes allow (named) records mix like: int float boolean objects 
// add has the advantage that it can have internal functions
// array of class, would be a single dim array of above records,
// combines that all, BUT
// it does not allow a easy ( numeric ) looping like a  [][]

// p.s. tables allow using column names ( word ) OR numbers 

// here my "get from class" idea:


class Book {
  String title;
  int catalogue;
  String author;

  //  Book() {}
  String get(int getit) {                        // readout like matrix
    if ( getit == 0 ) return title;
    if ( getit == 1 ) return str(catalogue);     // not INT
    if ( getit == 2 ) return author;
    return("NaN");
  }
}

int many = 3;
Book[] books = new Book[many];

void setup() {
  for ( int i =0; i< books.length; i++ )   books[i] = new Book();
  books[0].title = "T1";
  books[0].catalogue = 100;
  books[0].author = "alpha";
  books[1].title = "T2";
  books[1].catalogue = 200;
  books[1].author = "beta";
  books[2].title = "T3";
  books[2].catalogue = 300;
  books[2].author = "gamma";
  
  
  for ( int i = 0; i < books.length; i++ )
    for ( int l = 0; l < 3; l++ )
      println("i: "+i+" l: "+l+" : "+books[i].get(l));
}


1 Like