Selecting Proper Variables

H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
MyString.empty
myString = Nothing
myString = ""
Are all the same

They are not the same, 'Nothing' is a null reference, 'Empty', or '""'
are not.
 
G

Guest

Scott,

I tried what you suggested and it didn't really work. I keep getting same
error, 'to type integer Not valid'. Then I tried what some other guys said,
it didn't work. Help.

Have a look at this and you tell me OK.

Thanks again.



Dim LInch As Double
Dim LFeet As Double
Dim WFeet As Double
Dim WInch As Double
Dim TFeet As Double
Dim TInch As Double
Dim SqrFeet As Double
Dim Yard As Double

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click


If Me.txtLInch.Text Is Nothing Then LInch = 0 Else
Me.LInch = CType(Me.txtLInch.Text, Integer)

If Me.txtWInch.Text Is Nothing Then WInch = 0 Else
Me.WInch = CType(Me.txtWInch.Text, Integer)

If Me.txtTFeet.Text Is Nothing Then TFeet = 0 Else
Me.TFeet = CType(Me.txtTFeet.Text, Integer)


Me.LFeet = Me.txtLFeet.Text
'Me.LInch = Me.txtLInch.Text
Me.WFeet = Me.txtWFeet.Text
'Me.WInch = Me.txtWInch.Text
'Me.TFeet = Me.txtTFeet.Text
Me.TInch = Me.txtTInch.Text


If Me.WInch = 0 And Me.LInch = 0 Then Me.txtSqrFeet.Text = Me.LFeet
* Me.WFeet Else

Me.txtSqrFeet.Text = (Me.LFeet + (Me.LInch / 12)) * (Me.WFeet +
(Me.WInch / 12))

Me.SqrFeet = Me.txtSqrFeet.Text

If Me.TFeet = 0 Then Me.txtYard.Text = (Me.SqrFeet * (Me.TInch / 12)
/ 27) Else

Me.txtYard.Text = (Me.SqrFeet * (Me.TFeet + (Me.TInch / 12))) / 27

End Sub
 
C

Cor Ligthert

Herfried,
They are not the same, 'Nothing' is a null reference, 'Empty', or '""'
are not.
did I write Is Nothing?
Although = Nothing acts the same as Is nothing when there is no reference.

(This = Nothing is me so often told by Armin, that I keep me to that, you
never knows when he comes back to this newsgroup)

:)

Cor
 
C

Cor Ligthert

Correction,

= "" is told by Armin when I was using = Nothing however with a referenced
string they are the same and Armin said than it I should not do that, so I
did = "".

Cor
 
C

Cor Ligthert

Did you see that Hefried wrote that when you put on Option Strict on you
would be warned, why you did not do that.

Dim LInch As Double
Dim LFeet As Double
Dim WFeet As Double
Dim WInch As Double
Dim TFeet As Double
Dim TInch As Double
Dim SqrFeet As Double
Dim Yard As Double

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click
If txtWInch.Text Is Nothing

'This is an impossible situation as you write it
As sample
'Dim a as String
'Now a Is Nothing
'Dim a as String = ""
'Now a = Nothing
And because Text is a Text box property it is never Is Nothing

A problem however is that you did not test for numeric
Although this is not always sufficient, it is in most cases

If IsNumeric(txtWInch.Text) then
WInch = Cdbl(WInch.Text)
Else
'Show errormessage and do something.
End if

And than the rest in the same style.

I hope this helps?

Cor
 
C

Cor Ligthert

Brrrrrrrrr
Did you see that Hefried wrote that when you put on Option Strict on you
would be warned, why you did not do that.

His name is Herfried, would give me a long message when I did not correct
that.

:)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
did I write Is Nothing?
Although = Nothing acts the same as Is nothing when there is no reference.

\\\
Dim s1 As String = String.Empty
MsgBox(s1.Length) ' 0.
Dim s2 As String = Nothing
MsgBox(s2.Length) ' 'NullReferenceException'.
///

That's what I was talking about. If you compare a string that is
holding a null reference to 'String.Empty', you will receive a 'True' as
the result of the comparison. Nevertheless, both string variables
behave differently.
 
C

Cor Ligthert

OK

We have no difference in my opinion about that, I did not understand that
you was meaning "it can be" a null reference.

Therefore I use as well as Armin said String = ""

(As about in reallity about of course for very few things)

:)

Cor
 
S

Scott M.

When you say "It didn't work", can you be more specific? What error are you
getting?
If Me.txtLInch.Text Is Nothing Then LInch = 0 Else
Me.LInch = CType(Me.txtLInch.Text, Integer)

Should be:

If txtLInch.Text = string.empty Then
LInch = 0
Else
Me.LInch = CType(Me.txtLInch.Text, Integer)
End If
Me.LFeet = Me.txtLFeet.Text
'Me.LInch = Me.txtLInch.Text
Me.WFeet = Me.txtWFeet.Text
'Me.WInch = Me.txtWInch.Text
'Me.TFeet = Me.txtTFeet.Text
Me.TInch = Me.txtTInch.Text

