Help with some c# code please

Hi everyone and a happynew year to you all.

I am trying to populate some decimal boxes with the costs of various part numbers. What I am trying to is to read the part numbers into an array and then get the costs one by one.

I have this piece of code which I am using to try and read the part numbers.

decimal outStdMatCost = 0;

while (i < 13)
		        {
string PartNum = "Inputs.fb" + bomPartNumber[i - 1] + "PartNo.Value";
							if (PartNum != null)
										{
ReadPartCostForStdCost(PartNum, out outStdMatCost);
Inputs.decPartCost.Value = outStdMatCost;
//Inputs.decFrameExtnCost.Value = Inputs.decPartCost.Value * Inputs.decFrameExtnQty.Value;
Inputs.decCost.Value = Inputs.decCost.Value + Inputs.decPartCost.Value;
string msg = PartNum;
MessageBox.Show(msg);
}
 i = i + 1;
}

My problem is that instead of the part number which is in the textbox I am getting the name of the textbox each time. ie; Inputs.fbSubFramePartNo.Value.

Can anyone help me with the correct syntax to return the contents rather than the name please?

Any help much appreciated.

Adrian.

You are passing the name of the input to the function instead of its value… Unfortunately in C# is not easy to dynamically get values for similarly named variables by using a loop without going into the deep dark web of Reflection.
There are 13 inputs you are trying to use you could just write the code for each one, if you want to use reflection it can be done but it isn’t a walk in the park
You’ll have to find the class which defined these inputs then use reflection to get an instance of the field and then once again use reflection to get it’s Value property. It would go something like this

string PartNum = "Inputs.fb" + bomPartNumber[i - 1] + "PartNo.Value";
var fi = this.GetType().GetField(partNum, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
var obj = fi.GetValue(this);

var fiVal = obj.GetType().GetField("Value",  System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
ReadPartCostForStdCost(fiVal.GetValue(obj), out outStdMatCost);

Note that the above code I wrote off my head and it isn’t tested or even syntactically valid. I am just trying to get you an idea of what it would take to do what you are trying to do. Also “this” may not be the correct instance needed to get a hold of the Inputs.

Thanks Jose, I see what you mean about it not being straight forward. The
reason I was trying this is that I have 20+ configurators to convert from
Progress ABL to C#. The method I used for retrieving the costs is the same
in all of them but was very repetitive. The ABL code for one part is below.

Find first partcost where partcost.CostID = ‘1’ and partcost.partnum = fb
FrameExtPartNo no-lock no-error.
If available partcost then do:
decPartCost = partcost.stdMaterialCost.
decFrameExtnCost = decPartCost * decFrameExtnQty.
decCost = decCost + decFrameExtnCost.
End.

My thinking was that by creating a variable to hold the ‘FrameExt’ section
(in the above case). I could re-use the same method for all the parts in
all the configurators, which have various quantities of part numbers in
their bill of materials,

It just seems inefficient to be writing the same lines of code over and
over while only changing a part each time.

Any advice you have would be useful as I am under pressure to get
everything converted asap.

Thanks

Adrian.

Give the code I posted a shot and see, it should (mostly) work with some tweaking.

I got your code to compile error free which was great but I get the error “Object reference not set to an instance of an object” every time it runs now.

Here is the code as it stands at the moment. Can you see what the problem is?

String[] bomPartNumber = new String[12];
int i = 1;

			bomPartNumber[0] = "FrameExt";
    bomPartNumber[1] = "SubFrame";
    bomPartNumber[2] = "BladeExt";
    bomPartNumber[3] = "RearBladeExt";
    bomPartNumber[4] = "Wire";
    bomPartNumber[5] = "BladeCirclip";
    bomPartNumber[6] = "NylonBearing";
    bomPartNumber[7] = "Handle";
    bomPartNumber[8] = "Cleat";
    bomPartNumber[9] = "Paint";
    bomPartNumber[10] = "Fixing";
    bomPartNumber[11] = "Mullion";

decimal outStdMatCost = 0;

while (i < 12)
{
string PartNum = “Inputs.fb” + bomPartNumber[i - 1] + “PartNo.Value”;

var fi = this.GetType().GetField(PartNum, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
var obj = fi.GetValue(this);
var fiVal = obj.GetType().GetField(“Value”, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);

ReadPartCostForStdCost(fiVal.GetValue(obj).ToString(), out outStdMatCost);
Inputs.decPartCost.Value = outStdMatCost;
Inputs.decCost.Value = Inputs.decCost.Value + Inputs.decPartCost.Value;
string msg = fiVal.GetValue(obj).ToString();
MessageBox.Show(msg);

i = i + 1;
}

I have now got rid of the error message by checking for a null value.

The problem is that fi is always null so I’m getting no results.

String[] bomPartNumber = new String[12];
int i = 1;

bomPartNumber[0] = “FrameExt”;
bomPartNumber[1] = “SubFrame”;
bomPartNumber[2] = “BladeExt”;
bomPartNumber[3] = “RearBladeExt”;
bomPartNumber[4] = “Wire”;
bomPartNumber[5] = “BladeCirclip”;
bomPartNumber[6] = “NylonBearing”;
bomPartNumber[7] = “Handle”;
bomPartNumber[8] = “Cleat”;
bomPartNumber[9] = “Paint”;
bomPartNumber[10] = “Fixing”;
bomPartNumber[11] = “Mullion”;

decimal outStdMatCost = 0;
while (i < 12)
{
string PartNum = “Inputs.fb” + bomPartNumber[i - 1] + “PartNo.Value”;
var fi = this.GetType().GetField(PartNum,
System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.Static);

if(fi != null)
{
string msg = fi.ToString();
MessageBox.Show(msg);

var obj = fi.GetValue(this);
var fiVal = obj.GetType().GetField(“Value”,
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);
ReadPartCostForStdCost(fiVal.GetValue(obj).ToString(), out outStdMatCost);
Inputs.decPartCost.Value = outStdMatCost;
Inputs.decCost.Value = Inputs.decCost.Value + Inputs.decPartCost.Value;
}
i = i + 1;
}

Like I said “this” may not be the right class