How to trim duplicate code in a method

I have a method which executes in two ways, changing only a few ops in-between those two. Is there a way to turn that code into a repeatable subroutine? I tried functions but the IDE throws a fit. All of the repeteable code acquires one input and return one output.

Help?

This depends on the length of your code. If there are only a few lines then a couple of

if condition then
do something
else
do something else
end if

is needed.

The longer and the more complicated your code gets the uglier this looks. You can then refactor to using methods for the code in the if and else. You could even use classes instead of the methods.

I use all three methods depending on complexity. This is the beginning of using OO :slight_smile:

HTH

[quote=180646:@Beatrix Willius]This depends on the length of your code. If there are only a few lines then a couple of

if condition then
do something
else
do something else
end if

is needed.

The longer and the more complicated your code gets the uglier this looks. You can then refactor to using methods for the code in the if and else. You could even use classes instead of the methods.

I use all three methods depending on complexity. This is the beginning of using OO :slight_smile:

HTH[/quote]

This is a code sample of what i need to fit into the subroutine

[code]
Datetime = dbarray(1).split(" “)
DateArray = Datetime(0).split(”/")
TimeArray = Datetime(1).split(":")

        Filedate.Day = Val(DateArray(2))
        Filedate.Month = Val(DateArray(1))
        Filedate.Year = Val(DateArray(0))
        Filedate.Hour = Val(TimeArray(0))
        Filedate.Minute = Val(TimeArray(1))
        Filedate.Second = Val(TimeArray(2))
        [/code]

I provide datetime to that routine and filedate comes out

Then you need to add parameters and a return value to your method.

What syntax i should use to fit that into my existing method?

Try something like that:

dim FileDate as Date = calcDate(dbarray(1))

And your original code goes into the method.

I refactored most of the code to remove as much code duplication, turned some method-wise variables into shared ones and created a second method with the core routine. Trimmed quite a lot.

Thanks for the help!