Jump to content

Need help with VB project

zohan6934

I have a project due in a couple days for my class, it's in vb (I'm sorry) 

I keep getting an error that says  "Value of type 'String' cannot be converted to 'System.Windows.Forms.Label'."

Option Strict On
Public Class Form1
' Currency Calculator
    ' -----
    ' 2/22/2016
    ' Program will calculate currency exchange from Euros to USD and from Yen to USD
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        ' This event handler will take the users input and times it by 1.13 to get the exchange rate for Euros to USD
 lblUSD1 = Format("currency")
        ' Declare a variable to calcualte the exchange rate
        Dim dblexchange As Double
        ' Takes the number in txtEuro and times it by 1.13 to get the exchange rate
        dblexchange = CDec(txtEuro.Text) * (1.13)
        ' Displays the Result in lblUSD1
        lblUSD1.Text = CStr(dblexchange)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' This event handler will take the users input and times it by 0.0087 to get the exchange rate for Yen to USD
 lblUSD2 = Format("currency")
        ' Declare a variable to calcualte the exchange rate
        Dim dblexchange As Double
        ' Takes the number in txtEuro and times it by 1.13 to get the exchange rate
        dblexchange = CDec(txtYen.Text) * (0.0087)
        ' Displays the Result in lblUSD1
        lblUSD2.Text = CStr(dblexchange)
    End Sub
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ' Clears all numbers from the Text boxes and labels
        txtEuro.Clear()
        txtYen.Clear()
        lblUSD2.Text = ""
        lblUSD1.Text = ""
        ' Reset the focus
        txtEuro.Focus()
    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

 

 

 

I'm trying to create a currency converter for Euro and Yen to USD but my teacher wanted it the currency format, where it rounds to the nearest hundredths place in the decimals and includes the $ sign as well as any commas needed 

If anyone could help me out and point out in detail what it is I'm forgetting I'd love you forever

 

I'm sorry I'm still really new to programming

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, zohan6934 said:

-snip-

I'm not sure about this but instead of :

lblUSD2 = Format("currency")

Try:

lblUSD2.text = Format("currency")
or
lblUSD2.caption = Format("currency")

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
Share on other sites

Link to post
Share on other sites

Dave's got it: lblUSD1 is the actual label control, the object itself. You're wanting only to assign a value to the text property of that label so need to use lblUSD1.text = ... it's not the label itself, but instead the value to assign, which you want to format as currency.

 

This link describes one way of converting a string to a currency format, picking up the user's local computer settings (i.e. "£1,000.00" for UK or "1.000,00 €" etc.). String.format() is another way also. https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#CFormatString

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

On a side note, I want to try to give some helpful advice for the future - it doesn't matter so much right now you're learning & having fun, but these are some bits (haha!) which'll serve you well later if you take programming up professionally.

 

1. The CDec, CStr conversion methods are all valid & will run, but they are included as a throwback for people who have learnt VB6 or earlier, VBScript or VBA (the one in Office products). The newer, and preferred .net way, well one form of .net way is to use the convert.toDecimal, convert.toString etc. methods instead. The reason for this? The .net framework is shared and used by all .net "managed" languages - it'll mean C#.net, F#.net etc. programmers could pickup your code and easily understand it - they're used to using the convert.toString methods just the same (except with a ";" symbol at the end of their code perhaps), whereas CStr is a VB language specific thing which other programmers might not be aware of.

 

