20 times less lines suggestion for manufacturing application

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 941 Views - Last Post: 31 October 2012 - 04:29 PM Rate Topic: -----

#1 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

20 times less lines suggestion for manufacturing application

Posted 29 October 2012 - 12:50 PM

Hello

I made a small application to help decide what materials to use to assemble a certain product

my partners enter the clients' parameters, select which materials we currently have in stock, and the app throws all the possible assemblies that satisfy the request if there's any and gives them priorities according to some other considerations

i tested the calculations and all of them seem reliable

but on second analysis, i have nearly 20 materials that i process the very same way, so i have 20 times the lines i should. i want to compact that by using a method that works for all.

check this out

about passing stuff to the method,
1 material data is stored in global multidimensional arrays (dimensions, weights, prices, accessories needed, assembly time, this and that all so pretty)
2 most variables are global too
3 the calculation process is iterative, so new calculations overwrite previous ones and i abort iterations when i get the result i'm looking for
4 all that changes from the calculations for this material and this other are the data arrays and the display labels (i have those labels because i need to give the user a way to follow up the calculations and justify the selections the app made. those labels are organized on a tableLayoutPanel in the GUI, one per cell. the user must see the calculations made for all the materials to understand why some were discarded or not, and i need to see them for debugging sake)

question 1:
how should i pass the data arrays to such method? by ref and out or just like that? my guess is just as they are is enough because i'm not modifying the data in the arrays, i just need the data they hold...

question 2:
should i pass all the (10) labels as arguments to the method (with ref or out)? or is there some other easier way to display the info?


thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: 20 times less lines suggestion for manufacturing application

#2 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: 20 times less lines suggestion for manufacturing application

Posted 29 October 2012 - 02:41 PM

To question one: If you want to be able to modify the original objects inside the method with out creating a new instance of it then yes use ref. But what you are saying is that you only need the data that they hold so no you will not have to use ref.

To question two: You will not have to pass the labels to the method, you can simply change it's value by simply stating label1.Text = "for example";. You should be able to do that unless it is running on a separate Thread.

Is there some code that I can see to further more help you encase I am misunderstanding you?
Was This Post Helpful? 0
  • +
  • -

#3 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 29 October 2012 - 04:17 PM

thanks for your answer

about question 2, take this as example

assume material1 and material2 are two equally sized multidimentional arrays
to material1 correspond label1a, label1b, label1c, label1d
to material2 correspond label2a, label2b, label2c, label2d
in the GUI results table

so, should i...
myCalc(material1, label1a, label1b, label1c, label1d);
myCalc(material2, label2a, label2b, label2c, label2d);


?

...
i don't feel like that is right. shouldn't the labels have a ref? because i do want to modify the Text propriety
myCalc(material1, ref label1a, ref label1b, ref label1c, ref label1d);
myCalc(material2, ref label2a, ref label2b, ref label2c, ref label2d);





or should i make a string array instead, pass it with a ref, place on it all results of the method, and once outside the method transfer the values one by one to their respective labels?

string[] results = { "-", "-", "-", "-" };

myCalc(material1, ref results);
label1a.Text = results[0];
label1b.Text = results[1];
label1c.Text = results[2];
label1d.Text = results[3];

results = { "-", "-", "-", "-" };

myCalc(material2, ref results);
label2a.Text = results[0];
label2b.Text = results[1];
label2c.Text = results[2];
label2d.Text = results[3];



what do you think?
Was This Post Helpful? 0
  • +
  • -

#4 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: 20 times less lines suggestion for manufacturing application

Posted 29 October 2012 - 04:31 PM

You shouldn't have to pass the label to the method because you can reach them inside, unless this method is in a different class like this:

myclass r;
r.myCalc(material1);


then you would probably pass the ref string array.
If it is not in a separate class than your form or in a separate thread then
you can do this:

btnCalc_Click(blahblah)
{
    myCalc(material1);
}

private void myCalc(material material1)
{
   label1.Text = material1.Weight.ToString();
}



You will have to have a decisional statement in the method to decide what labels to
change rather it be label1a or label2a.
I would best go about this by making a label control arrays
label[0][0]
label[the material number][the material property]

private void myCalc(material material1, int materialnumber)
{
   label[materialnumber][0] = material1.Weight.ToString();
}


This post has been edited by jase kiw: 29 October 2012 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 07:14 AM

about the labels array, i tried but this thing doesn't wants it

