473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Implicit Conversion

2
Hello,
I am new to VB I get this error message "Conversion from string "" to type 'Double' is not valid. when running my app, (the underline codes are the errors). which is a basketball simulator. How can I correct this; Thank you so much for taking the time to review this.
Expand|Select|Wrap|Line Numbers
  1. Private Sub playButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playButton.Click
  2.         Dim rank As Integer
  3.         Dim player As Integer
  4.         Dim shoots As Integer
  5.         Dim RandomGenerator As New Random
  6.         Dim isConverted As Boolean
  7.         Dim I As Integer = 0
  8.         'generate random player from visitor 6-10, home 1-5
  9.         player = RandomGenerator.Next(1, 11)
  10.         Label84.Text = CStr(RandomGenerator.Next(1, 101))
  11.  
  12.         For I = 0 To 4
  13.             Select Case player
  14.                 Case 1
  15.                     Label1.Text = "Home Player 1 Selected"
  16.                     If TextBox6.Text < TextBox90.Text And Label84.Text < TextBox66.Text Then
  17.                         TextBox30.Text = +1 And CDbl(TextBox25.Text) = +1 And CDbl(TextBox20.Text) = +2
  18.  
  19.                     Else
  20.                         TextBox45.Text = +1 And CDbl(TextBox39.Text) = +1 And CDbl(TextBox35.Text) = +2
  21.  
  22.                     End If
  23.  
  24.                 Case 2
  25.                     Label1.Text = "Home Player 2 Selected"
  26.                     If TextBox7.Text < TextBox9.Text And Label84.Text < TextBox12.Text Then
  27.                         TextBox29.Text = +1 And CDbl(TextBox24.Text) = +1 And CDbl(TextBox19.Text) = +2
  28.                     Else
  29.                         TextBox44.Text = +1 And CDbl(TextBox39.Text) = +1 And CDbl(TextBox34.Text) = +2
  30.                     End If
  31.                 Case 3
  32.                     Label1.Text = "Home Player 3 Selected"
  33.                     If TextBox8.Text < TextBox88.Text And Label84.Text < TextBox78.Text Then
  34.                         TextBox28.Text = +1 And CDbl(TextBox23.Text) = +1 And CDbl(TextBox18.Text) = +2
  35.                     Else
  36.                         TextBox43.Text = +1 And CDbl(TextBox38.Text) = +1 And CDbl(TextBox33.Text) = +2
  37.                     End If
  38.                 Case 4
  39.                     Label1.Text = "Home Player 4 Selected"
  40.                     If TextBox9.Text < TextBox81.Text And Label84.Text < TextBox77.Text Then
  41.                         TextBox27.Text = +1 And CDbl(TextBox22.Text) = +1 And CDbl(TextBox17.Text) = +2
  42.                     Else
  43.                         TextBox42.Text = +1 And CDbl(TextBox37.Text) = +1 And CDbl(TextBox32.Text) = +2
  44.                     End If
  45.                 Case 5
  46.                     Label1.Text = "Home Player 5 Selected"
  47.                     If TextBox10.Text < TextBox86.Text And Label84.Text < TextBox76.Text Then
  48.                         TextBox26.Text = +1 And CDbl(TextBox22.Text) = +1 And CDbl(TextBox17.Text) = +2
  49.                     Else
  50.                         TextBox41.Text = +1 And CDbl(TextBox36.Text) = +1 And CDbl(TextBox31.Text) = +2
  51.                     End If
  52.  
Feb 24 '09 #1
3 3836
Plater
7,872 Expert 4TB
What on earth is this line doing:
TextBox30.Text = +1 And CDbl(TextBox25.Text) = +1 And CDbl(TextBox20.Text) = +2
That doesn't even look like a logical assignment of anything
Feb 24 '09 #2
Frinavale
9,735 Expert Mod 8TB
I think that the following line

Expand|Select|Wrap|Line Numbers
  1. TextBox30.Text = +1 And CDbl(TextBox25.Text) = +1 And CDbl(TextBox20.Text) = +2 
Is actually supposed to be:
Expand|Select|Wrap|Line Numbers
  1. TextBox30.Text = "+1" And CDbl(TextBox25.Text) = 1 And CDbl(TextBox20.Text) = 2
