VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,579 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,234 people online right now. Registration is fast and FREE... Join Now!




sub procedure

 

sub procedure, sub procedure

pomak

22 Nov, 2008 - 03:03 PM
Post #1

New D.I.C Head
*

Joined: 1 Nov, 2008
Posts: 37


My Contributions
hi people i have problem with my home work i have to code an application that uses two independent Sub procedure one to convert a temperature from Fahrenheit to Celsius and the other to convert a temperature from Celsius to Fahrenheit
the two formulas are highlighted and it says Option Strict On disallows implicit conversions from Double to String
if anybody can help me thanks in advance
here is my code


vb

Private Sub convertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertButton.Click


Dim celcius As Decimal
Dim fahrenheit As Decimal
Dim f As Decimal
Dim c As Decimal

celcius = Decimal.Parse(celTextBox.Text)
fahrenheit = Decimal.Parse(fahTextBox.Text)


'celcius to farenheit formula:


If celcius > 0 Then
fahTextBox.Text = c * 1.8 + 32
ElseIf celcius = " " Then
MsgBox("Enter No")
End If

'farenheit to celcius formula:


If fahrenheit > 0 Then
celTextBox.Text = f - 32 / 1.8
ElseIf fahrenheit = " " Then
MsgBox("Enter No")
End If



End Sub
End Class


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Sub Procedure

22 Nov, 2008 - 04:27 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The error means that you can't simply set the text of a textbox or anything that requires a string with a number without first converting it to the appropriate data type.

For instance....

vb

' If option strict is not on, the below line will take the number 4 (an integer)
' implicitly convert it to "4" and then put it in the textbox as a string.

txtNumber.Text = 4

' If option strict is on, no implicit conversions are allowed, so you must
' EXPLICITLY convert it yourself. Here we are telling it to take the integer
' convert it to a string and then give it to the textbox.

txtNumber.Text = CStr(4)


The reason Option strict flags this as a problem is because you should always be making sure that the data is of the right type for its destination. You should never rely on the compiler to decide what is best for your data. If it was to guess wrong, then it would lead to bugs that would be harder to track down.

You can find out more about this by doing a search for "implicit and explicit conversions".

"At DIC we implicitly assume you know what you are doing... then you prove us wrong by explicitly showing us that you don't know what the hell you are doing." decap.gif

This post has been edited by Martyr2: 22 Nov, 2008 - 04:28 PM
User is offlineProfile CardPM
+Quote Post

pomak

RE: Sub Procedure

22 Nov, 2008 - 04:45 PM
Post #3

New D.I.C Head
*

Joined: 1 Nov, 2008
Posts: 37


My Contributions
i really do not know what am doing i've been trying to do this thing all day long
i did the changes you told me and still not working
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Sub Procedure

22 Nov, 2008 - 05:11 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well I am not sure if you are working in VB.NET or vb6. Your code has hints of VB.NET but then you are using old legacy vb 6 and in the vb6 forum. So I am not sure with that.

If it is VB.NET you are in the wrong forum but no matter... here is a working example to get you started. Just make sure that both textboxes have values before you click the button.

vb

Dim celcius As Decimal = 0D
Dim fahrenheit As Decimal = 0D
Dim f As Decimal
Dim c As Decimal

celcius = Decimal.Parse(celTextBox.Text)
fahrenheit = Decimal.Parse(fahTextBox.Text)


'celcius to farenheit formula:


If celcius > 0 Then
' Notice the conversion from our decimal value to String
' Before placing it into the textbox
fahTextBox.Text = Convert.ToString(c * 1.8 + 32)
Else
MessageBox.Show("Enter celcius greater than 0")
End If

'farenheit to celcius formula:


If fahrenheit > 0 Then
' Again we convert from a decimal value to a string before
' placing into textbox
celTextBox.Text = Convert.ToString(f - 32 / 1.8)
Else
MessageBox.Show("Enter fahrenheit greater than 0")
End If


Notice in my code above where I have to explicitly convert equation results, that would have come out as decimal, into string values before placing them into textboxes which expect to be given a string value.

This is what it meant by strict not allowing implicit conversions. I have to TELL vb to convert the value.

smile.gif
User is offlineProfile CardPM
+Quote Post

pomak

RE: Sub Procedure

22 Nov, 2008 - 05:24 PM
Post #5

New D.I.C Head
*

Joined: 1 Nov, 2008
Posts: 37


My Contributions
thank you
i tried it but when i ran it it did nor work again there is a message that says this: Input string was not in a correct format.
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Sub Procedure

22 Nov, 2008 - 05:32 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Are you actually reading my posts? I told you! This EXAMPLE code assumes that you have entered data into BOTH textboxes. If you don't enter something in one or the other, it is going to try and parse nothing and cause the error.

Put a number in both boxes and then hit the button. tongue.gif
User is offlineProfile CardPM
+Quote Post

pomak

RE: Sub Procedure

22 Nov, 2008 - 05:44 PM
Post #7

New D.I.C Head
*

Joined: 1 Nov, 2008
Posts: 37


My Contributions
thank you its working
thanks again
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Sub Procedure

22 Nov, 2008 - 06:12 PM
Post #8

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:23AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month