VB methods & syntax

Started by
9 comments, last by Ravyne 14 years, 3 months ago
In my pursuit of re-learning how to program, I searched through my things and and came across my text book for Visual Basic 6.0. I figured that if I wanted to program again I should start at the begining. I read through the book and did exactly what it says. Somehow, things still manage to get screwed up. The book told me to write the following code: Private Sub cmdDisplayImage_Click() imgCurrent.Picture = LoadPicture(txtFileName.Text) End Sub As the code suggests, when I click the DisplayImage button it should load whatver image is in the file that txtFileName.Text points to. Only, it doesn't... 1st of all, it has no idea what imgCurrent is, which makes sense to me because it's never declared as a variable nor is it a control on the form. 2nd, even if imgCurrent did exist Picture is not an existing property. Lastly, LoadPicture is not a recognized method. So I recieve nothing but errors, it doesn't even run. I have tried another way to get it what I want to due. I changed the 3rd line of code to: imgDisplayImage.ImageLocation = txtFileName.Text It runs, but it doesn't get the image from the file name I type into the text. Can anyone tell me what is wrong?
Advertisement
Can you open the .frm file in notepad and paste it here? Does it tell you to put a Picturebox or an Image control on the form? It sounds like you don't have a control (either Image or Picturebox) on your form but there is supposed to be one.

Also, here's an MSDN link that may help you out.

http://msdn.microsoft.com/en-us/library/aa264946%28VS.60%29.aspx


Oh, also, do you see "LoadPicture" appear in the intellisense list? If not, what Service Pack of VB6 are you running?

Make sure you choose "Start with full compile" (Ctrl-F5) to run your programs, and also include the line "Option Explicit" at the top of your code modules/forms/etc.
I have a picture box control, it's named imgDisplay. I have no idea what service pack I'm using or where I can even find that information within.

The only code I have in the program is as follows:

Option Explicit ' I just added this part at your suggestion

Public Class Form1

Private Sub cmdDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplay.Click

imgDisplay.ImageLocation = txtFileName.Text

End Sub
End Class

The intellisense list also doesn't come up with "LoadPicture".
In the About Microsoft Visual Studio 2008 Express Edition it says:

Microsoft Visual Studio 2008
Version 9.0.30729.1 SP

and

Microsoft .NET Framework
Version 3.5 SP1
For starters, you're trying to code using VB6 in VB.NET -- though intended to be similar, they are distinct, incompatible languages.

I understand that you are working from the materials you have on hand, but VB6 is no longer in use, not available, and has been replaced by languages like C# and VB.Net.

While VB.Net may sound like exactly the ticket to "upgrade" your skills, I would seriously consider moving to C# instead, for a couple reasons.

Firstly, you're learning a new language no matter what -- VB.Net is different enough from VB6 that it's not going to be significantly harder to learn than C# will be.

Secondly, and a problem quite to the opposite, is that its still similar enough to VB6 that you will in all likelihood find yourself running up against problems in VB.Net as a result of thinking "Well, it was like this in VB6, why doesn't it work here?". A clean break to a fully distinct language will help avoid this.

Finally, in many ways, C# is really the *true* successor to the VB.Net lineage of RAD languages. It does everything that VB6 ever did, and it does it better and with greater ease. I'm personally convinced that the only reason that VB.Net was even created is because Microsoft's corporate clients were/are affraid their on-staff programmers would demand a higher salary if they had to program in a language that looked like C or C++.

C# express is available free as well, just as VB.Net is.

throw table_exception("(? ???)? ? ???");

Opinions follow - C# also has its share of design decisions that come from it's alignment with C/C++, Java - Frankly, I've worked with both, and I find myself a lot less annoyed with VB .Net that C#. (I abhor case sensitivity. I also like having clues as to which end of a code block exactly am I looking at.)

In any case, if you don't have an existing familiarity with either, pick whichever one you like. VB has a couple of disadvantages in my mind: 1st, despite Microsoft's statements to the contrary, most people do seem to think like Ravyne, even within Microsoft - so from a resume point of view, C# might look better. Second, the only situation I know of where VB does seem left behind by Microsoft is with XNA, which since you're posting on gamedev might be in your crosshairs. It is still possible to use VB, but only by jumping through some hoops.

All of that said, though, I do think that VB has the better editor experience, and is a fine language to use to learn how to program. It is also quite easy to jump between VB and C#, since they have allmost full feature parity.
One more thing:
http://msdn.microsoft.com/en-us/default.aspx

Has links to vb and C#, and under those there are multiple help docs to start learning either language.
God I hate Microsoft... They are both called Visual Basic even though they are practically two different languages (or maybe even are from the sound of the posts you gave me). Microsoft doesn't seem to do anything right. From their OS's to their game systems. They seem to have the reverse midas touch, anything they touch turns to crap. Thanks for your help though.

BTW, I know this is off topic (and probably on the wrong board) but has anyone used Windows 7? I heard they supposedly beta tested it (for once!). Does it actually work like it's supposed to? I'm using Vista on this brand new computer and it already is running like crap.
Yes, it is off-topic, but Windows 7 is great... but then again, I thought Vista was a good OS as well.
Firstly, the reason why you don't have the property .Picture is because you're not referencing the control properly.

As you've written this as a class, it's going to be in a class module, so you need to do:

Form1.imgDisplay.Picture (replace Form1 with whatever your form name is).

Secondly, use proper VB6 not .NET!!!

I'm going to get shot for this, but I personally have refused to upgrade to .NET, I tried it once, lost my temper with it. VB6 is fantastic for learners and for RAD, although maybe a little dated. If you need anything really powerful that VB6 doesn't offer then I jump over to C++, in the worse case creating a DLL in C++ to get that functionality and using VB6 to wrap it all up and provide a GUI for.

This topic is closed to new replies.

Advertisement