PDA

View Full Version : VB 6.0 --- UDTs as Functions?


FatboyPat
03-07-2005, 08:51 PM
Hi all, I'm trying to get (what would appear to be) a pretty simple program/module to work, but I'm running into some serious issues...

I'm very inexperienced with VB although my limited knowledge also applies to a few other languages so I'm not completely new to computer programming...

My problem is that I have a perfectly functioning Sub, but I want to turn it into a Function that would return a UDT of all the data created/set in the Function...

here's my code:


Public Type grid ' cant get it to work so that newGrid is a function that returns type grid
TILE_WIDTH As Integer
TILE_HEIGHT As Integer
TILE_GRIDWIDTH As Integer
TILE_GRIDHEIGHT As Integer
TILE_OFFSETX As Integer
TILE_OFFSETY As Integer
End Type

Public Sub newGrid(frm As Form, obj As Object, tWidth As Integer, tHeight As Integer, gWidth As Integer, gHeight As Integer, offSetX As Integer, offSetY As Integer)

tWidth = tWidth
tHeight = tHeight

Dim arrayElem As Object

If obj.Count > 1 Then
For i = 1 To (obj.Count - 1)
Set arrayElem = obj(i)
Unload arrayElem
Next i
End If

Set arrayElem = obj(0)

arrayElem.Left = offSetX
arrayElem.Top = offSetY
arrayElem.Width = 50
arrayElem.Width = tWidth
arrayElem.Height = tHeight
arrayElem.Visible = True

For i = 1 To (gWidth * gHeight) - 1
Load obj(i)
Set arrayElem = obj(i)
arrayElem.Left = (i * tWidth) Mod (tWidth * gWidth) + offSetX
arrayElem.Top = Int(i / gWidth) * tHeight + offSetY
arrayElem.Visible = True
Next i


End Sub



The long and short of what this module does, is it creates a "grid" of objects (cmdButtons, Image Boxes, Picture Boxes, etc...) it works pretty good, but because I can't "store" the information anywhere, it doesn't work exactly as I'd like :(

smeagol
03-08-2005, 03:12 AM
Would you just change the Sub to a Function and do it like this:

Public Function newGrid(frm As Form, obj As Object, tWidth As Integer, tHeight As Integer, gWidth As Integer, gHeight As Integer, offSetX As Integer, offSetY As Integer) As grid

Then, somewhere in your code you're going to create the grid object that you want to return. You would then return it by doing this:

newGrid = <whatever grid object here>

And then just end the function (don't forget to change, End Sub to End Function).

Is this what you're talking about?

FatboyPat
03-08-2005, 09:38 PM
Yep that's exactly what I'm talking about... but I get an error:

Compile error:

Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules

Here's the "new" code:

Module:

Public Type grid ' cant get it to work so that newGrid is a function that returns type grid
TILE_WIDTH As Integer
TILE_HEIGHT As Integer
TILE_GRIDWIDTH As Integer
TILE_GRIDHEIGHT As Integer
TILE_OFFSETX As Integer
TILE_OFFSETY As Integer
End Type


Public Function newGrid(frm As Form, obj As Object, tWidth As Integer, tHeight As Integer, gWidth As Integer, gHeight As Integer, offSetX As Integer, offSetY As Integer) As grid

newGrid.TILE_WIDTH = tWidth
newGrid.TILE_HEIGHT = tHeight
newGrid.TILE_GRIDWIDTH = gWidth
newGrid.TILE_GRIDHEIGHT = gHeight
newGrid.TILE_OFFSETX = offSetX
newGrid.TILE_OFFSETY = offSetY

Dim arrayElem As Object

If obj.Count > 1 Then
For i = 1 To (obj.Count - 1)
Set arrayElem = obj(i)
Unload arrayElem
Next i
End If

Set arrayElem = obj(0)

arrayElem.Left = offSetX
arrayElem.Top = offSetY
arrayElem.Width = 50
arrayElem.Width = tWidth
arrayElem.Height = tHeight
arrayElem.Visible = True

For i = 1 To (gWidth * gHeight) - 1
Load obj(i)
Set arrayElem = obj(i)
arrayElem.Left = (i * tWidth) Mod (tWidth * gWidth) + offSetX
arrayElem.Top = Int(i / gWidth) * tHeight + offSetY
arrayElem.Visible = True
Next i

End Function


Form:


Public myGrid, mySecondGrid As grid

Private Sub cmd_Click(Index As Integer)
myGrid = newGrid(Me, cmd, 32, 32, 10, 10, 0, 0)
End Sub

Private Sub cmd2_Click(Index As Integer)
mySecondGrid = newGrid(Me, cmd2, 32, 32, 10, 10, 320, 0)
End Sub

smeagol
03-08-2005, 09:55 PM
Try this:


Public Type grid ' cant get it to work so that newGrid is a function that returns type grid
TILE_WIDTH As Integer
TILE_HEIGHT As Integer
TILE_GRIDWIDTH As Integer
TILE_GRIDHEIGHT As Integer
TILE_OFFSETX As Integer
TILE_OFFSETY As Integer
End Type


Public Function newGrid(frm As Form, obj As Object, tWidth As Integer, tHeight As Integer, gWidth As Integer, gHeight As Integer, offSetX As Integer, offSetY As Integer) As grid

Dim ReturnGrid As grid

ReturnGrid.TILE_WIDTH = tWidth
ReturnGrid.TILE_HEIGHT = tHeight
ReturnGrid.TILE_GRIDWIDTH = gWidth
ReturnGrid.TILE_GRIDHEIGHT = gHeight
ReturnGrid.TILE_OFFSETX = offSetX
ReturnGrid.TILE_OFFSETY = offSetY

Dim arrayElem As Object

If obj.Count > 1 Then
For i = 1 To (obj.Count - 1)
Set arrayElem = obj(i)
Unload arrayElem
Next i
End If

Set arrayElem = obj(0)

arrayElem.Left = offSetX
arrayElem.Top = offSetY
arrayElem.Width = 50
arrayElem.Width = tWidth
arrayElem.Height = tHeight
arrayElem.Visible = True

For i = 1 To (gWidth * gHeight) - 1
Load obj(i)
Set arrayElem = obj(i)
arrayElem.Left = (i * tWidth) Mod (tWidth * gWidth) + offSetX
arrayElem.Top = Int(i / gWidth) * tHeight + offSetY
arrayElem.Visible = True
Next i

newGrid = ReturnGrid

End Function

FatboyPat
03-08-2005, 11:47 PM
Alright looks like it's working... but...

Any idea why this would work:


Dim secondGrid As grid
Dim myGrid As grid

Private Sub cmd_Click(Index As Integer)
myGrid = newGrid(Me, cmd, 32, 32, 3, 3, 0, 0)
End Sub

Private Sub cmd2_Click(Index As Integer)
secondGrid = newGrid(Me, cmd2, 32, 32, 3, 3, 96, 0)
End Sub


... but this won't?


Dim secondGrid, myGrid As grid

Private Sub cmd_Click(Index As Integer)
myGrid = newGrid(Me, cmd, 32, 32, 3, 3, 0, 0)
End Sub

Private Sub cmd2_Click(Index As Integer)
secondGrid = newGrid(Me, cmd2, 32, 32, 3, 3, 96, 0)
End Sub


I guess I missed a footnote somewhere on variable declaration :P

Actually now that I noticed that... my original code worked fine... I just couldn't declare two variables on the same line... weird...

smeagol
03-09-2005, 03:17 PM
What's the error you're getting?