Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

To convert datatypes you have to use CType.

Dim A As String = "123"
Dim B As Integer = CType(A, Integer)

Alex :D

Software bugs are impossible to detect by anybody except the end user.
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted
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.
Posted (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 by splice

-=splice=-

It's not my fault I'm a Genius!

Posted
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.
  • 2 weeks later...
Posted
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.
Posted

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);

Posted (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 by splice

-=splice=-

It's not my fault I'm a Genius!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...