C
C#5mo 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
Omnissiah
Omnissiah5mo ago
but you kinda have to tell what is not working
The_Rayz
The_RayzOP5mo 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
Anton5mo 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_RayzOP5mo ago
I will share the codes and form with you tomorrow, I hope I can do it.
The_Rayz
The_RayzOP5mo 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_RayzOP5mo ago
@Anton @boiled goose
Anton
Anton5mo 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_RayzOP5mo ago
Error: Input string is not in the correct format.
Anton
Anton5mo 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_RayzOP5mo ago
I do not know how to do it
Anton
Anton5mo 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_RayzOP5mo ago
help
No description
Anton
Anton5mo ago
You're probably using .net framework use .net not .net framework
The_Rayz
The_RayzOP5mo ago
I looked at the zip file, can you help me?
Anton
Anton5mo ago
It should use SDK style csproj format The csproj format in that zip is a relic of the past Don't use it
The_Rayz
The_RayzOP5mo ago
Is there any chance I can send it to you?
Anton
Anton5mo ago
Send me what?
The_Rayz
The_RayzOP5mo ago
I need some help with codes or screen sharing. What can I show you that you can help me with?
Anton
Anton5mo ago
read about sdk-style projects and how msbuild works in general
The_Rayz
The_RayzOP5mo ago
ok thabks
Want results from more Discord servers?
Add your server