Excel VBA Class Methods

Josh Hazel
07-01-2010, 02:14 AM
I am wondering how I can pass variables to a class method, my obvious solution does not seem to work.

Im trying to use: oMyClass.setUserForm(100,200,300,400)
Receiving a syntax error

My Class:
Public Sub setUserForm(UserFormLeft As Double, UserFormTop As Double, UserFormWidth As Double, UserFormHeight As Double)
pUserFormLeft = UserFormLeft
pUserFormTop = UserFormTop
pUserFormWidth = UserFormWidth
pUserFormWidth = UserFormHeight
End Sub
Public Property Let UserFormLeft(ByVal Value As Double)
pUserFormLeft = Value
End Property
Public Property Get UserFormLeft() As Double
Value = pUserFormLeft
End Property
Public Property Let UserFormTop(ByVal Value As Double)
pUserFormTop = Value
End Property
Public Property Get UserFormTop() As Double
Value = pUserFormTop
End Property
Public Property Let UserFormWidth(ByVal Value As Double)
pUserFormWidth = Value
End Property
Public Property Get UserFormWidth() As Double
Value = pUserFormWidth
End Property
Public Property Let UserFormHeight(ByVal Value As Double)
pUserFormHeight = Value
End Property
Public Property Get UserFormHeight() As Double
Value = pUserFormHeight
End Property

Colin Legg
07-01-2010, 02:21 AM
No brackets -

oMyClass.setUserForm(100,200,300,400)

becomes:

oMyClass.setUserForm 100, 200, 300, 400


Also, presumably the bit in red should be Height, not Width

pUserFormWidth = UserFormHeight

Josh Hazel
07-01-2010, 08:16 PM
Thanks, I will give that a try when I get home, thought i did try that but maybe not

Bob Phillips
07-02-2010, 05:39 AM
Can I ask why you have Let properties, but you also have a function to set them, seems odd to me.

Josh Hazel
07-05-2010, 01:41 PM
Can I ask why you have Let properties, but you also have a function to set them, seems odd to me.

Just for ease of use, i thought it would be convenient to set just a single property, or using the function to set all of them at once.

Bob Phillips
07-05-2010, 02:45 PM
Just seems contrary to object principles to me, and unnecessary.

Josh Hazel
07-05-2010, 03:29 PM
Thank you for asking for addl details. This is my first attempt at a Class Bob, so I appreciate any and all tips/suggestions.
What I figure is that being able to simply use a function like
SetUserform "height", "width", "etc" it saves extra typing and makes it more readable, im a bit ocd about trying to keep code on a single line instead of doing

With olObject
.height =
.width =
.etc =
End with

If there are drawbacks to this though please let me know b4 i get too far into the code!
End With

Bob Phillips
07-05-2010, 03:53 PM
Josh,

LOL! If you want to keep it simple, avoid classes as they are not in that game.

Seriously, classes are good, but there are object principles, which you should adhere to. I know some will call it anal, but if you don't adhere to the object principles you are starting to obfuscating the code, which is what classes should not do.

I accept that initialising the class like this


oMyClass.setUserForm 100,200,300,400


might seem cleaner than


With oMyClass

.Left = 100
.Top = 200
.Width = 300
.Height = 400
End With


is the proper way to do it, they are very separte, very distinct properties. And you do have them all explicitly exposed as class properties.

Bob Phillips
07-05-2010, 03:56 PM
And ... the latter is better documented, you know what properties are being set. You could call the function like


oMyClass.setUserForm Left:=100, Top:=200, Width:=300, Height:=400


but that is still not as clear.

Josh Hazel
07-05-2010, 05:09 PM
I know classes are not simple, but I think a class is the next step for me bettering my coding. I know a lot of basic stuff (though for some reason i cant get a grip on arrays no matter how i try, unless they are the simplest of arrays)
Knowlege of classes too will help me later when i go on to learn other programming languages when I go back to school =p

Bob Phillips
07-06-2010, 12:00 AM
They are very useful IMO too Josh, but again I re-iterate that if you start 'cutting corners' you start to lose the advabtages of abstraction, code re-use and so on that classes give.

BTW, there was a good item on Sunday posted on Daily Dose of Excel about collection classes (http://www.dailydoseofexcel.com/archives/2010/07/04/custom-collection-class/), worth a read.

Josh Hazel
07-06-2010, 12:33 PM
Thanks I will check that out

tragic
07-08-2010, 07:53 AM
did you try "Call oMyClass.setUserForm(100,200,300,400)"?

I also prefer calls with specified parameters (oMyClass.setUserForm Left:=100, Top:=200, Width:=300, Height:=400)

Josh Hazel
07-10-2010, 02:15 PM
Ive forgotten to check, but this has reminded me. And yes, the latter does seem like it would be good for readability, thought the former for keystrokes.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum