Archive for Software Development

Visual Basic: What’s a subroutine / Difference between Subroutine and Function?

Subroutines are another word for methods, and they’re code blocks that will be executed by your application as you call them elsewhere in your applications. To call a subroutine, just call it by the subroutine’s name (for instance, if you created your own subroutines).

The Public, Private and Protected keywords are incase you create multiple classes (explained below) and you want to choose whether other classes have access to use the respective methods (or subroutines) you have created. You can search online for the exact definition of these three keywords.

Say you have a subroutine that does some task of some kind, you call it and the code within that subroutine gets executed. However, the thing is, what if you wanted to make sure some code in the subroutine did it’s job? What about using the Return keyword and return either true or false depending on whether the portions of the code block was executed? You may want to do this at some point in your Visual Basic development. Either way, subroutines cannot return values – they’re there purely to execute code. If you wanted to do this, you’d need to use a Function in Visual Basic. It’s like a Subroutine, except at some point you have to return a value of some form, like: Return TRUE etc.

Unlike Subroutines, with Functions you have to specify the data type of the Function (in other words, what sort of data type you’re returning. Here’s a basic example:

Public Function returnTrue() As String
Return "Executed"
End Function

The brackets are required there incase you may want to take in arguments as part of when some other part of the application call’s the function. For example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
returnTrue("Hello world")
End Sub
Public Function returnTrue(ByVal n) As String
Return n
End Function

Ignore ByVal for now, you can learn that later – it’s just telling Visual Basic how to handle the argument (the variable in the brackets). You can always search online about the ByVal keyword. Now, all this does is returns whatever you enter when calling the Function. If you forget to add ByVal when creating a Function or Subroutine, Visual Studio or Visual Basic Express will do it for you – incase you forget. You can also specify arguments (brackets) for Subroutines too. The only difference is Subroutines cannot return values, hence why Functions require an As Type keyword after them.

What are classes and methods?

Methods are the code blocks that are programming functionality or logic (in other words, the code in a method code block (subroutine) does something – therefore it is programming functionality). In VB, all programming functionality is wrapped within a method (or what is called a subroutine or function). Classes are simply structures for all the methods you create (or VB create for you in the application), and is there purely for organisation of multiple methods. All Visual Basic applications and code must reside within a class and method of some kind – such as a subroutine or function. A lot of other programming languages have these sorts of features (classes, methods) and some also require that all code also be within a class and method. By the way, this method of coding (classes/methods/etc) is called object oriented programming. It’s a complicated word and you don’t need to worry about what the word means for now, but generally object oriented programming is a programming paradigm (way of programming) in languages like Visual Basic, Java, C++, etc.

Related Posts:

  • No Related Posts

How to dynamically resize controls in Code (Visual Basic)

Novices to Visual Basic may appreciate that you can visually build your application via the Visual Studio (or Visual Basic Express) interface, but what about when you create applications where you want the controls to dynamically resize to fit the size of the window. First, we need to logically think how this will be done:

  • First, we need to set the initial dynamic size as the application is loaded.
  • Then,  handle an event where when the window is resized we further dynamically resize the controls to fit the new resized size.

And that’s all there is to it! Let’s get started

01vb 01 300x299

Here’s an example application I’m creating – a simple Web browser. I’ve added two controls on the form, and changed the Location property for each control to be in the top left corner in order for the resizing to work correctly. The first is a textbox and the second being a WebBrowser control.

For my application I’m going to set an event against the TextBox to see when the Enter key has been pressed. To do this I go to the Events section of the Properties window (the lightning bolt) and scroll until I find KeyPress and add the code that detects when the Enter key has been pressed:

Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If AscW(e.KeyChar) = 13 Then
WebBrowser1.Navigate(TextBox1.Text)
End If
End Sub
End Class

I’ll explain what’s happening here. The AscW function returns an Integer representation of a character – such as the Enter key, A, Q, etc – each key on a keyboard has such an integer value – called an ASCII value, technically. We simply compare the key entered to the ASCII value to see if the Enter key was pressed. However, in order to compare it to an integer value we first need to get an integer representation of the key entered, as if we just did e.KeyChar = 13 it would be comparing a char to an integer – and that’s going to result in an error. The e variable is declared through the subroutines argument (if you look carefully) against the KeyPressEventArgs class – and this has the KeyChar property which holds the character entered.

We then set the Navigate subroutine to TextBox1.Text, and of course, the Navigate subroutine is responsible for navigating the WebBrowsr control to a new website. There’s also the Url property that does the exact same thing but with a slightly different implementation since it is set as a “data type” of System.Uri while Navigate is of data type String, hence we use that for simplicity.

Now let’s get going to resize the controls as the form (application/window) is first loaded. So go back to the form and double click the title bar of the form window to get the Load event code up.

In the event subroutine, we’ll enter the following:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Size = Me.ClientSize
WebBrowser1.Height = WebBrowser1.Height - TextBox1.Height
TextBox1.Width = Me.ClientSize.Width
End Sub

So what’s happening here? The Size property of the WebBrowser class (“WebBrowser1″) allows us to dynamically set the size of both the width and height at the same time, and then we assign it to Me.ClientSize that is the size of the client area or what I tend to call, the content area.

However, it doesn’t take into account any other controls on the form, so we need to subtract the height of the TextBox from the WebBrowser control.

Finally, we change the width of the TextBox too.

Now, we need to set the dynamic size as the form is resized, as right now when we resize the form the controls stay in the same static position. All we do is get the Resize event in the Events section of the Properties window against the Form, and add the same code we just added to it – and we’re done!

Related Posts:

  • No Related Posts

Scripting Languages: What are they?

Scripting languages are a type of programming language that controls a specific software application. One example is JavaScript, that controls slight behaviour of a Web browser. For example, JavaScript in a particular fashion is an event-driven scripting language, so in this way, upon an event taking place, JavaScript “springs into action”. One JavaScript code could wait for an event when the user tries to close the tab or browser, and a message box will appear upon the event happening. As such, JavaScript is a client-side scripting language.

What is a client-side scripting language?

Client-side scripting languages are like any form of scripting language but the browser in some form is the interpreter. As such, a server-side scripting language would be a different type of scripting language genre, but in this case the interpreter is a virtual machine inside a server. In this case, the server is a physical machine that has the virtual interpreter installed on it. Basically, a virtual interpreter reads the scripting code, and translates it to HTML code of that specific result, which the browser can understand, and the same pattern would happen with the browser (HTML code -> bytecode -> screen). In this case, the screen part is where the text is displayed on the users screen.

Why have scripting languages?

Unfortunately computers aren’t like humans, and as a result can’t understand human speech commands that we say. For example, in a command-line interface, if we wanted the computer to open a specific file, we couldn’t just say “open notepad.exe” – the computer would just respond and say “unknown command”. The reason there are scripting languages are for a computer to accomplish a specific task. There are many variety of scripting languages out there, and different scripting languages are for different tasks. For example, JavaScript is a client-side scripting language and mainly an event-driven scripting language, and hence, it usually waits for events to happen. JavaScript is commonly known to be used for pop-ups, and in use with web-forms for validation purposes. Another good purpose of JavaScript is that it is capable of changing images as the mouse moves over them. Much like desktop programming languages which are mostly event-driven as well.

There are other scripting languages that are used for general-purpose activities, for example creating dynamic Web pages to create interactivity to the visitor. In such a sense, we mean that, for example, a page could display two different things. If the user is using Firefox, some text with “You are using Firefox” would be displayed, conversely, if the user is using Internet Explorer, some text would display “You are using Internet Explorer” instead. This is one form of dynamic Web pages, but of course many scripting languages could do things at a much higher-level scale than just this. Another example is creating a text-based game, completely coded in a scripting language.

Just for your information, there is a distinct differentiation between scripting languages and good old HTML. HTML alone cannot create dynamic Web pages, as such some of the examples explained in the preceding sections. HTML was created for the building of Web pages, and as a result, it is a static language, and is the structuring of Web pages (and in some cases the styling of Web pages, too).

How do I learn a scripting language?

First of all you need to understand whatever scripting language you want to go for and what exactly it does. If you want to go for client-side scripting languages, JavaScript may be a good option. And in another criteria, for server-side scripting, PHP may be a good option, as a general-purpose scripting language. Nonetheless, whatever scripting language you go for, you need to look into what exactly it is, and understand the concepts of it first. If you want more information on PHP, you can pop on over to PHP’s official Website, PHP.net and JavaScript resources and tutorials can be found with a little searching with Google.com.

Good luck!

Related Posts:

  • No Related Posts

Creating an RSS Feed

RSS feeds are xml files updated on a regular basis which people subscribe to within RSS readers. The information carried on them is normally up to date and they are normally found on news sites and blogs, basically sites which are updated regularly and which people might like to be up to date with. In this article I will go through creating an RSS feed; they can be created in two forms: dynamic – this is where dynamic server side pages are created to query databases to display the new information automatically and require no manual intervention for the data to be displayed; manual – this is where a basic xml file is created and which has to be updated manually.

The Basic RSS Format

RSS feeds are made up of several different elements and tags which create the final document. The following is an example of an RSS feed:

As you can see the XML version used is the first thing to be declared within the document, the second being the beginning of the RSS tag which means the beginning of the feed; including both of these within your feed is important since a browser will use these to work out that it is an XML file which contains an RSS feed. Click here for an example of an RSS 2.0 feed.
The feed itself is contained within a ‘channel’ tag – some browsers output RSS feeds into a styled, readable format and this tag is important to ensure that browsers which have this capability are able to render your feed in this way. The first few lines within this tag describe your feed and contain information such as the title of the feed, a description of it, the language in which it is written as well as the last time it was updated.

After that the different items of the feed are shown – these are displayed within ‘item’ tags and there is no limit on the amount of these which you can have in one feed. The basics needed to structure an ‘item’ tag are the ‘title’ and ‘description’ tags which ensure that the item is outputted into a readable format.

Dynamic RSS Feeds

Feeds of this type require a server side page such as a PHP or ASP page be created to query a database so that the up to date information be displayed. This process requires no manual intervention and is completely automatic. To begin with you need to make sure that your page is outputted to the browser as an XML document, to do this you will need to add a header response line to the top of your page, and make sure that it outputs the page in the ‘text/xml’ format. The rest of the feed is outputted like the example above, except
Manual RSS Feeds

These feeds are ‘.xml’ files which are updated manually and always take the form of the feed displayed within the first section of this article. You should only opt for this type of feed if you aren’t going to update often as updating it can be a timely procedure.

Related Posts:

  • No Related Posts

Future of On Demand Service

On demand service is very young on this stage but I am sure it will take a big place in forthcoming business environment as today’s business environment is fast-moving and become notorious and unpredictable. On demand integration and services can give the organization the flexibility to dart obstacles and grab opportunity without losing rapidity and competence.

On demand service will open a multi-billion dollar market in near future. The best example is Google’s contextual advertising/ YPN that has on-demand service — buyers go online (or connect to an API) to place it and manage it, Google’s service is entirely virtual, they have nice portal system and highly customizable and scalable applications which is entirely of its own creation. In today’s challenging marketplace, it is essential for an organizations to streamline business processes and respond quickly to changes as what is today may not going to be suffice for future.

You need to spend time on MIS/market forecast and to create value-added functions, key applications, core competencies and create a role-based portal which can create on demand service that can connect content to applications to business process to people.

-Bob

Related Posts:

  • No Related Posts
« Previous entries Next Page » Next Page »