nakhatib Posted January 3, 2004 Posted January 3, 2004 for example: DIM A AS STRING="textbox1" dim c as control= ' here i want to convert the string in the A variable to a datatype can i do that. This is a general question. The datatype i want to convert to, is not a standard datatype, it is a class data type. Thank you Quote
AlexCode Posted January 4, 2004 Posted January 4, 2004 To convert datatypes you have to use CType. Dim A As String = "123" Dim B As Integer = CType(A, Integer) Alex :D Quote Software bugs are impossible to detect by anybody except the end user.
nakhatib Posted January 4, 2004 Author Posted January 4, 2004 I already tried ctype but it does not work Quote
Administrators PlausiblyDamp Posted January 7, 2004 Administrators Posted January 7, 2004 Could you give a bit more information? What is the datatype you want to convert the string to and what relation is the string to this datatype? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
nakhatib Posted January 9, 2004 Author Posted January 9, 2004 the example this is what i want to do exactly: dim t as textbox dim s as string="textbox1" t=s 'here i want to convert the string "s" to a textbox type Quote
Administrators PlausiblyDamp Posted January 9, 2004 Administrators Posted January 9, 2004 If the control already exists on the form then you could find it through a loop Dim s As String = "TextBox1" Dim t As TextBox Dim c As Control For Each c In Me.Controls If c.Name = s Then t = c Exit For End If Next There may be a better way but that should do the trick. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
nakhatib Posted January 12, 2004 Author Posted January 12, 2004 i think no one in the world knows the answer (""""""""""""""""""""""""""" UNFORTUNATELY """"""""""""""""""""""""""""""""""""") :( ::mad: :-\ :confused: Quote
Administrators PlausiblyDamp Posted January 13, 2004 Administrators Posted January 13, 2004 Still not sure what you mean. Given a string TextBox1 do you want to find an existing textbox or create a new textbox? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
nakhatib Posted January 14, 2004 Author Posted January 14, 2004 The string represent an existing textbox named textbox1. I want the methodology used to convert strings to class types. the ctype did not work, the type.gettype did not work, the gettype did not work. I do not want to use late binding, this means that i do not want to define the textbox variable as object. What i want is to define a variable of type textbox like this: dim a as textbox. and then i want this variable(a) to represent an existing textbox on the form that its name stored in a string variable. So i mean that the string content must be converted to a textbox type(textbox1) which is already exist and then make my variable a represent this existing textbox "textbox1" I hope it is clear now. Quote
Administrators PlausiblyDamp Posted January 14, 2004 Administrators Posted January 14, 2004 did you try the code I posted earlier? Dim s As String = "TextBox1" Dim t As TextBox Dim c As Control For Each c In Me.Controls If c.Name = s Then t = c Exit For End If Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
nakhatib Posted January 15, 2004 Author Posted January 15, 2004 i know this methodology. i do not want to use it, because i need performance (looping through the controls collection that contains 300 controls in my case). i need a direct conversion mechanism to optimize performance. Quote
splice Posted January 15, 2004 Posted January 15, 2004 (edited) It's Not possible to do that cast! You could however create a hashtable with the key being the objects name and the value is the Control object. That's about as fast as you are going to get.... So, when you want to access a control it would go something like this: Dim c As Control = DirectCast(ctlTable("text name"), Control) Keep in mind that looping over 300 objects in an array *should not * lag your application. Edited January 15, 2004 by splice Quote -=splice=- It's not my fault I'm a Genius!
nakhatib Posted January 16, 2004 Author Posted January 16, 2004 it should be made the way i told you about. i will try to do more searches in the Reflection library(i think the answer is there). If i find the solution i will submit it. Quote
beekde Posted January 26, 2004 Posted January 26, 2004 From http://drgui.coldrooster.com/DrGUIdotNet/4/drguinet4code.htm // s should include the full assembly to the class string s = "TextBox1"; TextBox t; Type stype = Type.GetType(s); t = System.Activator.CreateInstance(stype); Quote
nakhatib Posted January 26, 2004 Author Posted January 26, 2004 i already mentioned that type.gettype did not work. the example you submit is not working. it is working only with classes you create, but it will not work with .net classes like textboxes and other controls. Quote
beekde Posted January 26, 2004 Posted January 26, 2004 Sorry I misunderstood. I believe splice had the most elegant solution to your problem. To expand upon it here is a sample. I agree you should be able to do it your way. Let us know if you find out how. TextBox TextBox1 = new TextBox(); TextBox1.Text = "Hello World"; System.Collections.Hashtable map = new Hashtable(); map.Add("TextBox1", TextBox1); string s = "TextBox1"; TextBox t = (TextBox) map; Console.WriteLine(t.Text); Quote
splice Posted January 26, 2004 Posted January 26, 2004 (edited) This might be a bit more simplified. Private Function FindControl(ByVal ctl As Control.ControlCollection, ByVal name As String) As Control Dim c As Windows.Forms.Control Dim t As Windows.Forms.Control For Each c In ctl If (name = c.Name()) Then Return c ElseIf (c.Controls().Count() > 0) Then t = FindControl(c.Controls(), name) If (Not t Is Nothing) Then Return t End If Next c Return Nothing End Function Call example (in Form class): Dim c As Control = FindControl(Me.Controls(), "btnName") On a form with 60+ controls it took an average of 0.055ms to find the control on a P3-700 system.... lag.... I think not. :P Edited January 26, 2004 by splice Quote -=splice=- It's not my fault I'm a Genius!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.