0

This is a code from a program in visual basic and I was wondering if there was a way to simplify it?

It takes the number of strings and says, if you equal this number then show this letter and this cover.

Is there a simpler way to do this so I don't have to repeat the code over and over again?

If UBound(Variables.Words) = "0" Then Round1.Letter6.Show() Round1.Cover6.Show() ElseIf UBound(Variables.Words) = "1" Then Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() ElseIf UBound(Variables.Words) = "2" Then Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() ElseIf UBound(Variables.Words) = "3" Then Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() ElseIf UBound(Variables.Words) = "4" Then Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() ElseIf UBound(Variables.Words) = "5" Then Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() ElseIf UBound(Variables.Words) = "6" Then Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() ElseIf UBound(Variables.Words) = "7" Then Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() Round1.Letter10.Show() Round1.Cover10.Show() ElseIf UBound(Variables.Words) = "8" Then Round1.Letter2.Show() Round1.Cover2.Show() Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() Round1.Letter10.Show() Round1.Cover10.Show() ElseIf UBound(Variables.Words) = "9" Then Round1.Letter2.Show() Round1.Cover2.Show() Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() Round1.Letter10.Show() Round1.Cover10.Show() Round1.Letter11.Show() Round1.Cover11.Show() ElseIf UBound(Variables.Words) = "10" Then Round1.Letter1.Show() Round1.Cover1.Show() Round1.Letter2.Show() Round1.Cover2.Show() Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() Round1.Letter10.Show() Round1.Cover10.Show() Round1.Letter11.Show() Round1.Cover11.Show() ElseIf UBound(Variables.Words) = "11" Then Round1.Letter1.Show() Round1.Cover1.Show() Round1.Letter2.Show() Round1.Cover2.Show() Round1.Letter3.Show() Round1.Cover3.Show() Round1.Letter4.Show() Round1.Cover4.Show() Round1.Letter5.Show() Round1.Cover5.Show() Round1.Letter6.Show() Round1.Cover6.Show() Round1.Letter7.Show() Round1.Cover7.Show() Round1.Letter8.Show() Round1.Cover8.Show() Round1.Letter9.Show() Round1.Cover9.Show() Round1.Letter10.Show() Round1.Cover10.Show() Round1.Letter11.Show() Round1.Cover11.Show() Round1.Letter12.Show() Round1.Cover12.Show() End If 

Any help is much appreciated!

Thanks, Dan.

EDIT ** I am not new to this but I only know basic functions. Sorry to be difficult, but could you show me full examples of what i would need to do.

Thanks, again.

3
  • you can simplify it it use switch...case instead of if....else Commented Feb 27, 2014 at 17:15
  • No, he can´t. @Searle95: If something >= anotherthing then show... This way, you´ll have only two statements in each if-part, instead of 2,4,6,8... Commented Feb 27, 2014 at 17:19
  • 1
    why 'he can't @deviantfan? see the code it always check for = how you can set a range comparison >=? because in the code its start checking from 0 to 11 and checks for every number. How you can set >=? Commented Feb 27, 2014 at 17:28

3 Answers 3

1

Could you refactor the letters and covers so that they are in an array or collection , and then assuming they are always consecutive, write a procedure passing start and end params along these lines:

dim i as integer = Ubound(words) select case i case 0 showlettersandcovers(6,6) case 1 showlettersandcovers(6,7) . . . case 3 showlettersandcovers(5,8) . . . 

(added in response to comment)

sub ShowLettersAndcovers(startnum as integer, endnum as integer) for i as integer = startnum to endnum Round1.Letter(i).show Round1.Cover(i).show next 

That's assuming you can get your letters and covers into an array or similar - without knowing the details of your application, I've no idea if this is a reasonable assumption or not. For instance, you might be able to set your letters up like this:

Dim letterarray as letter() with {new letter(paramsforletter0), new letter(paramsforletter1 . . . } 

Or you might be loading them from a database, so they might already be in a dataset which you could iterate over as above. . .

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to be difficult Peter, please could you give me a full example?
0

Another option to reduce repetition slightly would be to group calls into functions since it seems like the letter and the cover are always shown together.

Private Function ShowSection6() Round1.Letter6.Show() Round1.Cover6.Show() End Sub Private Function ShowSection7() Round1.Letter7.Show() Round1.Cover7.Show() End Sub '.... etc ... 

Then group things in a Select statement as mentioned by @Westie since you only have a single outcome each time

 Select Case UBound(Variables.Words) Case 0 ShowSection6() Case 1 ShowSection6() ShowSection7() Case 2 ... Case Else ... End Select 

1 Comment

Thanks Brent, I didn't think of this, this would definitely help!
0

Building on Peter G's idea:

You can avoid the situation where you have to name so many variables so specifically to be able to control them by creating them dynamically in the LoadItemLookup method shown below, but doing it the way you've done it you can use a lookup like this:

' Lookup of Cover/Letter objects Private _ItemLookup as Dictionary(of Integer, YourObjectType) Private ReadOnly Property ItemLookup As Dictionary(of Integer, YourObjectType) Get If IsNothing(_itemLookup) Then _ItemLookup = LoadItemLookup End If Return _ItemLookup End Get End Property ' Loads the lookup Private function LoadItemLookup as Dictionary(of Integer, YourObjectType) dim newLookup As New Dictionary(of Integer, YourObjectType) newLookup.Add(1, Round1.Letter1) newLookup.Add(2, Round1.Letter2) newLookup.Add(3, Round1.Letter3) 'etc Return newLookup End Sub Private Sub YourExecutingSub Select Case UBound(Variables.Words) Case "0" ShowItems(6, 6) Case "1" ShowItems(6, 7) Case "2" ShowItems(5, 7) Case "3" ShowItems(5, 8) Case "4" ShowItems(4, 8) End Select End Sub ' taken from peter g's answer Private Sub ShowItems(ByVal startNumber as Integer, byval EndNumber as Integer) For i As Integer = StartNumber to EndNUmber If ItemLookup.ContainsKey(i) Then ItemLookup(i).Letter.Show ItemLookup(i).Cover.Show End If Next End Sub 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.