Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Visual Basic newbie help!
Old 08-19-2007, 07:52 PM Visual Basic newbie help!
extra.account's Avatar
Average Talker

Posts: 25
Trades: 0
Hey

Just started with Visual Basic today, finding it unnecessarily hard. All the tutorials I have used and the code i've copied just produces errors.

I'm trying to make a program to calculate the area of a rectangle. A form with 3 text boxes and a command button, heres my code:

Code:
Public Class Form1

    Private Sub cmdCalculate_Click()
        Dim rec1 As Integer
        Dim rec2 As Integer
        Dim rec3 As Integer

        rec1 = txtLength
        rec2 = txtWidth
        rec3 = rec1 * rec2

        textArea = rec3

    End Sub

End Class
Yet it fails and the bits highlighted in red are underlined with error messages. The textArea bit says that textArea hasn't been declared and the other two state "Value of type System.Windows.Form.Textbox cannot be converted to integer."

Whats going on? I'm using http://www.cleartutorials.com/calcul...ual-basic-6/27
as a tutorial.

I'm using "Visual Basic 2005 express edition" which i downloaded from the microsoft site.

Thanks
extra.account is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-19-2007, 08:01 PM Re: Visual Basic newbie help!
extra.account's Avatar
Average Talker

Posts: 25
Trades: 0
Ok i added this code:

Code:
        Dim textArea As Integer
        Dim txtLength As Integer
        Dim txtWidth As Integer
And the errors went away. But when I test ran the program, it worked, but when i went to type into the textboxes i got a "LoaderLock was detected. Do not attempt to run code inside a Dllmain........."

Why? Happens everytime, then the application doesnt respond and then nothing happens.
extra.account is offline
Reply With Quote
View Public Profile
 
Old 08-19-2007, 09:32 PM Re: Visual Basic newbie help!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
First problem is you're reading outdated tutorials that tell you about VB 6, but you're working in VB.NET.

In the first example, you've got some integer variables that you're using to describe a rectangle in primitive terms ... and you have three textbox variables representing user interface elements on your window or page. Obviously, these two kinds of variables are very different. In your second example, I would think your code just wouldn't compile, but that's odd. Anyway, you can't just replace the user interface elements where people tell you some data with integers to hold that data.

Try:

Public Class Form1

Private Sub cmdCalculate_Click()
Dim rec1 As Integer
Dim rec2 As Integer
Dim rec3 As Integer

rec1 = int.Parse(txtLength.Text)
rec2 = cInt(txtWidth.Text)
rec3 = rec1 * rec2

textArea.Text = rec3

End Sub

End Class
You can use int.Parse or cInt ... they'll ultimately have the same effect. Which is to turn "17" into just 17, but to throw an exception if you try to convert "seventeen" into a number. It's a matter of preference ... cType is a VB only set of functions, whereas type.Parse extends across the framework.

You have a variable pointing at the text boxes; you need it's .Text property to get what someone typed into it. You could use its other properties to calculate its width, to move it, enable or disable it, etc.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 08-19-2007, 09:56 PM Re: Visual Basic newbie help!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I think you may have missed out a few steps

The example you are using does has some lazy coding examples in it

Set the names of your text boxes to txtLength, txtWidth and txtArea

in the IDE double click the button, this will create you a Click procedure


which will look like

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
Inside this subroutine is where your code goes so the form code will look like
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rec1 As Integer
        Dim rec2 As Integer
        Dim rec3 As Integer

        rec1 = txtLength.Text
        rec2 = txtWidth.Text
        rec3 = (rec1 * rec2)


        txtArea.Text = rec3

    End Sub
End Class
save and run the form.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-19-2007, 09:58 PM Re: Visual Basic newbie help!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
He can type quicker that I can

and what's this about VB6 being outdated????
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-20-2007, 04:42 AM Re: Visual Basic newbie help!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Quote:
Originally Posted by chrishirst View Post
and what's this about VB6 being outdated????
Well some of the rules have changed, especially when it comes to types. The code would work perfectly in asp, but in a windows or web project in .net, because you can put anything into an object type - a lot like a variant - you're not allowed to just refer to a text box or check box or drop down and pull it's default property anymore. Microsoft would have had to make the compiler a little more intelligent for that to work ... although we get "indexers."

Oh, and all I meant about vb 6 is that the Amish web site I saw used asp 2.0. Microsoft decided instead of fixing things like allowing buffer overruns in the first place, they'd instead kill off more direct access to memory, and .net was born. Managed code, like java ... not javascript but actual java. With all that said, for writing windows software, COM is dieing slowly.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 08-20-2007, 08:36 AM Re: Visual Basic newbie help!
extra.account's Avatar
Average Talker

Posts: 25
Trades: 0
Hey guys, thanks for your replies. Got it working.

Do you think I should only be reading tutorials on VB.net and not just googling "visual basic tutorials" like I have been?

Also, am I right in saying that if i made a program in VB.net and sent it to someone, it wouldn't work cause most people don't have the framework installed?
extra.account is offline
Reply With Quote
View Public Profile
 
Old 08-20-2007, 09:19 AM Re: Visual Basic newbie help!
hamesy's Avatar
Webmaster Talker

Posts: 639
Name: Steve
Location: Birmingham, England
Trades: 0
If you are learning VB .NET then I recommend that you read tutorials related to VB .NET. The reason for this is because the way .NET handles things is totally different to Visual Basic.

Also I'm not 100% on this, but most windows users have the .NET framework installed on their machine, as it comes default on newer machines (2002 onwards) so they shouldn't have a problem with running your program.
__________________
Hamesy

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
hamesy is offline
Reply With Quote
View Public Profile
 
Old 08-20-2007, 03:11 PM Re: Visual Basic newbie help!
extra.account's Avatar
Average Talker

Posts: 25
Trades: 0
Another quick one, i spent a while googling code to detect if a variable is empty. Current if i just hit the button the program crashes, what code can i use to see if an "double" variable is empty? (I am creating a currency converter now)
extra.account is offline
Reply With Quote
View Public Profile
 
Old 08-20-2007, 03:25 PM Re: Visual Basic newbie help!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
I take it you're writing Windows software. Most people seem to have the framework installed, with the exception of a few dial up users, and a few "Windows for Workgroups 3.11was the best operating system in history" types.

Use TryParse instead of Parse, and/or check beforehand to make sure empty strings. You can use If string.IsNullOrEmpty(x) or If x = "".
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to Visual Basic newbie help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.56723 seconds with 12 queries