Adding to a variable name

This is a discussion on Adding to a variable name within the C++ Programming forums, part of the General Programming Boards category; Hey, Is it possible to add a character to the end of a variable name? I have a set of ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    8

    Question Adding to a variable name

    Hey,

    Is it possible to add a character to the end of a variable name?

    I have a set of shapes on my form. The shapes are named Shape1 through to Shape11. The object is to get the shapes moving using a function that adjusts the Left attribute of each shape. Instead of writing out a line of code for each shape, I am using a loop to send the number of each shape to a function. The function then needs to put the number onto the end of the word Shape to form ShapeNumber (i.e. Shape1, or Shape2 etc). I've heard its possible but being relativly new to C++ I can't figure it out. Any help would be greatly appreciated.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    What you're looking for is arrays. If you create an array of 11 shapes:
    Shape myShapes[11];

    Then you can access each individual one from shape 0 to shape 10:
    ++(myShapes[0].Left);
    --(myShapes[10].Left);

    Otherwise, your best bet would be to look into std::map which I doubt you'd really get at the moment. It's the closest you'll ever get to creating 'variables' based on a string generated at run-time, but for your purposes I think an array would be a much better choice.

    There's a tutorial on arrays on the main cprogramming.com website, in case you don't have a textbook or something
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Thanks for the help, but how do I go about creating an array of shapes?

    shape myShapes[11]; brings up an undefined symbol 'shape' error.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Make sure that shape has been defined properly. Post your code.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Ok, just trying it on a brand new form:

    Code:
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
     shape myShapes[11];
    }
    //---------------------------------------------------------------------------

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Post Unit1.h as well. No where in this code do you define shape.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Ok,

    Code:
    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
    private:	// User declarations
    public:		// User declarations
            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Do you have any other files that you're not posting? Where do you define shape?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    I have no other files.. I'm confused as to where and how i define and then declare the shape/s.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The simplest way is to just define a shape class in your main file.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    ok, so I have defined a shape class in my unit1.h file and put shape myShapes[11]; in my unit1.cpp file.

    How do I relate the shape array to shapes on my form?

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The same way you relate a string of characters to text you send to the screen. Would you care to be more specific?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Well i have a set of shapes on my form called Shape1 through to Shape11. How do I relate those shapes to positions in the myShapes array?

    so myShapes[1] would refer to Shape1..

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Instead of having Shape1 through Shape11, use Shape[0] through Shape[10].
    Last edited by sean; 01-21-2005 at 05:05 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    I can't name the shapes with the [ and ] characters. Gives me an invalid component name error.

    I've tried:

    Code:
     shape myShapes[11];
     myShapes[0] = Shape1;
     myShapes[1] = Shape2;
    but it says "cannot convert 'TShape *' to 'shape'"
    Last edited by middric; 01-21-2005 at 05:07 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 12:25 PM
  5. Replies: 8
    Last Post: 04-22-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21