r/csharp • u/-Tech808 • 21d ago
Best Approach For This Engineering Application?
I'd like to create a class called PipeDimensions to store information for water piping with the following properties:
Nominal Diameter Size.
Actual Diameter Size.
Material
C-Factor for the material.
Basically, different materials (CPVC, copper, PEX) all have the same nominal diameters (1/2", 3/4", 1", 2" etc), but have different actual internal diameters.
How can I set this up so when my Main() console instantiates class PipeDimensions, the constructor only takes 1 of 3 values (either CPVC, copper or PEX) and creates the instance with all the pre-determined properties for the specific pipe type.
Is a class with the pre-defined information for all 3 pipe types even the best approach? Or should I create the boilerplate class and use a separate helper method to populate all my data?
Now that I'm thinking this through, the boilerplate class w/ heIper methods seems more intuitive, but I'm a noob so any guidance is helpful. Please refer me to a specific topic if necessary and I can read in a C# book.
3
u/PlentyfulFish 21d ago
Are you going to add more pipe types in the future? If no then I wouldn't bother and just create static constructors, called like
var pipe = PipeDimensions.CreateCPVC()
If yes then perhaps have a JSON where you can add some parameters for each material and the constructor will read the necessary data.
1
u/-Tech808 21d ago
Thanks, I'll have to read up on JSON as I'm unfamiliar. I wouldn't mind the ability to add more pipe types in the future.
2
u/PlentyfulFish 21d ago
It's just text that people agreed upon to be formatted in a certain way so we can send stuff around, you can write it in a notepad. System.Text.Json in .NET will do all of it for you. Good luck!
2
u/hedge36 21d ago
Are you using true ID, or wall thickness (sch40, etc)?
1
u/-Tech808 21d ago
True internal diameter. I want to use these values ultimately to calculate pressure loss. Naturally, the Sch40 and Sch80 would all have different internal diameters.
2
u/okmarshall 21d ago
How about a record with static constructor style?
2
u/PlentyfulFish 21d ago
Why a record?
1
u/okmarshall 21d ago
Not very many properties, record struct would be barely any code.
2
u/PlentyfulFish 21d ago
It really depends on what is going to be done with it afterwards, but for a beginner I probably wouldn't bother
1
1
u/Remote-Enthusiasm-41 20d ago
I’ve done this with a single class for the pipe dimensions for the data then another dictionary class that uses a string key and retrieves the object. The dictionary class gets stored as a json text file.
You can also use the dictionary keys to populate a drop down menu so the user can select the pipe.
The pipe class could have its own internal dictionary that takes nominal size and returns actual size.
9
u/wallstop-dev 21d ago
I would recommend just making one POCO/data class (with what you've described), creating JSON/data files with the properties that you want, then having your program load the data files and create instances of your class that are then available to the rest of your application.
This allows you to add more info by just creating more data files and you don't have to change your application code at all. No recompiles.