Hello guys, I would like to print out the property names and the values but can't find the right

BlazeBin - rmeikgewgcfo
A tool for sharing your source code with the world!
58 Replies
TheRanger
TheRanger3y ago
if ur on a Console Project just use Console.WriteLine to print the property names and their values
Console.WriteLine("Title: " + Title);
Console.WriteLine("Title: " + Title);
The king of kings
for example, I want to output like this: Title: Spicy-Sweet Glazed Salmon Type: Lunch Number: 2 Difficulty: Intermediate Ingredients: 2 Now it's only outputting the values Unfortunately, I couldn't accomplish this!
TheRanger
TheRanger3y ago
on the same line?
Console.WriteLine("Title: " + Title);
Console.WriteLine("Type: " + Type);
Console.WriteLine("Number: " + Number);
Console.WriteLine("Difficulty: " + Difficulty);
Console.WriteLine("Ingredients: " + Ingredients);
Console.WriteLine("Title: " + Title);
Console.WriteLine("Type: " + Type);
Console.WriteLine("Number: " + Number);
Console.WriteLine("Difficulty: " + Difficulty);
Console.WriteLine("Ingredients: " + Ingredients);
The king of kings
Ok Great! I'll give it a try.
The king of kings
Unfortunately! I got errors.
TheRanger
TheRanger3y ago
you need a reference of your recipe instance in order to access its members eg
Console.WriteLine("Title: " + recipe1.Title);
Console.WriteLine("Title: " + recipe1.Title);
The king of kings
Ok
TheRanger
TheRanger3y ago
my example was just if you use that code inside Recipe's method
The king of kings
You're awesome bro, it worked just I expected 😃 Oh my god! I spent days trying to figure out this and you just solved it in minutes.
TheRanger
TheRanger3y ago
you're just missing the basic knowledge of C# try studying C# from $helloworld
TheRanger
TheRanger3y ago
literally the 2nd video teaches you that 1st video is just an introduction
The king of kings
I am so terrible at coding and creating projects. How do you know all that? How could solve this although it's not mentioned in the document?
TheRanger
TheRanger3y ago
what document?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
I mean the Microsoft tutorial
TheRanger
TheRanger3y ago
it is mentioned string concatenation is one of the fundamentals of C#
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
I've been watching C# tutorials for many months but I don't feel I'm doing any progress.
TheRanger
TheRanger3y ago
one of the first things you learn
The king of kings
Ok I would like to ask you guys, do you guys recommend me to purchase a good course to be able to learn the fundemantls or should I just continue practice? I feel like I'm wasting many days every time I encounter an issue of finding the right code to implement any project.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
$helloworld is a good course
TheRanger
TheRanger3y ago
and it's free
The king of kings
Aha! Ok! Great to hear that, then I'll take your advice. That's right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
Ok! The problem with google is that you don't find the specific solution for your project.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
Ok
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
Ok! I was thinking of this way too in order to shorten the lines of code. Instead of adding all these. Console.WriteLine("Title: " + recipe1.Title); Console.WriteLine("Type: " + recipe1.Type); Console.WriteLine("Number: " + recipe1.Number); Console.WriteLine("Difficulty: " + recipe1.Difficulty); Console.WriteLine("Ingredients: " + recipe1.Ingredients
TheRanger
TheRanger3y ago
you could, with System.Reflection methods
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
it wont shorten the lines of code tho
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
foreach(var property in recipe1.GetType().GetProperties())
{
Console.WriteLine($"{property.Name}: {property.GetValue(recipe1)}");
}
foreach(var property in recipe1.GetType().GetProperties())
{
Console.WriteLine($"{property.Name}: {property.GetValue(recipe1)}");
}
that will iterate through each property of recipe1, prints its name and its value
The king of kings
I typed instead in this way but it didn't seem right to me Console.WriteLine($"Title: " + recipe1.Title + "Type: " + recipe1.Type + "Number: " + recipe1.Number + "Difficulty: " + recipe1.Difficulty + "Ingredients: " + recipe1.Ingredients);
TheRanger
TheRanger3y ago
you can only write this code once, and all types of C# can use it by just replacing recipe1 with a type of object that can be passed through an argument of a method that will just print your data on the same line
The king of kings
yeah
TheRanger
TheRanger3y ago
u can put a "\n" or Environment.NewLine between the string to append a new line
MODiX
MODiX3y ago
TheRanger#3357
REPL Result: Success
Console.WriteLine($"Hello \nWorld{Environment.NewLine}How's life");
Console.WriteLine($"Hello \nWorld{Environment.NewLine}How's life");
Console Output
Hello
World
How's life
Hello
World
How's life
Compile: 541.925ms | Execution: 27.277ms | React with ❌ to remove this embed.
The king of kings
Oh! Really, I didn't know that.
TheRanger
TheRanger3y ago
google won't really help if you're trying to search for a specific solution, just take the course i linked above
The king of kings
I tried to add the but didn't accomplish it Console.WriteLine($"Title: " + recipe1.Title{Environment.NewLine} + "Type: " + recipe1.Type \n+ "Number: " + recipe1.Number + "Difficulty: " + recipe1.Difficulty + "Ingredients: " + recipe1.Ingredients);
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
\n it works if I add it like this "title: \n" but then the value property is not append with theTitle Ok! I'll do that.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
Ok Title: Spicy-Sweet Glazed Salmon Type: Lunch Number: 2 I didn't get the rest like (Difficulty & Ingridents) It's fixed now! I had to add the get; set; for both
The king of kings
As matus mentioned, what if I wanted to create recipe2, and recipe3 should I copy all these codes 2 times in order to create a total of 3 receipies?
Monsieur Wholesome
If you do the same steps at multiple places, where the only things different is the variable, you will want to write a method for it
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger3y ago
thats where you create methods to avoid writing repeated codes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
The king of kings
Ok! I guess I need to review again the same content that provides the feature that's required in a project. Hey everyone, I have updated my code and I hope I got it right since it's meant to be a practicing project. https://paste.mod.gg/
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?