What to do when getting this error: This Method is too Long
Make the method shorter. There is a line count limit for methods.
You probably need to refactor your code and put some parts of the method in other methods.
How many lines does it have ?
Keep your methods short - that works better for debugging too.
For example my code consists usually of lots of method calls like this
[code]ImportFastaFile
CreateProteinListFromFastaFile
For each prot as protein in ProteinList
DigestProtein // creates PeptideList
Next
For each Pep as peptide in PeptideList
ProcessPeptide // sets Peptide properties
Next[/code]
You can also just split the method in two halfs and call the second half at the end if the first.
I once had a long method where I set a lot of values -> too long -> split it in two
There isn’t
BUT there are stack size limits and longer methods use more temporaries & therefore more stack space
So its entirely possible you’ll run into this with really long methods
Often I’ll see really long methods to really, really big Select Case or If statements. In those cases, it’s almost guaranteed that you can make your code, and your life, simpler by encapsulating your code, usually into classes.
My problem is like this
via forfar_simplificada unidad_administracion Instrucciones
BUCAL CHICLE chicle Masticar
BUCAL PULVERIZACION BUCAL pulverizacin Aplicar
BUCAL COMPRIMIDO BUCAL/PARA CHUPAR comprimido Chupar
BUCAL COMPRIMIDO BUCAL/PARA CHUPAR pastilla Chupar
BUCAL COMPRIMIDO BUCODISPERSABLE/LIOTAB comprimido Colocar en la boca hasta que se desintegre
BUCAL GEL/PASTA/LIQUIDO BUCAL aplicacin Aplicar en el rea afectada
BUCAL GEL/PASTA/LIQUIDO BUCAL jeringa precargada Aplicar en el espacio entre la enca y la mejilla el contenido de
BUCAL GEL/PASTA/LIQUIDO BUCAL ml Aplicar en el rea afectada
BUCAL PRODUCTO USO BUCAL TOPICO aplicacin Aplicar en el rea afectada
These are just possible combinations for the via BUCAL. But there are hundreds of possible combinations of via/forfar_simplificada/unidad_adminstracion to get a specific Instrucciones.
So the number of If… Then are too much and I get the error.
Once you make a match, then what? Can you post a portion of your If statement?
Dim Instruccion As String
If PopupMenuVia.Text =“BUCAL” and CampoFormaFarmaceutica.Text = “GEL/PASTA/LIQUIDO BUCAL” and CampoUnidadDosificacion1.Text = “jeringa precargada” Then
Instruccion= “Aplicar en el espacio entre la enca y la mejilla el contenido de "
InstruccionesTextArea.Text = Instruccion + " " + TextField1.Text + " " + CampoUnidadDosificacion1.Text + " cada " + CampoFrecuencia.Text + " " + EtiquetaUnidaddeFrecuencia.Text +” por " + CampoDuracion.Text + " " +EtiquetaUnidaddeDuracion.Text + " va " + PopupMenuVia.Text
End if
If PopupMenuVia.Text =“BUCAL” and CampoFormaFarmaceutica.Text = “GEL/PASTA/LIQUIDO BUCAL” and CampoUnidadDosificacion1.Text = “ml” Then
Instruccion= “Aplicar en el rea afectada”
InstruccionesTextArea.Text = Instruccion + " " + TextField1.Text + " " + CampoUnidadDosificacion1.Text + " cada " + CampoFrecuencia.Text + " " + EtiquetaUnidaddeFrecuencia.Text +" por " + CampoDuracion.Text + " " +EtiquetaUnidaddeDuracion.Text + " va " + PopupMenuVia.Text
End if
Is this part the same in every instance?
InstruccionesTextArea.Text = Instruccion + " " + TextField1.Text + " " + CampoUnidadDosificacion1.Text + " cada " + CampoFrecuencia.Text + " " + EtiquetaUnidaddeFrecuencia.Text +" por " + CampoDuracion.Text + " " +EtiquetaUnidaddeDuracion.Text + " va " + PopupMenuVia.Text
Yes
To recap the problem, you have three controls, PopupMenuVia, CampoFormaFarmaceutica, and CampoUnidadDosificacion1 that each contain a value. You use those values to combine into a “key” that links to an Instruccion, then output a string based on the three values and Instruccion into InstruccionesTextArea, right?
If so, the solution seems simple. Rather than using a long If statement, build a Dictionary first where the keys are the three values and the values are the instructions. They you just need to lookup the instruction from the Dictionary, or react if it isn’t there.
You can do this rather easily by building a constant that looks something like this:
BUCAL GEL/PASTA/LIQUIDO BUCAL jeringa precargada = Aplicar en el espacio entre la enca y la mejilla el contenido de
BUCAL GEL/PASTA/LIQUIDO BUCAL ml = Aplicar en el rea afectada
Create a Dictionary property somewhere and initialize it like this:
LookupDict = new Dictionary
dim lines() as string = kDictValues.Split( EndOfLine )
for each line as string in lines
dim parts() as string = line.Split( "=" )
dim key as string = parts( 0 ).Trim
dim value as string = parts( 1 ).Trim
LookupDict.Value( key ) = value
next
Now when you have to fetch your instruction, it’s simply a matter of looking it up from the dictionary.
dim key as string = PopupMenuVia.Text + " " + CampoFormaFarmaceutica.Text + " " + CampoUnidadDosificacion1.Text
Instruccion = LookupDict.Lookup( key, "NOT FOUND" ) // Or whatever is appropriate
InstruccionesTextArea.Text = Instruccion + " " + TextField1.Text + " " + CampoUnidadDosificacion1.Text + " cada " + CampoFrecuencia.Text + " " + EtiquetaUnidaddeFrecuencia.Text +" por " + CampoDuracion.Text + " " +EtiquetaUnidaddeDuracion.Text + " va " + PopupMenuVia.Text
This should make it easier for you to maintain too.
Anyway, this is just one way to handle it.
Thanks a lot. It seems like I skipped the Dictionary Chapter in the Introduction to Programming Book.
The Dictionary is awesome and will help solve a lot of problems just like this one. Once you look into it, you’ll wonder how you lived without it.
Very nice, Kem. I might have to use this myself
For every methode that could be complex and long you should make a plan first. Make a drawing and identify the functionality you’re going to develop. Once you have a clear view half of the work is done and you will automatically code that way. And as been stated already, encapsulate proven code.