Or maybe (probably more likely) it should be:
Expand|Select|Wrap|Line Numbers
  1. TextBox30.Text = "+1" And TextBox25.Text= "+1"  And TextBox20.Text = "+2"
When you are comparing Strings make sure that the values being compared are Strings.

For example:
TextBox30.Text will return a String.
In order to check if this string contains "+1" you have to compare it to a String that contains "+1".

The following will check to see if the TextBox contains "+1" as it's value:

Expand|Select|Wrap|Line Numbers
  1. If TextBox30.Text = "+1" Then
Please note the double quotes around the +1 value. This is required for the comparison to work.

Now, say you're expecting the TextBox to contain a Number, and you want to see if that number is equal to 1 (a positive 1, not a negative 1).

First of all, you cannot compare numbers to Strings. They are different animals. So you have to either cast or parse the String into a number, or you have to convert the number into a String.

To cast a String into a Number (a type Double holds a number) you would do something like:
Expand|Select|Wrap|Line Numbers
  1. Dim myNumber As Double = CDbl(TextBox30.Text)
Or
Expand|Select|Wrap|Line Numbers
  1. Dim myNumber As Double =CType(TextBox30.Text,Double)
Or
Expand|Select|Wrap|Line Numbers
  1. Dim myNumber As Double =DirectCast(TextBox30.Text,Double)
Please note that if the String does not contain a Number, then casting will throw an Exception. A String containing the value of "+1" is not a number. Therefore the cast is failing in your case....

If you want to convert a String into a number, you can use the Parse() method.

For example:
Expand|Select|Wrap|Line Numbers
  1. Dim myNumber As Double =Double.Parse(TextBox30.Text)
The above code will attempt to convert the text in TextBox30 into a Double.
If the text is not a number (don't forget that "+1" is not a number), this will also thrown an exception.

Since (I'm assuming) you are checking if the TextBox contains "+1", you cannot convert this value into a number. This is a String and needs to be compared to another String. Therefore, I recommend you change your code and compare Strings instead of casting the text into a number:
Expand|Select|Wrap|Line Numbers
  1. If TextBox30.Text = "+1" And TextBox25.Text= "+1"  And TextBox20.Text = "+2" Then
I find it incredible that the compiler even got so far to throw the exception that you're getting!

You Must realize that a "+" is an Operator that preforms addition on 2 Objects.

For example adding 2 numbers:
1 + 1

Will result in
2

Adding two Strings:
"1" + "1"

Will result in the two Strings being put together:
"11"

When you start mixing numbers and Strings things get messed up.
The compiler get's confused about what you really want.
For example say you attempt to add a String and a number together:
"1" + 1

What do you want?
Do you want the number to be converted into a String?
Or do you want the string to be converted into a number?

This is why the compiler is not happy with what you've done.
You're taking the String stored in your TextBox and attempting to check if it's equal to "something" added it to a number:
Expand|Select|Wrap|Line Numbers
  1. TextBox30.Text = +1 
The compiler's throwing an error because it doesn't know what you are doing.
(We actually don't know what you're doing either)

The lesson here is: don't mix types.
Tell the computer exactly what you want and it will do what you ask.
Feb 24 '09 #3
candy1
2
Thank you for taking the time to go through my code. What I want the computer to due it the conditions are true to add points to the textboxes. Which are scores of the players. If the condition are true then the textboxes store the points for shots attempted, shots made and points made. I am apparently way off. I'm looking for an example of a basketball simulator that has match ups. It is back to the drawing board. Thanks again.
Feb 24 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
1
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
3
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef...
3
by: Jess | last post by:
Hello, I can perform implicit conversion through constructor, like class A{ public: A(int x):a(x){}; int a; };
1
by: drop | last post by:
Hi all, I'd like to know if it's possible to declare an implicit type conversion for when I use declarative Synthax in html. For example, when I have the DropDownList, I can declatively set...
4
by: The Last Danish Pastry | last post by:
If I have two types, t1 and t2, say, I can write an implicit conversion operator from t1 to t2. Can I write implicit conversion operator from t1 to t2? I don't think I can... but, if not, what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.