PDA

View Full Version : Passing a UDT member/element



KingofKong
06-11-2009, 10:24 AM
Hello! I am trying to pass a member (element) of a UDT and running into difficulties. i can pass a UDT array, but im not able to specify which piece of the UDT i want the sub to focus on. im sure its in my declaration. im declaring it as a string which is probably the problem.


Type SpreadData
name As String
time As Date
l As Double
b As Double
a As Double
End Type

Public NIY(100) As SpreadData

Sub Start()
Dim i As Integer
Dim element As String

i = 0
NIY(i).name = "NIY"
NIY(i).time = Now
NIY(i).l = 10055
NIY(i).a = 10050
NIY(i).b = 10045

element = "b" 'element (type.member) needs to be dynamic between l, b, and a
Call f(NIY, num, element)
End Sub

Sub f(ByRef NIY() As SpreadData, i As Integer, element As String)
Worksheets("Sheet1").Rows(1).Columns("A") = NIY(i).&"part" 'the problem for me is passing the element
End Sub


Thank you so much for your help!

Happy coding

p45cal
06-11-2009, 05:56 PM
perhaps if you set num to something before executing this line?:
Call f(NIY, num, element)
I replaced it with i below and put a Select Case in the functionSub Start()
Dim i As Integer
Dim element As String

i = 0
NIY(i).name = "NIY"
NIY(i).time = Now
NIY(i).l = 10055
NIY(i).a = 10050
NIY(i).b = 10045

element = "b" 'element (type.member) needs to be dynamic between l, b, and a
Call f(NIY, i, element)
End Sub

Sub f(ByRef NIY() As SpreadData, i As Integer, element As String)
Select Case element
Case "b": Result = NIY(i).b
Case "l": Result = NIY(i).l
Case "a": Result = NIY(i).a
End Select
Worksheets("Sheet1").Rows(1).Columns("A") = Result
End Sub

KingofKong
06-12-2009, 06:03 AM
Thank you so much my friend!!! the select case works and that was conceptually holding me back but you were so right on the num too.

I still wish there was a way to pass the actual member of the UDT (.a or .b in NIY(0).a) and not have to work around it by passing a string to a select case to define the member but oh well. Does anyone know how to do this?

Thanks again!

p45cal
06-12-2009, 09:02 AM
and why not:Sub Start()
Dim i As Integer
Dim element As String

i = 0
NIY(i).name = "NIY"
NIY(i).time = Now
NIY(i).l = 10055
NIY(i).a = 10050
NIY(i).b = 10045

f (NIY(i).b)
End Sub

Sub f(ZZ)
Worksheets("Sheet1").Rows(1).Columns("A") = ZZ
End Sub

KingofKong
06-12-2009, 09:54 AM
Thanks for your help p45cal! im trying to pass all of the elements in the NIY.b array not just NIY(i).b. It looks like case select is the best bet. Again, thank you so much!