xod Posted May 7, 2017 Share Posted May 7, 2017 (edited) How to put repetitive code in a method or subroutine? switch (AmountX) { case 0: { //some code here, eg. gPen.DashStyle = DashStyle.Solid; gPen.Width = 5; //here is the repetitive code e.g g.DrawLine (gPen, a, b, c, d); ... } break; case 1: { //some code here, eg. gPen.DashStyle = DashStyle.Dash; gPen.Width = 6; //here is the repetitive code e.g g.DrawLine (gPen, a, b, c, d); ... } break; Edited May 7, 2017 by xod Quote Link to comment Share on other sites More sharing options...
Zagna Posted May 7, 2017 Share Posted May 7, 2017 Google suggests this. try { switch (AmountX) { case 0: { //some code here, eg. gPen.DashStyle = DashStyle.Solid; gPen.Width = 5; } break; case 1: { //some code here, eg. gPen.DashStyle = DashStyle.Dash; gPen.Width = 6; } break; } } finally { //here is the repetitive code e.g g.DrawLine (gPen, a, b, c, d); } Does this look sane? Quote Link to comment Share on other sites More sharing options...
xod Posted May 7, 2017 Author Share Posted May 7, 2017 Thank you Zagna, but I'm looking for something else. Something like call Subroutine (name) – Return like in assembler. Google suggestion does not work. Quote Link to comment Share on other sites More sharing options...
MJW Posted May 7, 2017 Share Posted May 7, 2017 In that particular case, you could have an array of DashStyes and an array of ints (or an array of a struct containing both). The arrays would be initialized to the proper values, then indexed by AmountX to get the gPen.DashStyle and gPen.Width. gPen.DashStyle = penDashStyle[AmountX]; gPen.Width = penWidth[AmountX]; g.DrawLine (gPen, a, b, c, d); It could, of course, be put in a separate method. 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.