Cricinfo Live Scores

Sunday, February 24, 2008

Returning more than one value from a function

A function can only have one return value. In Access 2, there were a couple of ways to work around this limitation:
  • Use a parameter to define what you want returned. For example:
    Function MultiMode(iMode As Integer) As String
    Select Case iMode
    Case 1
    MultiMode = "Value for first Option"
    Case 2
    MultiMode = "Value for second Option"
    Case Else
    MultiMode = "Error"
    End Select
    End Function
  • Another alternative was to pass arguments whose only purpose was so the function could alter them:
    Function MultiArgu(i1, i2, i3)
    i1 = "First Return Value"
    i2 = "Second Return Value"
    i3 = "Third Return Value"
    End Function

VBA (Access 95 onwards) allows you to return an entire structure of values. In database terms, this is analogous to returning an entire record rather than a single field. For example, imagine an accounting database that needs to summarize income by the categories Wages, Dividends, and Other. VBA allows you to declare a user-defined type to handle this structure:

    Public Type Income
Wages As Currency
Dividends As Currency
Other As Currency
Total As Currency
End Type

You can now use this structure as the return type for a function. In a real situation, the function would look up your database tables to get the values, but the return values would be assigned like this:

    Function GetIncome() As Income
GetIncome.Wages = 950
GetIncome.Dividends = 570
GetIncome.Other = 52
GetIncome.Total = GetIncome.Wages + GetIncome.Dividends + GetIncome.Other
End Function

To use the function, you could type into the Immediate Window:

    GetIncome().Wages

(Note: the use of "Public" in the Type declaration gives it sufficient scope.)

Programmers with a background in C will instantly recognize the possibilities now that user-defined types can be returned from functions. If you're keen, user-defined types can even be based on other user-defined types.


Collection
Kazi Masudul Alam

Changing Array Size

When you want to declare array in VBA and change the array size
you can go like this.

Dim list1() as String

When you want to first use it, set the size:

Redim List1(10)

When you want to change size and lose the contents

Redim List1(20)

If you want to increase size and retain the contents

Redim Preserve List1(30)

Author
Kazi Masudul Alam

OOA/OOD/OOP

Object-Oriented Analysis

  • It's general, and thus reusable.
  • Information access is enough. Objects that don't need information can't get to it. Objects that do need information can get to it (i.e., either they have it, or they have an instance variable that points to an object that has it.)
  • Responsibility, control, and communication is distributed. One object doesn't do everything. Makes it easier to reuse, easier to develop and manage.
  • Minimize assumptions of language or system. Try to describe the world, not a program.
  • Define objects not functions or managers. Objects are nouns.
  • Don't include things you don't need, even if it is part of the real world. Sure, everything is made up of molecules, but you probably don't need a molecules class.
  • Good hierarchy is always linked by IsA relationships.
  • Attributes and services should be factored out as high in the hierarchy as possible.
  • It should be easy to add on to, and thus reusable.
  • There should be little or no redundancy.
  • Again, objects are NOUNS.

Object-Oriented Design

  • Implementable. You can see how you'd write code from here.
  • Complete. It's obvious that the OOA is right because everything you need to do is (1) covered in the OOD and (2) matches the OOA.
  • Removes unnecessary middlemen. If A needs to reach B, but can only do it through C...think about allowing A to reach B directly.
The idea is that the OOD is where you patch the pieces together, where you make sure that messages are caught, important information is known at the right places, that all the important services are accounted for.

Object-Oriented Programming

  • NEVER change a reused class! Use subclasses.
Collection
Kazi Masudul Alam
Software Engineer