Converting string into non-string Form.Object types for passing to methods or updating non-string object.Property with strings
-
Thank You Jerry, ButtonBase BUTTONx = this.Controls.["button1"]; I tried that before and ButtonBase doesn't associate with the string name. I get error CS1001 at the '['. This doesn't work either ButtonBase.Button BUTTONx = this.Controls.["button1"];
I posted about "is" and "as" already in this thread; which would have answered your question. The other aspect related to "is" and "as" is "boxing"; but that is generally used for "value types".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Hello & Salutations to Everyone, using: Win Forms .NET 4.7.2 I have a set of Form buttons named button1, button2, button3,……… I have an array of strings BUTTON_LIST_str[] =” button1”, “button2”, “button3”,… I have a function: private void APPLY_BUTTON_CFG(Button b) { …………… } What I need to do is loop through BUTTON_LIST_str[] and pass each button object to APPLY_BUTTON_CFG(Button b). Basically for every string I need to use for pointing to a different type form object or its property of object, I get the error "String cannot be converted to the object required type/format (System.Form......) I have searched every convoluted order of words for hours and cannot find a solution that matched my scenario of converting strings to all form object types (or specific ones either). With so many unique object specific variables how does one research the solution to convert a string to each type for each case? Is there a ConvertToType universal function that solves this issue. I also ran into same problem with: FlatStyle P_STYLE = “Flat”; Button1.FlatStyle = P_STYLE or FlatStyle.P_STYLE ; // error cannot convert string to…………..FlatStyle object type I understand that a string name cannot point to a form object OOP when passing to a function. I’m not sure I can ask my question competently…………. With so many unique object specific variables how does one research the string conversion solution for each case? Simply put, how would I research and find the solution for these types of problems: Button1.FlatStyle = How to convert string to FlatStyle object reference, or Font, TextAlign when an error occurs? I did find this translator for colors but not FlatStyle though or for any other applicable parameters. P_COLOR = System.Drawing.ColorTranslator.FromHtml("White"); //"White" will be replaced with a string array element CFG_PARAM_PARSED[2]); Button1.ForeColor = P_COLOR; This is the real kicker: Button name as string (String_Array[x]) needs conversion (typecast?) to object name reference var ButtonObject = String_Array[x]; or Button ButtonObject = String_Array[x]; APPLY_BUTTON_CFG(ButtonObject); private void APPLY_BUTTON_CFG(Button b) { …………… } Thank You So Much for your time and assistance…………………….