C
C#2mo ago
The_Rayz

I'm making a pipe friction loss calculation tool

Hello, the system I am trying to make, the calculation tool, is the pipe friction loss calculation, but I could not do it. Can anyone help me with this?
No description
No description
No description
21 Replies
unhAPI
unhAPI2mo ago
but you kinda have to tell what is not working
The_Rayz
The_Rayz2mo ago
izle şimdi When I try to calculate it, it gives an error. Some of the values are not the same as those on the website. What I want to do is that when the same values are entered as on the website, the result is the same. The code is very faulty, that's why. site link
Anton
Anton2mo ago
make a unit test make the function that computes the result of the calculation static (and pure) then test it with the inputs from the image if that works, you'll know the problem is with how your ui parses inputs or how it displays the result
The_Rayz
The_Rayz2mo ago
I will share the codes and form with you tomorrow, I hope I can do it.
The_Rayz
The_Rayz2mo ago
As I said, I am making a program to calculate pipe friction loss, but I could not solve the problems. When I try to calculate, it gives this error. What is the reason for this? Here are the codes.
The_Rayz
The_Rayz2mo ago
@Anton @boiled goose
Anton
Anton2mo ago
What does the error say in english Don't switch on strings, make enums for everything. Then you will be able to move that logic to the static function I mentioned
The_Rayz
The_Rayz2mo ago
Error: Input string is not in the correct format.
Anton
Anton2mo ago
I mean, you can switch on them, but you should convert them to enums, and then switch on enum / use them to index an array of constants
The_Rayz
The_Rayz2mo ago
I do not know how to do it
Anton
Anton2mo ago
enum DebiBirimMeasure
{
Meter3PerHour,
LtPerHour,
Meter3PerSecond,
KilogramPerHour,
KilogramPerSecond,
Count,
}
static class Conversion
{
public static DebiBirimMeasure ConvertDebiBirim(string str)
{
return str switch
{
"m3/h" => DebiMirimMeasure.Meter3PerHour,
"lt/h" => DebiMirimMeasure.LtPerHour,
// ...
};
}
}
static class Constants
{
// Either like this with a lookup
private static readonly double[] DebiMirimLookup = ComputeDebiMirimLookup();

private static double[] ComputeDebiMirimLookup()
{
var ret = new double[(int) DebiMirimMeasure.Count];
ret[(int) DebiMirimMeasure.Meter3PerHour] = 1 / 3600.0
// ...

// Make sure you've initialized all of the constants
Debug.Assert(!ret.Any(x => x == 0));

return ret;
}

// Or like this, with a switch
public static double GetDebiMirimCoefficient(DebiMirimMeasure m) => m switch
{
DebiMirimMeasure.Meter3PerHour => 1 / 3600.0,
DebiMirimMeasure.LtPerHour => 1 / 360_000.0,
// ...
};
}
enum DebiBirimMeasure
{
Meter3PerHour,
LtPerHour,
Meter3PerSecond,
KilogramPerHour,
KilogramPerSecond,
Count,
}
static class Conversion
{
public static DebiBirimMeasure ConvertDebiBirim(string str)
{
return str switch
{
"m3/h" => DebiMirimMeasure.Meter3PerHour,
"lt/h" => DebiMirimMeasure.LtPerHour,
// ...
};
}
}
static class Constants
{
// Either like this with a lookup
private static readonly double[] DebiMirimLookup = ComputeDebiMirimLookup();

private static double[] ComputeDebiMirimLookup()
{
var ret = new double[(int) DebiMirimMeasure.Count];
ret[(int) DebiMirimMeasure.Meter3PerHour] = 1 / 3600.0
// ...

// Make sure you've initialized all of the constants
Debug.Assert(!ret.Any(x => x == 0));

return ret;
}

// Or like this, with a switch
public static double GetDebiMirimCoefficient(DebiMirimMeasure m) => m switch
{
DebiMirimMeasure.Meter3PerHour => 1 / 3600.0,
DebiMirimMeasure.LtPerHour => 1 / 360_000.0,
// ...
};
}
You maybe want to argue that you don't need the enum, but it's just way easier to see what values you can actually provide it
static class Formula
{
public readonly struct ComputeArgs
{
public required DebiMirimMeasure DebiMirimMeasure { get; init; }
public required double Akiskan { get; init; }
// ...
}
public static double Compute(ComputeArgs args)
{
// This does the calculus, not the form
}
}
static class Formula
{
public readonly struct ComputeArgs
{
public required DebiMirimMeasure DebiMirimMeasure { get; init; }
public required double Akiskan { get; init; }
// ...
}
public static double Compute(ComputeArgs args)
{
// This does the calculus, not the form
}
}
The form parses data and calls
Formula.Compute(new()
{
Akiskan = ...,
DebiMirimMeasure = Conversion.ConvertDebiBirim(debiMirimText),
// ...
});
Formula.Compute(new()
{
Akiskan = ...,
DebiMirimMeasure = Conversion.ConvertDebiBirim(debiMirimText),
// ...
});
Then make a unit test that does
double result = Formula.Compute(new()
{
Akiskan = 5,
DebiMirimMeasure = DebiMirimMeasure.LtPerSecond,
// ...
});
Assert.Equal(expectedValue, result);
double result = Formula.Compute(new()
{
Akiskan = 5,
DebiMirimMeasure = DebiMirimMeasure.LtPerSecond,
// ...
});
Assert.Equal(expectedValue, result);
Dictionary<string, double> roughnessValues = new Dictionary<string, double>
{
{ "CTP", 0.0001 },
{ "PE-X", 0.000007 },
{ "PPRC", 0.0001 },
{ "PVC", 0.0000015 },
{ "Çelik", 0.000045 }
};

cmbBoruCinsi.Items.Add("CTP");
cmbBoruCinsi.Items.Add("PE-X");
cmbBoruCinsi.Items.Add("PPRC");
cmbBoruCinsi.Items.Add("PVC");
cmbBoruCinsi.Items.Add("Çelik");
Dictionary<string, double> roughnessValues = new Dictionary<string, double>
{
{ "CTP", 0.0001 },
{ "PE-X", 0.000007 },
{ "PPRC", 0.0001 },
{ "PVC", 0.0000015 },
{ "Çelik", 0.000045 }
};

cmbBoruCinsi.Items.Add("CTP");
cmbBoruCinsi.Items.Add("PE-X");
cmbBoruCinsi.Items.Add("PPRC");
cmbBoruCinsi.Items.Add("PVC");
cmbBoruCinsi.Items.Add("Çelik");
Don't do this. Don't let your constants appear in two places Your strings should be in an array, if you're going to need to do some action on each of them But you still need access to individual constants to initialize the lookup Here, you either have to do a custom type, that has a field for each of the constants, and is enumerable Or make an array and lookup by index (enum casted to int) This pattern is really really common, and I see few people actually doing the right thing As is, your methods aren't even static for some reason The least you can do is make them static and test them separately
The_Rayz
The_Rayz2mo ago
help
No description
Anton
Anton2mo ago
You're probably using .net framework use .net not .net framework
The_Rayz
The_Rayz2mo ago
I looked at the zip file, can you help me?
Want results from more Discord servers?
Add your server