PDA

Click to See Complete Forum and Search --> : When to declare variable as texbox or string?


tony007
May 16th, 2005, 01:52 PM
HI guys. I want to learn when to declare a variable as string and when
to declare it as TextBox like the example below?
How to use visual studio .net 2003 to construct these variables for us?

Furthermore, in constructing the sql statement like the example below, when we need to use replace andWhen not to use it.?
How to use visual studio .net 2003 to construct this sql statement for us?

I addition, when to use single ' and when not to use it?

Furthermore, why the “replace” is used in this particular order in the example below?


I will be happy if some expert teaches me how to use them properly.
A reference to a complete tutorial about these topics is highly appreciated.




Dim tbText0 As String = E.Item.Cells(0).Text
Dim tbText1 As String = E.Item.Cells(1).Text
Dim tbText2 As TextBox = E.Item.Cells(2).Controls(0)


' Update the appropriate record in our database.
Dim objCommand As SqlCommand
Dim strSQLQuery As String

' Build our update command.
strSQLQuery = "UPDATE MATCHES " _
& "SET teamno = '" & Replace(tbText0, "'", "''") & "', " _
& "PLAYERNO = '" & tbText1 & "', " _
& "won = '" & Replace(tbText2.Text, "'", "''") & "' " _
& "WHERE matchno = '" & tbText1 & "';"

nemaroller
May 16th, 2005, 08:38 PM
1: Dim tbText0 As String = E.Item.Cells(0).Text
2: Dim tbText1 As String = E.Item.Cells(1).Text
3: Dim tbText2 As TextBox = E.Item.Cells(2).Controls(0)

Here's how it should be written, and i'm going to analyze a few things for you. The whole intent of this code is to build a sql statement dynamically - which is really big no-no but that issue is far above this particular situation.

String is just a string. The first line of code is taking the value of E.Item.Cells(0) and using its Text Property. What you probably don't realize is what E.Item.Cells(0) is referring to - its refering to a TableCell within a DataGridItem.

A TableCell in a DataGridItem can contain controls and it can also just display text (but it can't do both at the same time).

Assuming you use the TableCell to display text, then the Text property that the TableCell class exposes will return a string of text that the TableCell contains. In the first line of code, you are creating a string called tbText0 that now contains the same characters as the string contained in the TableCell.

How ya doing?

The second line follows the same type of logic - reads the Text property of a TableCell contained with a DataGridItem and mirrors its contents in a new string called tbText1.

Now, the third line is a little different. Because now, you're not reading some text and assigning a string. Now you're actually referencing a control contained within a TableCell and assigning that reference to TbText[\b]. TbText is a TextBox, which is exactly the type of control that is contained within E.Item.Cells(2). The [b]Controls(0) at the end means to grab the first control contained within the TableCell... if there were two textboxes within the cell, you grab the other textbox by using .Controls(1).

Anyway that's about it for your first issue - other than your code should look more like this so any other person looking at the code easily realizes the last control is a textbox, and the first two lines are assigning strings.

Dim firstName As String = E.Item.Cells(0).Text
Dim lastName As String = E.Item.Cells(1).Text
Dim tbAddress As TextBox = E.Item.Cells(2).Controls(0)


And VS does not generate this type of code for you.