Click to See Complete Forum and Search --> : General questions
Blixa
Dec 29th, 2009, 10:44 AM
Hello,
I hope I am posting in the right forum saw a few alike sub categorys.
Anyway I recently started with VB2008, there seems to be a 2010 should I switch while I am still at the very beginning?
I have a few questions in general. I used to program/script in other languages and there seem to be alot of differents for the moment. And yes I googled most of it and a clear answer never game out or I am looking in the complete wrong place.
First of all Subs. This seem to be some sort of function but its not. What is it exactly?
Global variables, I have read some tutorials but they refer to a tab in VB2008 that says '(general)' and '(Declaration)' however nothing changes withing VB2008 on selecting those.
Modules, What are those? on first sight they seem to be sort of code structure to be called on later.
Namespacing, Never really had to worry about those. It seems to be a sort of easy way to level your libary. Like I saw 'Me.system' and Me is the name of the namespace and it includes 'system' to your current page/program(sort of include?)
How do you know where the program is running from? For example the program is running from C:\myprogam\ how do i know this so I could load files from there? like images/text/whatever.
Multipule forms, Lets say I start form1, like the default naming, and I want on a button click to open an other form, form2, how can I interact with both of them? Like a page of 'settings' form1 has to adjust to that. Pass By Referrence comes to mind.
When I click properties on my project in the Solution explorer I get a view tabs. What are the following: Resources, Service, settings(refers to my question above???), Reference?
What is the 'main' for my program is it form1? and is that also the global scope?
Threads, I heard thing about this during video tutorials like thread pool, single thread, thread synchronization. What are they and what do they do? I assume windows somehow works with this.
If for example I got 'settings' in my program I have to save those in a file and load it in every time the program is being run?
I assume that a programs written in VB only works on Windows? true/false?
That are the questions for now. I do not expect any completly written explainations but would be much appreciated. Linking tutorials/other resource would be great.
Thanks in advance
JuggaloBrotha
Dec 29th, 2009, 11:59 AM
Hello,
I hope I am posting in the right forum saw a few alike sub categorys.
Anyway I recently started with VB2008, there seems to be a 2010 should I switch while I am still at the very beginning?
I would stick with VS 2008 until 2010 is officially released, while they do work together just fine 2010 is still in Beta and bad things could happen, I prefer to be cautious
I have a few questions in general. I used to program/script in other languages and there seem to be alot of differents for the moment. And yes I googled most of it and a clear answer never game out or I am looking in the complete wrong place.
First of all Subs. This seem to be some sort of function but its not. What is it exactly?
A sub is a function that doesn't return anything.
For example in Java and C# if you want a sub you declare a void function:
private void function YourSubName{
}
In vb and vb.net you just use the sub keyword:
Private Sub YourSubName
End Sub
Global variables, I have read some tutorials but they refer to a tab in VB2008 that says '(general)' and '(Declaration)' however nothing changes withing VB2008 on selecting those.
A global variable is just a variable that can be accessed anywhere in the project, usually in vb and vb.net you declare it in a module
Modules, What are those? on first sight they seem to be sort of code structure to be called on later.
A module is a static shared class, all Public/Friend/Protect Public variables, subs, functions can be accessed from all other files in the project.
Namespacing, Never really had to worry about those. It seems to be a sort of easy way to level your libary. Like I saw 'Me.system' and Me is the name of the namespace and it includes 'system' to your current page/program(sort of include?)
'Me' always refers to the current instance of the Class you're coding in, there is no 'System' namespace in 'Me', 'System' is a root namespace in the Framework. System.IO, for example, has tons of IO classes you can use to access things on the file system.
How do you know where the program is running from? For example the program is running from C:\myprogam\ how do i know this so I could load files from there? like images/text/whatever.
Application.StartUpPath is the full path of where the executable is on the file system
Multiple forms, Lets say I start form1, like the default naming, and I want on a button click to open an other form, form2, how can I interact with both of them? Like a page of 'settings' form1 has to adjust to that. Pass By Referrence comes to mind.
In form1:
Dim frm2 As New Form2
you now have declared and instantiated a Form2, so frm2.Show will physically show Form2, you can have multiple Form2's floating around.
When I click properties on my project in the Solution explorer I get a view tabs. What are the following: Resources, Service, settings(refers to my question above???), Reference?
What is the 'main' for my program is it form1? and is that also the global scope?
Sub Main is typically where Java and C# programs start (vb.net does to, but you generally never see the actual Sub Main) but vb.net allows you to specify your own Sub Main, you generally put it in a module and the program completely terminated when the code hits the 'End Sub' part of 'Sub Main'
Threads, I heard thing about this during video tutorials like thread pool, single thread, thread synchronization. What are they and what do they do? I assume windows somehow works with this.
Threading is something you should get into a little later, get the basics of Subs/Functions and Multiple forms down first.
If for example I got 'settings' in my program I have to save those in a file and load it in every time the program is being run?
Use My.Settings, they're saved to a file on exit and loaded from the file on start be default anyways
I assume that a programs written in VB only works on Windows? true/false?
False, though there really isnt support for it other than on Windows.
That are the questions for now. I do not expect any completly written explainations but would be much appreciated. Linking tutorials/other resource would be great.
Thanks in advanceYou've got a long road ahead of you, but welcome to the club!
techgnome
Dec 29th, 2009, 12:39 PM
There's a lot there.... I'll try to answer what I can. First, I'd like to point out that we have a number of FAQ/Tutorial threads here, some of which will answer some of your questions.
http://www.vbforums.com/forumdisplay.php?f=73
Anyway I recently started with VB2008, there seems to be a 2010 should I switch while I am still at the very beginning?
since you are just starting out, I wouldn't use VB2010 just yet. It's still in beta, so there are some quirks and problems with it. VB2008 should suit you just fine.
First of all Subs. This seem to be some sort of function but its not. What is it exactly?
Sub is a function that in short doesn't return anything. It's the same as a void function in C.
Global variables, I have read some tutorials but they refer to a tab in VB2008 that says '(general)' and '(Declaration)' however nothing changes withing VB2008 on selecting those.
This is something that is unique to the VB Editor. The one on the left "(general)" allows you to select objects (usually the ones on the form) and the one on the right allows your to select an event of the object (if you selected an object on the left drop down)... or a sub/function (if you select "(general)") ... The General selection option tells the IDE to look in the "general" section of the code.... When you select "Declaration" that takes you to the "general" "declaration" part of the file... usually all the way at the top, where your general declarations go (public/private variables, constants, etc.)
Modules, What are those? on first sight they seem to be sort of code structure to be called on later.
In short, yes. Modules are basically shared classes. Generally used for global declarations.
Namespacing, Never really had to worry about those. It seems to be a sort of easy way to level your libary. Like I saw 'Me.system' and Me is the name of the namespace and it includes 'system' to your current page/program(sort of include?)
It's a way of logically organizing your classes. For instance, the Me namespace. Several of the namespaces and classes found under "Me" reside in several different assemblies. But because they all reside in the Me namespace, they take on the appearance of being grouped together. You can even then extend the namespace and add your own class to it. Add a new class file to a project, at the top, put "Namespace Me" (don't forget the end namespace at the end) ... then put in some public functions in the class1 shell.... then from in your app, you should be able to type "Me." and at the "." get "Class1" to show up.
How do you know where the program is running from? For example the program is running from C:\myprogam\ how do i know this so I could load files from there? like images/text/whatever.
Application.StartupExe (or something along those lines). It's in the Application class.
Multipule forms, Lets say I start form1, like the default naming, and I want on a button click to open an other form, form2, how can I interact with both of them? Like a page of 'settings' form1 has to adjust to that. Pass By Referrence comes to mind.
This isn't simple, since a lot depends on what it is you are trying to accomplish. But there are a number of threads running around on how to get two forms to communicate with each other.
When I click properties on my project in the Solution explorer I get a view tabs. What are the following: Resources, Service, settings(refers to my question above???), Reference?
Resources are embedded resources... they can be images, files, strings, a number of things. It's a way of having related data (for example images) with out having to include the actual images as separate files. All you need to do is extract them and use them.
Settings are stored in the config file. Application settings are readonly to the end user and can't be changed w/o editing the config file. User settings get stored in a user.config file and are read/write. It allows to you customize the app per user.
What is the 'main' for my program is it form1? and is that also the global scope?
It depends. By default, yes. But that can also be changed (by changing some of the settings in the Project Options.
If for example I got 'settings' in my program I have to save those in a file and load it in every time the program is being run?
If you use the Settings from the Project Options.... yes and know. You don't have to explicitly do anything to load them, they are automatically loaded when the application starts. Saving them.... there's an option to make it autosave when the app shuts down, but I tend to go ahead and explicitly issue a My.Settings.Save when ever the user changes an option and hits the OK/Save button.
I assume that a programs written in VB only works on Windows? true/false?
Sort of... there's a project called MONO which can take .NET projects and run them on Linux. as long as you don't use anything that is Windows specific, and target framework 2.0 (I don't know which latest version of the FW Mono supports) then you can use Mono to compile and run the application on Linux.
Hope that helps.
-tg
Blixa
Dec 29th, 2009, 02:03 PM
wauw thanks for those amazing answer, never expected it.
Going to try to write my first program and will get back if I got more questions.
Thanks both of you for those awesome answers!
Shaggy Hiker
Dec 30th, 2009, 07:36 PM
Those answers covered much of it, so here's a little supporting information.
A thread about passing things between forms:
http://www.vbforums.com/showthread.php?t=466253&highlight=Definitive
An item to watch out for:
A form is just a class (an object), and it is like any other class. However, ever since VB2005, forms had one added feature: The default instance. For any other class, you have to actually create an instance of the class with the New keyword (in some cases, a method will return an instance of a class, such as the CreateCommand method of a database connection object, which creates a new Command Object). Unfortunately, all forms have a default instance that is created automatically in the background, and which have the same name as the class. Therefore, if you have a Form1 in your project, that is the name of a class, but there is also an instance called Form1 automatically added to your project. Therefore, you can either do something like this:
Dim nf as New Form1
nf.SomeProperty = something
or you can do this:
Form1.SomeProperty = something
In the first case, you are creating a new instance of Form1 and calling it nf, while in the second case you are working with the default instance of Form1. Technically, there is nothing wrong with using the default instance. It is a valid instance of the form class, just like any other instance. However, because the instance has the same name as the class (which is just a category, not a specific object), people often get confused. They don't realize that an actual instance was created for them in secret. For this reason, I thoroughly despise the default instances, and always prefer to explicitly create a new form every time I want to show one (also I started with .NET prior to 2005 when the default instance didn't exist).
Threads:
Threading is certainly an advanced subject, but it is also one that you should definitely be aware of. Every process runs on a thread. Normally, you just have a single thread, which is known as the User Interface, or UI, thread. However, suppose you have some task that takes a long time to complete. If the UI thread runs that task, it will block all other messages from being processed, which means that buttons won't click, scroll bars won't scroll, screens won't paint themselves, and so on. The program will appear to be frozen until that long running process finishes. Not good. Multi-threading is a way to have multiple processes running simultaneoulsy. The processor will allocate some tiny slice of time to one process, then to the next, and so forth. If you only have a single core, and all processes take ALL the time they can have, then you would see little benefit from multithreading. However, in the earlier example, there is one process that is long running, and it is blocking the UI from processing input and other events. By moving the long running process onto a background thread, the UI thread is free to respond to events, and that is all that it needs to do. Consider that even for a fast typist, the rate of keystrokes is only a few per second. A computer can perform millions of computations between keystrokes, so why make it sit and wait if it has something else to do? By moving the long running process into a background thread, the computer can switch to working on that process in those lengthy pauses between keystrokes, and it will appear to be doing two things at once. Better yet, with the rise of multi-core processors, each processor can run a thread (or multiple threads) at the same time. In such a case, you can have two long running processes running at the same time and running at full speed. The OS will divvy up the processor cores to the running processes without you doing anything. The only thing you need to do would be to set up your long running processes in their own threads, and the OS will do the rest, whether it has one core to work with, or many.
However, there are some real drawbacks to multithreading, as well. For one thing, an object is not specific to a single thread. This can mean that two different threads will alter the object, and you can never know when it will happen. For instance, if two threads both executed the statement:
a = a + 1
If thread X ran this and checked the value of A before and after this statement, then A could be 1 higher, or two higher, because the value of A would first be loaded into a register, then incremented. If A was loaded, then the processor switched to the other thread that loaded A and added 1, then switched back, thread X would add 1 to the original value, and all would be well....but nearly anything else could happen, too. For instance it could check A, then the other thread could increment A, then the first thread could increment A, and A would appear to have increased by two. Therefore, if multiple threads write to the same object, then you have a race condition. You can't predict which thread will get there first. Sometimes this doesn't matter, other times it becomes the most maddening of bugs, because it will be intermittent. To solve that, you have to make sure that reads and writes between different threads are synchronized, which can lead to deadlocks if not done properly, but I won't go into that. The point is that multithreading can be very useful, but it can also be very tricky.
Blixa
Jan 2nd, 2010, 09:28 PM
Thanks for the explaination of threading I think I understand the basic.
However I got a new question.
I am trying to make a sort of ping pong program between a server and a client. (1 client at a time for now, multiple will come later).
However the server seems to be listening but the client cant seem to connect and I cant figure out why.
Private Sub connectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectButton.Click
If Not ipText.Text = "" Then
sock.RemoteHost = ipText.Text
sock.RemotePort = portText.Text
sock.Connect()
End If
End Sub
server listening code
Private Sub startServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startServer.Click
If Not portText.Text = "" Then
sock.LocalPort = portText.Text
sock.Listen()
startServer.Enabled = False
If sock.CtlState = MSWinsockLib.StateConstants.sckListening Then
writeLog("listening...")
End If
End If
End Sub
Public Sub sock_connectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles sock.ConnectionRequest
If sock.CtlState <> MSWinsockLib.StateConstants.sckConnectedThen
sock.Accept(e.requestID)
End If
End Sub
tried: sckconnected,listening
but when I check the status with: sock.CtlState = MSWinsockLib.StateConstants.sckClosing
its true, I don't seem to get any errors or anything.
the IP is my localIP (192.168.x.xxx) and port is 28920(just random).
sock is the winSock object.
When I press connect in the GUI it goes without problems. when I check the status it says its closing.
anyone have an idea what the problem might me? Would pass on more code but it doesn't seem to be relevant. Is there someway I can find out why its closing?
thanks in advance
Nightwalker83
Jan 3rd, 2010, 03:38 AM
I'm not sure if it would help but checkout the client/server example in my sig.
Blixa
Jan 3rd, 2010, 06:40 AM
I checked your signature and I dont see any big differents (except its vb6).
the sock.CtlState returns 6
http://msdn.microsoft.com/en-us/library/aa228160%28VS.60%29.aspx
I am wondering what state 8 "Peer is closing the connection" means.
is the server closing it or the client?
update:
I tried to turn all my networks off and tried it at 127.0.0.1 (dont know why just be sure I guess)
It however results directly in a state 6 and a sec later in state 8.
When I try to connect on something that does not exist I get an error tho 'Connection is forcefully rejected'
Is the connectionRequest event correct? I think that the problem is there but not sure.
anyone sees the problem?
Nightwalker83
Jan 3rd, 2010, 08:39 PM
I checked your signature and I dont see any big differents (except its vb6).
Yeah, since I haven't used vb.net I don't know what the differences are with the code.
JuggaloBrotha
Jan 3rd, 2010, 09:56 PM
Yeah, since I haven't used vb.net I don't know what the differences are with the code.Well for one: winsock isn't used in .Net, I think it can be used but .Net has much more up to date networking classes already available in the FW
Nightwalker83
Jan 4th, 2010, 01:55 AM
Well for one: winsock isn't used in .Net, I think it can be used but .Net has much more up to date networking classes already available in the FW
I guess I will fine that out when I go back to school and start my programming course, one of my subjects is VB.NET.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.