Jump to content

How to avoid repetitive code in CodeLab


xod

Recommended Posts

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 by xod
Link to comment
Share on other sites

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?

sig.jpg.7f312affa740bae49243c4439bc4a244.jpg

Link to comment
Share on other sites

Thank you Zagna, but I'm looking for something else.
Something like call Subroutine (name)Return like in assembler.
Google suggestion does not work.

Link to comment
Share on other sites

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.

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...