2. Comments are super important, and often, they're not written enough. Right now your code is great & will serve you well whilst learning. But you're like me, wanting to explain everything. In the future, you'll want to focus on more writing comments for why you're doing something & leave the programmer reading the code to understand how the code is implemented to achieve this (i.e. if you look at this code in a year's time without any comments, you'll understand those lines perfectly as-is). For those smaller methods, you might later just need a 1 line comment in each (possibly above the method by entering ''' above each private sub line).

 

3. You're taking input from the text box then multiplying that with a number. I realise again that you're learning now & probably the idea of error & exception handling won't be covered until a much later time, however again for future, a general rule of thumb is never to trust anything which a user can interact with. If there's a textbox asking for a number, there will be a time someone writes in there "one thousand" or some other text string value. As you get more familiar with programming, you'll always want to check for this somehow (either by playing with the properties of the textbox etc. control, or then through code by saying "did the user enter anything? was it a number? yeah, but was it a real number - have I got many commas or decimal points in there? Is the number in the right format? is it a minus number? is it less than 2 decimal places or do I need to round it? ok well then it appears to be valid, phew finally lets try to multiply it & hope for the best...")

 

4. I understand you're learning about variables which is great. It's made your code clearer to understand by far. Later down the line though, it'll serve you well to consider the RAM allocation usage in this though. RAM is really cheap right now for PCs, but if you understand & consider what's happening when you declare a variable, it'll help if you then wanted to move to really lower powered hardware programming and broaden your prospects especially if IoT ever becomes more than just a buzzword. I want to re-iterate here, there's nothing wrong with your code at all & whilst those methods could mostly be written on one line of code each, it's not at all always the best approach to take. Deciding whether to write for better performance, less CPU cycles and less RAM allocation (with a lot of comments to help) versus writing for clarity & future ease of maintenance is one of the struggles and joys programmers can often face. There's no right or wrong way & both will run, I just wanted to throw this consideration out there to help your knowledge & to help you become a guru developer in the future if you start to understand & consider always what's happening "under the hood" when your code might execute.

 

Good luck!

Link to comment
Share on other sites

Link to post
Share on other sites

55 minutes ago, alex_read said:

-Almighty snip-

This is among the best advice for beginners I have seen. Being mostly self taught, until finally going and getting a diploma in software development, I learned the hard way about commenting your code. I got back to things even the next day and forgot what it did sometimes. 

 

And on the input validation, that is also something you want to learn sooner rather than later, saves the headaches down the line. Few extra steps now saves a lot of time down the line trying to track down issues then implementing the validation you should have done in the first place. 

 

@zohan6934 if you have any programming questions, feel free to PM me, I'll do my best to help. While I mainly use C#, it's still a .NET language, so I should be able to convert just fine 

Link to comment
Share on other sites

Link to post
Share on other sites

Cheers :cD  I also came from the self-learnt camp. I never went back for a computer science course myself but now can hugely understand & appreciate why they exist. I've had a hell of a lot of catching upto do to understand the concepts they teach & am always still trying to learn.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, alex_read said:

1. The CDec, CStr conversion methods are all valid & will run, but they are included as a throwback for people who have learnt VB6 or earlier, VBScript or VBA (the one in Office products). The newer, and preferred .net way, well one form of .net way is to use the convert.toDecimal, convert.toString etc. methods instead. The reason for this? The .net framework is shared and used by all .net "managed" languages - it'll mean C#.net, F#.net etc. programmers could pickup your code and easily understand it - they're used to using the convert.toString methods just the same (except with a ";" symbol at the end of their code perhaps), whereas CStr is a VB language specific thing which other programmers might not be aware of.

When it comes to string conversions, there's more than just the Convert.To methods as well which you alluded to but I'll explain below.

 

The Convert.To methods (on strings) are simply adding a null check to the Parse methods.

// Implementation of Convert.ToInt16 given a string (using dotPeek)
// Sorry about the C#. dotPeek doesn't generate any VB code at this time

public static short ToInt16(String value) {
    if (value == null) 
        return 0;
    return Int16.Parse(value, CultureInfo.CurrentCulture);
}
 
public static short ToInt16(String value, IFormatProvider provider) {
    if (value == null) 
        return 0; 
    return Int16.Parse(value, NumberStyles.Integer, provider);
} 

If this is the functionality you want, then go ahead and use it. If not, you'll want to choose another method. If you know the value can never be null, then just go ahead and use Parse. If you need to know the difference between input that is a valid, like "0", vs a null input, then Convert.To isn't what you want either.

 

Since both Convert.To and Parse can throw exceptions on invalid input you'll need to handle those exceptions as well. One option is to wrap them in a Try/Catch. This can be useful if you need to handle specific exceptions in different ways, but many times you do the same thing no matter which exception is raised and the TryParse methods are specifically optimized to handle this task.

 

tl;dr: In many cases you'll probably want to use TryParse over any other string conversion method.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×