if i write
public Label[] somelabel = new Label [10];



it says

Error 1 The type or namespace name 'Label' could not be found (are you missing a using directive or an assembly reference?)

and if i do this

        public System.Windows.Forms.Label[] somelabel = new System.Windows.Forms.Label[10]; //as a global
        //lots of stuff and then
        private System.Windows.Forms.Label MyLabel;




and then, after the form inicialization,


somelabel[0] = MyLabel;




it turns crazy like this

Error 1 The type or namespace name 'Label' could not be found (are you missing a using directive or an assembly reference?)
Error 2 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 3 Invalid token '=' in class, struct, or interface member declaration
Error 4 Invalid token ';' in class, struct, or interface member declaration
Error 5 'BattSelect.BattSelect.somelabel' is a 'field' but is used like a 'type'
Error 6 'BattSelect.BattSelect.MyLabel' is a 'field' but is used like a 'type'



...

i'm gonna try placing this in a different place
Was This Post Helpful? 0
  • +
  • -

#6 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 07:39 AM

well i tried two ways that seemed rather unlikely and just as i expected, they didn't work


        public System.Windows.Forms.Label[] TRLABELS = new System.Windows.Forms.Label[] { 
        {labela1, labelb1, labelc1}};*/

        public string[] labels = new string[] { 
        {labela1.Text, labelb1.Text, labelc1.Text}};




sorry, had to try
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,918
  • Joined: 02-June 10

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 09:10 AM

You seriously need to work on seperating your GUI from your data.
You shouldn't be working directly with the labels or even label.text like that.
This is a OBJECT ORIENTED lanaguage. You want to be working with objects that contain the data.

Separating data from GUI - PLUS - serializing the data to XML


The tutorials below actually teach and explain it further.
[*]Passing values between forms/classes
[*]Bulding an application - Part 1
[*]Building an application - Part 2
[*]Quick and easy custom events
Was This Post Helpful? 0
  • +
  • -

#8 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 11:18 AM

...
oks, dont get me wrong, i do wanna do this right

so, what you are saying is
i should make an object with all the properties i get from the form, and also all the properties i want to display; then i should make its calculation method and volleyball the info from the form to the object to the form?

if that's the way to do it, i'll do it, but it feels like it just complicates stuff, because it implies a freaking lot of lines to write just to create the object and because the object only purpose seems to be holding information and calling a method, when i can just make the form calculate what i want...

in the end, my method wont change, it will just take the data from one material array at a time, use it to make a mess out of some variables, display a result and start again with another material

the only advantage i see in all this is that i be able to update the object properties like delivery time, price, provider, and the likes faster...



finally i made my method so now my code is 2480 lines aprox shorter than it was going to be.
this is an example

//the method
public void checkMaterial(double[,] m, int[] t){
    //lots of stuff recorded in results[]
    //the next time i enter this method, results[] will be overwritten
}




//what happens after someone clics calculate in the form
checkMaterial(material1, desiredProperty1);
label1SP.Text = results[0];
label1TB.Text = results[1];           //still this looks oh so bad to me...
label1C.Text = results[2];
label1DPB.Text = results[3];          //because there are like 20 of this chunks
label1R.Text = results[4];
label1ITE.Text = results[5];
label1GS.Text = results[6];           //just imaging how awfull it could have been without the method
label1PE.Text = results[7];
label1CE.Text = results[8];
label1P.Text = results[9];

checkMaterial(material2, desiredProperty1);
label2SP.Text = results[0];
label2TB.Text = results[1];
label2C.Text = results[2];
label2DPB.Text = results[3];
label2R.Text = results[4];
label2ITE.Text = results[5];
label2GS.Text = results[6];
label2PE.Text = results[7];
label2CE.Text = results[8];
label2P.Text = results[9];




if i where to use an object, wouldn't it end like this?

Material1.check(desiredProperty1);
label1SP.Text = Material1.sp.toString();
label1TB.Text = Material1.tb.toString();
label1C.Text = Material1.c.toString();
label1DPB.Text = Material1.dpb.toString();
label1R.Text = Material1.r.toString();
label1ITE.Text = Material1.ite.toString();
label1GS.Text = Material1.gs.toString();
label1PE.Text = Material1.pe.toString();
label1CE.Text = Material1.ce.toString();
label1P.Text = Material1.p.toString();

