PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to refer to a class object by combining text and a number - C++

 July 08, 2022     c++, class, object     No comments   

Issue

is there a way to directly use the menuItem variable (which is an integer obviously) to put in the tft.print functions, so I don't have to use "if - else statements" like in the code below?

My idea is that it works kind of like this (I know that this code doesnt work - just the idea):

tft.print(dmx(menuItem).channelName);

the "dmx" and "menuitem (1, 2,3...)" need to form a new word like in the code below. as example dmx1 or dmx2, which is a class object.

its basically adding a number to a text to form the object name, that has already been initialized. (dmx1, dmx2, dmx3 and so on...)

Here Is a snippet of my code:

void print_keyboard() { 
      tft.fillScreen(BLACK);
      tft.drawRoundRect(11, 40, 220, 35,5, WHITE);
      tft.setCursor(15,50);
      tft.setTextColor(WHITE);
    
      if (menuItem == 1){
        tft.print(dmx1.channelName);
      }
      else if (menuItem == 2){
        tft.print(dmx2.channelName);
      }
      else if (menuItem == 3){
        tft.print(dmx3.channelName);
      }
      else if (menuItem == 4){
        tft.print(dmx4.channelName);
      }
      else if (menuItem == 5){
        tft.print(dmx5.channelName);
      }
      else if (menuItem == 6){
        tft.print(dmx6.channelName);
      }
   }

I am really a beginner in programming, and its hard to search for the right questions, as I don't know all the right expressions.


Solution

Thank you for all the answers. I managed to bring it to work!

we can initialize an object array with parameterized constructors (This is done outside main loop, but can also be done inside):

Dmx dmx[] = {Dmx("1","DMX CHANNEL 1"), Dmx("2","DMX CHANNEL 2"),Dmx("3","DMX CHANNEL 3"), Dmx("4","DMX CHANNEL 4"), Dmx("5","DMX CHANNEL 5"),
            Dmx("6","DMX CHANNEL 6"), Dmx("7","DMX CHANNEL 7"), Dmx("8","DMX CHANNEL 8"), Dmx("9","DMX CHANNEL 9"), Dmx("10","DMX CHANNEL 10")};

By then calling:

tft.print(dmx[menuItem-1].channelName);

the menuItem variable is assigned to the index of dmx object. This way, when at menuItem 1 page, dmx[0] is called. Its not the cleanest solution. But it works. Here is the code example but with the new changes:

void print_keyboard() { 
  tft.fillScreen(BLACK);
  tft.drawRoundRect(11, 40, 220, 35,5, WHITE);
  tft.setCursor(15,50);
  tft.setTextColor(WHITE);

  tft.print(dmx[menuItem-1].channelName);
  }


Answered By - LastHerd
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing