Tuesday, 3 July 2007

Working With A Simple Structure Array In VB.NET

Recently asked in Yahoo! Answers:

In Visual Basic.NET how do you assign a grading scale to an array of structure variables?

I want to assign a grading scale to an array of structure variables. HOW do you assign a range of values, such as 0 to 299, to a variable within a structure? (In Visual Basic.NET)

Example:
‘declare array of structure variables
Dim graGradingScale(4) As Grades
Dim intSearchfor As Integer
Dim intX As Integer = 0

‘assign grading scale to the array
graGradingScale(0).intPoints = 0 to 299
graGradingScale(0).strGrades = “F”

The part that needs to be changed is “0 to 299″ . How do you assign this range of values to the variable, so that if the user enters a 7, for example, the grade that will match that number will be “F”? I have the rest of the code done correctly, except this one part.

Here is the Grades structure:

Structure Grades
Public intPoints As Integer
Public strGrades As String
End Structure

As I understand the question, what this questioner wants is to create a five-cell array — each cell corresponding to a letter grade of A, B, C, D or F — and assign both the letter grade and the range of numbers needed to obtain that grade into a Structure (struct, for you C-language folks).

Then, she wants to be able to run an input value through the array, to see what letter grade the person should get.

Well, that’s pretty straightforward, but we need to make a couple minor modifications to the questioner’s methodology to make this work as desired. So let’s get to it.

Continue reading: Working With A Simple Structure Array In VB.NET »

Saturday, 12 May 2007

The Value Of Relational Databases: A Case Study

Recently asked on Yahoo! Answers:

SQL / Access query?

I have a 1-table client database which, as well as all the usual name / address info, includes client ‘˜visit’™ fields. Each client may have between 1 and 5 visits per week (Mon to Fri) and each visit can be at a different time, duration and frequency (weekly, fortnightly or monthly) e.g.

Cust XVisit 1 Day1 Hrs1 Time1 Freq1
Visit 2Day2 Hrs2 Time2 Freq2
Visit 3Day3Hrs3 Time3 Freq3etc’¦

Cust YVisit 1 Day1 Hrs1 Time1 Freq1
Visit 2Day2 Hrs2 Time2 Freq2
Visit 3Day3Hrs3 Time3 Freq3etc’¦

I want to generate a report that shows monthly visits – but not details of weekly or fortnightly calls the same customer may have. E.g. If Cust X has 1 weekly visit on a Mon, a fortnightly visit every other week on a Wed – and a visit on the last Fri of each month, I only want to see details of the Fri/monthly visit.

Hope this makes sense!!
Any assistance very much appreciated

My initial answer to the question prompted a reply from the questioner, who noted my suggested SQL query returned all visits for the client, and who also asked about relational databases and expressed some confusion.

I’ll explain why the questioner’s current design is bad, how to make the proper relationships and demonstrate how to best generate queries such as this user wants.

Continue reading: The Value Of Relational Databases: A Case Study »

Friday, 22 December 2006

Objects (Classes) Explained In Very Simple Terms

One of the most common questions on Yahoo! Answers is how classes or objects, or the parts of a class / object, such as properties, methods and events, work.

Here’s a gross simplification of how classes and objects work. Most of this is lifted from a previous blog entry, Interpreting A C# Class Code Structure.

Classes / Objects

A class or object is a way of combining all sorts of data that relates to a single thing in one place, and a way of associating functions with that data. (For most intents and purposes, classes and objects are the same thing. Whenever I use the term class, I also mean object; whenever I use object, I also mean class. More on that in a moment.)

For example, you can think of a human as a class.

In the human class, you are an instance. For example, you might think that God decided to run this code when you were born:

Dim you As New HumanBeing()

What He did was create a variable, named you, and set the value of that variable to be an instance of the HumanBeing object. An instance is the actual use of an object. The object is the mold; the instance is the actual thing you made from the mold.

(Most programmers use the term class when they are speaking about an instance, and object when they are talking in general terms, so that’s the real difference between class and object. However, you can generally use the two terms interchangeably when discussing a specific programming issue.)

Continue reading: Objects (Classes) Explained In Very Simple Terms »

Monday, 18 December 2006

Variable Scope Made Simple

Recently asked on Yahoo! Answers:

What does this statement mean? “Local variables are local to a function or a procedure.”?

What it means, in a nutshell, is if you declare a variable inside a function or procedure, you cannot get the value of that variable from outside that function or procedure.

Continue reading: Variable Scope Made Simple »