Material2.check(desiredProperty1);
label2SP.Text = Material2.sp.toString();
label2TB.Text = Material2.tb.toString();
label2C.Text = Material2.c.toString();
label2DPB.Text = Material2.dpb.toString();
label2R.Text = Material2.r.toString();
label2ITE.Text = Material2.ite.toString();
label2GS.Text = Material2.gs.toString();
label2PE.Text = Material2.pe.toString();
label2CE.Text = Material2.ce.toString();
label2P.Text = Material2.p.toString();





or how?

because tho i have no troubles with this

http://www.dreaminco...izing-the-data/

this other file here

http://www.dreaminco...ny-other-forms/

is a nightmare
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,918
  • Joined: 02-June 10

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 12:53 PM

2480 line SHORTER..? OMG

Even in entire Point of Sale systems I write, I don't think I have too many classes that exceed 2000 lines.

Your code is still trying to micro-manage. Its not actually make use of Properties which have a get/set method within them.

You should not be setting .Text directly like you are. You set a property value. The property might set the GUI.

This is just a HUGE amount of brute force. What's with this results[] array that you never seem to actually set? Oh wait, is it a global variable? Because your method returns void instead of some results for some reason.


1//the method
2 public void checkMaterial(double[,] m, int[] t){
3    //lots of stuff recorded in results[]
4   //the next time i enter this method, results[] will be overwritten
5}


And your style of bracing looks very JAVA: With the opening brace on the signature line. Is that your background, java?

This post has been edited by tlhIn`toq: 30 October 2012 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 01:16 PM

View PosttlhIn`toq, on 30 October 2012 - 12:53 PM, said:

2480 line SHORTER..? OMG

Even in entire Point of Sale systems I write, I don't think I have too many classes that exceed 2000 lines.

Your code is still trying to micro-manage. Its not actually make use of Properties which have a get/set method within them.

You should not be setting .Text directly like you are. You set a property value. The property might set the GUI.


then how should it be done?

View PosttlhIn`toq, on 30 October 2012 - 12:53 PM, said:

This is just a HUGE amount of brute force. What's with this results[] array that you never seem to actually set? Oh wait, is it a global variable? Because your method returns void instead of some results for some reason.


it is indeed


View PosttlhIn`toq, on 30 October 2012 - 12:53 PM, said:

1//the method
2 public void checkMaterial(double[,] m, int[] t){
3    //lots of stuff recorded in results[]
4   //the next time i enter this method, results[] will be overwritten
5}


And your style of bracing looks very JAVA: With the opening brace on the signature line. Is that your background, java?


not quite, i was writing in this screen the whole time, not copy-pasting my code, and just forgot about typing it in another line.

my background is assembler and C. So please lets keep going

how to change the .text property if i shouldn't do it this way?
Was This Post Helpful? 0
  • +
  • -

#11 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 01:35 PM

in here

http://msdn.microsof...orms.label.aspx

i cant find any other way to change what the label says besides

Text Gets or sets the text associated with this control. (Overrides Control.Text.)

ResetText Resets the Text property to its default value. (Inherited from Control.)
Was This Post Helpful? 0
  • +
  • -

#12 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 03:17 PM

I don't understand why you wouldn't use .Text? It has never served me a problem
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 03:23 PM

Tutorial: Why Separation Of Concerns Matters
Was This Post Helpful? 0
  • +
  • -

#14 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 03:52 PM

If you still want to add label arrays to the form then declare this at the top of your form's class so it spans the whole class.
Label[ ] TheLabels = new Label[10];



then add this into the Form_Load Method:
 private void Form1_Load(object sender, EventArgs e)
 {
     TheLabels[i] = new Label();
     TheLabels[i].AutoSize = true;
     // you can set these properties to what ever you want
     TheLabels[i].Text = "label:" + i;
     TheLabels[i].Size = new Size(50, 35);
     TheLabels[i].Location = new Point(i * 40, 20);
     Console.WriteLine("The" + i);
     this.Controls.Add(TheLabels[i]);
     TheLabels[i].BringToFront();      

 }


this just makes 10 labels show across the form

This post has been edited by jase kiw: 30 October 2012 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 vitroblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-October 12

Re: 20 times less lines suggestion for manufacturing application

Posted 30 October 2012 - 04:31 PM

View PostAdamSpeight2008, on 30 October 2012 - 03:23 PM, said:



which encorages me to make the horrible object for the sake of organization...
thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2