Should be:

LFeet = CType(txtLFeet.Text,Double)
LInch = CType(txtLInch.Text,Double)
WFeet = CType(txtWFeet.Text,Double)
WInch = CType(txtWInch.Text,Double)
TFeet = CType(txtTFeet.Text,Double)
TInch = CType(txtTInch.Text,Double)
 
S

Scott M.

Cor,

I'm not sure I'm following your point. If a user types: 12e4 and then you
check that value with IsNumeric, it will return true, allthough the value is
not completely numeric. If you use IsNumeric as your basis for determining
if doing math (for example) on the input value and the user had typed 12e4,
IsNumeric will return True, you would then assume it is safe to do math and
your statement would fail.
 
C

Cor Ligthert

Scott,

I hope that this sample makes clear what I want to say.
\\\\
Dim str As String = "12E4"
If IsNumeric(str) Then
MessageBox.Show(CLng(str).ToString)
End If
///

However you can of course test if there is something as

if str.tolower.indexof("e") <> -1 then doError

Cor
 
D

David

you should check the contents of the text boxes first to see if they can be
converted to numbers. - (use the isnumeric function)
then you should convert to and assign to -> variables of a numeric data
type - like double.
then use the numeric variables to do your calculations.

IsNumeric has extremely limited usefulness, and is worth calling only in
very unique situations. It does little more than waste time, since it
almost never gives you the information you really need to know. It's
much better to simply try to convert to the type of variable you want,
and catch the exception on failure. After all, that's pretty much all
IsNumeric is going to do, except that IsNumeric has to check all kinds
of possibilities that aren't useful in the current context, and you
*still* need to embed your conversion in a Try/Catch because IsNumeric
doesn't tell you whether the string in question is valid for the type
you really need.

For example, in this case, the user really needs integers rather than
doubles, and IsNumeric doesn't tell you whether the current string is a
valid integer.
by the way - at the top - you should type 'option strict on' and 'option
explicit on'
vb 6.0 was much easier to learn for newbies.

Agreed on both wholeheartedly. I would add though, that VB.Net makes it
much easier for experienced programmers to write good programs.
 
D

David

Scott,

I hope that this sample makes clear what I want to say.
\\\\
Dim str As String = "12E4"
If IsNumeric(str) Then
MessageBox.Show(CLng(str).ToString)
End If
///

In which case you just threw an exception if str doesn't fit into a
long. IsNumeric probably took *longer* to run than the CLng, and still
didn't tell you if the CLng would be a valid call.
 
H

Herfried K. Wagner [MVP]

* David said:
In which case you just threw an exception if str doesn't fit into a
long. IsNumeric probably took *longer* to run than the CLng, and still
didn't tell you if the CLng would be a valid call.

That's why there will be a 'TryParse' method for the 'Int32' datatype in
..NET 2.0.
 
S

Scott M.

Right, that's my point.


David said:
In which case you just threw an exception if str doesn't fit into a
long. IsNumeric probably took *longer* to run than the CLng, and still
didn't tell you if the CLng would be a valid call.
 
C

Cor Ligthert

Herfried,
That's why there will be a 'TryParse' method for the 'Int32' datatype in
.NET 2.0.
Which you can make yourself of course by just making a methode which does a
simple calculation. When an error is thrown you know there it is not numeric
or whatever test you put in it, however I do not find that elegant.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
Which you can make yourself of course by just making a methode which does a
simple calculation. When an error is thrown you know there it is not numeric
or whatever test you put in it, however I do not find that elegant.

Sure, you can implement such a method currently too. Nevertheless, I
prefer a speed-optimized version that comes out of the box.
 
J

Jay B. Harlow [MVP - Outlook]

SStory,
Better to use string.empty instead.
Agree, I normally use String.Empty, although I use "" also.
using "" means vb creates a new string for each ""
Not really, the compiler, CLR & JIT will intern "" to the same "" that
String.Empty refers to. In other words they both refer to the same string
instance!

It even appears that String.Trim will return the same instance (when the
result is empty).

Dim s1 As Object = String.Empty
Dim s2 As Object = ""
Dim s3 As Object = "aaa".Trim("a"c)

Debug.WriteLine(s1 Is s2, "s1 Is s2")
Debug.WriteLine(s1 Is s3, "s1 Is s3")
Debug.WriteLine(s2 Is s3, "s2 Is s3")

Remember that the Is operator compares instances, not values:

Dim a1 As String = "aaa"
Dim a2 As String = New String("a"c, 3)
Debug.WriteLine(a1 Is a2, "a1 Is a2")
Debug.WriteLine(a1 = a2, "a1 = a2")


Hope this helps
Jay
 
Top