|
-
Apr 30th, 2008, 10:49 AM
#1
Messing with UDTs (how to go bananas)
I was trying to use UDTs like below but I think this is too messy for me to handle and I feel there must be some trick to make it easier before I go nuts. Could anyone with good experience in using UDTs give me some advice?
Code:
Type Type2
N As Integer
Z() As Single
End Type
Type Type1
N As Integer
T() As Type2
End Type
Dim T1 As Type1
T1.N = 0
'...
'Later in the code:
T1.N = T1.N +1
Redim T1.T(T1.N - 1)
T1.T(T1.N - 1).N = 0
'...
'Further ahead in the code:
T1.T(T1.N - 1).N = T1.T(T1.N - 1).N + 1
ReDim T1.T(T1.N - 1).Z(T1.T(T1.N - 1).N - 1)
'...
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Apr 30th, 2008, 11:20 AM
#2
Re: Messing with UDTs (how to go bananas)
What kind of advice are you looking for?
What is your goal here?
(PS: I use UDTs ALL the time.....in fact, I can't remember the last application (of any size anyway) in which I did not use a UDT or two)
-
Apr 30th, 2008, 12:07 PM
#3
Re: Messing with UDTs (how to go bananas)
I have to agree with Hack. I normally restrict UDTs to random access file records, but there are times when they are handy for other coding as well.
One thing that might help is to use more descriptive terms or longer words to define your variables within your data types rather than just one or two letters. Also, I keep track of the byte location within the type using a remark statement right after the variable is defined:
Code:
' Practice Test/Section History Data Structure
Type TestRecordType
PracticeTest As String * 1 ' 1 1
Section As String * 1 ' 1 2
Trial As String * 1 ' 1 3
TimedTestStatus As String * 1 ' 1 4
TestDate As String * 10 ' 10 14
CurrentQuesNum As String * 1 ' 1 15
TimeUsed As Single ' 4 19
MCRightAnswer(1 To MaxQnum) As String * 1 ' 50 69
MCUserAnswer(1 To MaxQnum) As String * 1 ' 50 119
MathGridAnswer(1 To MathGrids) As String * 4 ' 80 199
SATScore(1 To 3) As Integer ' 6 205
End Type
The first column is the bytes occupied and the second is the cumulative count. The last number in the table defines the length of the UDT.
-
Apr 30th, 2008, 01:26 PM
#4
Re: Messing with UDTs (how to go bananas)
A udt that goes in nearly all of my projects is UserInfo in which I track various pieces of information particular to the user logged into the machine.
-
Apr 30th, 2008, 04:27 PM
#5
Re: Messing with UDTs (how to go bananas)
Well, the only issue is it seems so easy to get confused, but what the heck, if you guys work with this notation and survive then I'll bite the bullet myself.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Apr 30th, 2008, 04:35 PM
#6
Re: Messing with UDTs (how to go bananas)
I notice that you aren't using Redim Preserve. Is that on purpose?
-
Apr 30th, 2008, 04:41 PM
#7
Re: Messing with UDTs (how to go bananas)
 Originally Posted by krtxmrtz
Well, the only issue is it seems so easy to get confused, but what the heck, if you guys work with this notation and survive then I'll bite the bullet myself.
You might look at it this way. It is a rather nifty way to organize a group of variables that apply to a single entity, such as a user's profile, an event such as a work session trial, or some object that has lots of grouped attributes.
These are characteristics that collectively describe the object or entity, and all that we are doing is clustering them for tracking by using the UDT.
Suppose your object was a baseball player. You can describe him on the basis of batting average, hits, home runs, triples, doubles, singles, walks, RBIs, stolen bases, fielding percentage, etc. These can be collected together as part of the UDT and thus presented as a profile. Note that we have a mixture of variable types--batting average and fielding percentage are single precision, and the rest can be either integers or character strings to save a byte that max out at 256.
If an MLB player, his annual salary these days would have to be long integers if rounded to the nearest buck or perhaps single precision!
-
Apr 30th, 2008, 04:51 PM
#8
Re: Messing with UDTs (how to go bananas)
You can use With blocks:
Code:
With T1
ReDim .T(.N)
.N = .N + 1
With .T(.N - 1)
ReDim .Z(.N)
.N = .N + 1
End With
End With
-
Apr 30th, 2008, 05:12 PM
#9
Re: Messing with UDTs (how to go bananas)
You can also use dummy classes instead of UDTs. eg
Code:
'Class1
Public Var1 As Integer
Public Var2 As String
Public Var3 As Integer
The notation to use them would be the same, but the advantage of classes is that you can pass them around to Subs and Functions more easily that with UDTs.
-
May 1st, 2008, 03:21 AM
#10
Re: Messing with UDTs (how to go bananas)
 Originally Posted by MartinLiss
I notice that you aren't using Redim Preserve. Is that on purpose?
Yes, as it wasn't relevant to the specific issue I was concerned about.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
May 1st, 2008, 03:46 AM
#11
Re: Messing with UDTs (how to go bananas)
 Originally Posted by Logophobic
You can use With blocks:
Code:
With T1
ReDim .T(.N)
.N = .N + 1
With .T(.N - 1)
ReDim .Z(.N)
.N = .N + 1
End With
End With
This is a good point, the code looks more orderly.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
May 1st, 2008, 07:33 AM
#12
Re: Messing with UDTs (how to go bananas)
One of the reasons I use UDTs is to avoid what you are doing. I do not know what any of this represents.
Code:
Type Type2
N As Integer
Z() As Single
End Type
Type Type1
N As Integer
T() As Type2
End Type
What is N supposed to be? What is T() supposed to be? What is Type1 and Type2 supposed to represent?
-
May 1st, 2008, 01:56 PM
#13
Re: Messing with UDTs (how to go bananas)
 Originally Posted by Hack
One of the reasons I use UDTs is to avoid what you are doing. I do not know what any of this represents.
Code:
Type Type2
N As Integer
Z() As Single
End Type
Type Type1
N As Integer
T() As Type2
End Type
What is N supposed to be? What is T() supposed to be? What is Type1 and Type2 supposed to represent?
I made this up for the thread and the actual variables I'm using have more stuff in them.
Type 2 is a curve on a plane defined by N points with coordinates X() and Y() though I used Z() for illustration purposes not to complicate it further. The curve represents a isovalue path for a function f(x,y) and has been calculated from a rectangular matrix where the values of this function are stored.
Type1 represents a set of curves for the same isovalue.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
May 3rd, 2008, 03:31 PM
#14
Re: Messing with UDTs (how to go bananas)
How about saving a UDT to a (text) file, is there a specific statement that takes care of evrything or do I have to issue one output statement for each of the variables that make it up?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|