paul_0722
07-21-2008, 11:34 PM
I'm a bit of newbie and I cannot get my head around how to do this...
I have two arrays and one set of code that processes the data in the same way. I'm on the verge of copying the 50+ lines of code twice - once for each set of array data - and I just *know* there is a better way to do this, but not sure what it is...
Just to explain, this is a bus schedule program that returns the time of the next bus. The input arrays are the Monday-Friday schedule (mf) array and the Saturday/Sunday (ss) array. The next bus arrive time code is the same regardless of day but the schedules are quite different.
Here is what I might do (probably wrong):
Dim one(3) as variant
Dim two(3) as variant
Dim a as integer 'this value will come from a user form
Dim b as variant
Dim c as variant
one(1) = "mon"
one(2) = "tue"
one(3) = "wed"
two(1) = "sat"
two(2) = "sun"
two(3) = "wkd"
If a = "mf" then
b = one(1) & " " & one(2)
c = one(1) & " " & one(3)
<<...and insert lots more code using array one here!!>>
End If
If a = "ss" than b = two(b)
b = two(1) & " " & two(2)
c = two(1) & " " & two(3)
<<...and insert lots more code using array two here!!>>
End If
and here is what I want to do without copying the code block twice but the ?? indicate where I run out of ideas..
Dim one(3) as variant
Dim two(3) as variant
Dim a as integer 'this value will come from a user form
Dim b as variant
Dim c as string
one(1) = "one"
one(2) = "two"
one(3) = "three"
two(1) = "Bill"
two(2) = "Sam"
two(3) = "Larry"
If a = "mf" then b = ??
If a = "ss" than b = ??
b = ??(1) & " " & ??(2)
c = ??(1) & " " & ??(3)
<<...and insert lots more code using array one or two as determined by variable a here!!>>
End If
I have an idea that the code block should be a function but have not been brave enough to try that approach yet - is that a good way to go? Any help appreciated...
I have two arrays and one set of code that processes the data in the same way. I'm on the verge of copying the 50+ lines of code twice - once for each set of array data - and I just *know* there is a better way to do this, but not sure what it is...
Just to explain, this is a bus schedule program that returns the time of the next bus. The input arrays are the Monday-Friday schedule (mf) array and the Saturday/Sunday (ss) array. The next bus arrive time code is the same regardless of day but the schedules are quite different.
Here is what I might do (probably wrong):
Dim one(3) as variant
Dim two(3) as variant
Dim a as integer 'this value will come from a user form
Dim b as variant
Dim c as variant
one(1) = "mon"
one(2) = "tue"
one(3) = "wed"
two(1) = "sat"
two(2) = "sun"
two(3) = "wkd"
If a = "mf" then
b = one(1) & " " & one(2)
c = one(1) & " " & one(3)
<<...and insert lots more code using array one here!!>>
End If
If a = "ss" than b = two(b)
b = two(1) & " " & two(2)
c = two(1) & " " & two(3)
<<...and insert lots more code using array two here!!>>
End If
and here is what I want to do without copying the code block twice but the ?? indicate where I run out of ideas..
Dim one(3) as variant
Dim two(3) as variant
Dim a as integer 'this value will come from a user form
Dim b as variant
Dim c as string
one(1) = "one"
one(2) = "two"
one(3) = "three"
two(1) = "Bill"
two(2) = "Sam"
two(3) = "Larry"
If a = "mf" then b = ??
If a = "ss" than b = ??
b = ??(1) & " " & ??(2)
c = ??(1) & " " & ??(3)
<<...and insert lots more code using array one or two as determined by variable a here!!>>
End If
I have an idea that the code block should be a function but have not been brave enough to try that approach yet - is that a good way to go? Any help appreciated...