CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2013
    Posts
    7

    Accessing objects using their name reference through string variables

    I have an check box name into a string variable as follows : String Myvar = "checkbox01".

    I want to access the check box properties doing something like Myvar.checked = true; as I was referring direct to the string content or maybe Mayvar.text = "New-text"...

    Is there a way to make C# understand this?

    Thank you very much.


    Gus

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Accessing objects using their name reference through string variables

    Um, this usually leads to some hard to maintain code. What's the bigger picture on what you are trying to accomplish?

  3. #3
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    Actualy I am trying exactely the opposite, to get a ease to mantain code. I used the name of the components in my form to know the namo of the refered other components, as so, just for an example, I have a check box named "check_01_hora_1011" and also I have a radio button named "prim_dia_radio", when both are selected, a textbox named "banda_prim_1011" must be visible and enable to be fullfiled, and, if you noticed, I can have the name of the right textbox just combinig peaces of the other two controls, I take the "1011" substring from the check box name and the "prim" substring from the radio name and then I can get the "banda_PRIM_1011" textbox name... The trik I have to do than is how to make "banda_prim_1011".text ou other property to run properly. Do you got it?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Accessing objects using their name reference through string variables

    Oh I got it. I just happen to think it's a bad idea.

    If these are static controls (i.e. fixed controls on a form), you are going to have some dev in the future not understand the naming convention and screw things up.

    If the controls aren't static and you are building them dynamically, then there isn't any need to use a string to access the control (because you have the reference to the control already).

  5. #5
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    I am the only developer envolved and will always be, because I am writing this code for my personal use only in a specific personal project. Anyway, Do you know how to solve this?

  6. #6
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    Ah, they are all fixed controls....

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Accessing objects using their name reference through string variables

    I wonder, do you have a background in web programming?

  8. #8
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    not much, but I am a hard researcher and a hard student... If you have any ideas that may lead to a solution "I am all ears".... shoot it!

  9. #9
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    not much, but I am a hard researcher and a hard student... If you have any ideas that may lead to a solution "I am all ears".... shoot it!

  10. #10
    Join Date
    May 2013
    Posts
    7

    Re: Accessing objects using their name reference through string variables

    Tks for your time and for chatting.... I got it and already done!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured