C
C#8mo ago
Dinny

are my getters and setters correct?

Before I work on the rest of my code, please let me know if there are any errors in my getters and setters using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class Menu { private string dishName; private int dishType, dishPrice; //def constructor class Menu() { } //const with param public Menu(String dishName, int dishType, int dishPrice) { SetName(dishName); SetType(dishType); SetPrice(dishType); } //GETTERS / Returns the name of the dish. @return: dish name */ public void GetName() { return dishName; } / Returns the type of dish ordered. @return: returns either appetizer, entree, or dessert */ public int GetType() { return dishType; } / Returns the height of the rectangle. @return: height of the rectangle */ public interface GetPrice() { returns dishPrice; } //SETTERS / Updates name of dish. @param width: the updated dish name */ public void SetName(string dishName) { this.Name = dishName; } / Updates the type of dish. @param width: the updated dish type */ public void SetType(string dishType) { this.Type = dishType; } / Updates the type of dish. @param width: the updated dish type */ public void SetPrice(string dishPrice) { this.Price = dishPrice; } public override string ToString() { return string.Format(dishName + "(" + dishPrice + ")", GetName(), GetPrice()); } }
173 Replies
Angius
Angius8mo ago
$code
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius8mo ago
They're correct in that they will work But... they're useless C# has properties for that It ain't Java
Jimmacle
Jimmacle8mo ago
yeah you'd never see this in normal C# code you'd just use autoproperties
Pobiega
Pobiega8mo ago
The "constructor" is also uh... Not a constructor. I don't think this even compiled
Jimmacle
Jimmacle8mo ago
looks like it should, the formatting is just messed up
Pobiega
Pobiega8mo ago
GetName is void and returns a string.
Jimmacle
Jimmacle8mo ago
oh i see
Pobiega
Pobiega8mo ago
This does not compile.
Jimmacle
Jimmacle8mo ago
my eyes must automatically block out impossible code
Murten
Murten8mo ago
Not with that attitude. (Also, returns does not work as a substitute for return)
Pobiega
Pobiega8mo ago
this strikes me as (invalid) java code written by someone who learned C at uni
Murten
Murten8mo ago
/ Returns the height of the rectangle.
@return: height of the rectangle
*/

public interface GetPrice() { returns dishPrice; }
/ Returns the height of the rectangle.
@return: height of the rectangle
*/

public interface GetPrice() { returns dishPrice; }
This strikes me as a wtf.
Pobiega
Pobiega8mo ago
interface 😄 @Dinny please re-join us, we need feedback on our feedback
Jimmacle
Jimmacle8mo ago
seems like the correct course of action is "actually test your code and make sure it compiles before asking for advice on good practices"
Murten
Murten8mo ago
I think the getters and setters should be the least of his concern.
Dinny
Dinny8mo ago
apologies, i didn’t see earlier messages allow me a moment to read up i have two separate files this one was getting all the constructors, getters, setters, and the override to string and stuff the second file calls upon this one to actually do thing
Jimmacle
Jimmacle8mo ago
okay, but this file doesn't compile it's not valid C#
Dinny
Dinny8mo ago
well that’s the assignment anyways
Jimmacle
Jimmacle8mo ago
did you try it before asking for help?
Dinny
Dinny8mo ago
well yes that’s
Jimmacle
Jimmacle8mo ago
your IDE should be screaming at you
Dinny
Dinny8mo ago
why i’m here it’s not that’s why i’m here
Jimmacle
Jimmacle8mo ago
what editor are you using?
Dinny
Dinny8mo ago
it’s saying no syntax errors no warnings or anything i’m using visual studios
Jimmacle
Jimmacle8mo ago
either way the compiler will output errors if you try to run the code
Dinny
Dinny8mo ago
it’s not outputting anything i’m trying to put things in an output file
Pobiega
Pobiega8mo ago
do you have a project?
Jimmacle
Jimmacle8mo ago
okay, so let's start from the very beginning yes, what pob asked
Dinny
Dinny8mo ago
yes
Murten
Murten8mo ago
doubt
Jimmacle
Jimmacle8mo ago
so you have a .sln, .csproj, and one or more .cs files?
Dinny
Dinny8mo ago
.cs i have two of those
Jimmacle
Jimmacle8mo ago
that's not a project
Dinny
Dinny8mo ago
oh ugh this shii is so hard i’m gonna throw up
Jimmacle
Jimmacle8mo ago
just go into VS's file menu, click new project, and console application (i'm assuming that's where it is, i don't use VS) C# applications are based on projects, you can't run loose .cs files
Dinny
Dinny8mo ago
i don’t have that option on VS :/ one second
Pobiega
Pobiega8mo ago
No description
Dinny
Dinny8mo ago
i went there i had to go all the way out
Pobiega
Pobiega8mo ago
No description
Dinny
Dinny8mo ago
yes i’m using that
Pobiega
Pobiega8mo ago
okay Well, your first file clearly didn't
Dinny
Dinny8mo ago
didn’t what
Pobiega
Pobiega8mo ago
have a project file
Dinny
Dinny8mo ago
i’m not sure how tho i went through the same steps
Pobiega
Pobiega8mo ago
C# and .NET is entirely project based. You never open a folder or a file directly
Dinny
Dinny8mo ago
for both files
Pobiega
Pobiega8mo ago
you open the project file
Jimmacle
Jimmacle8mo ago
can you send a screenshot of your solution explorer window? with your project open
Dinny
Dinny8mo ago
one moment
Dinny
Dinny8mo ago
sorry the picture would not load for some reason
No description
Dinny
Dinny8mo ago
it’s working now tho
Jimmacle
Jimmacle8mo ago
your program or the picture?
Dinny
Dinny8mo ago
the picture
Jimmacle
Jimmacle8mo ago
okay, so what happens if you click the run button in the toolbar at the top of VS
Dinny
Dinny8mo ago
ok now i’m getting errors i added in the stream reader stuff since i last posted too tho idk if that’s contributing to it
Jimmacle
Jimmacle8mo ago
right now i'm focused on making sure your IDE is working correctly
Dinny
Dinny8mo ago
alright
Jimmacle
Jimmacle8mo ago
you should be seeing some red squiggles in your code and errors listed in the problems window at the bottom, is that what's happening?
Dinny
Dinny8mo ago
yes
Jimmacle
Jimmacle8mo ago
alright, seems like it's fine now
Dinny
Dinny8mo ago
i see a couple green squiggles as well
Jimmacle
Jimmacle8mo ago
those are just suggestions, they won't stop your code from compiling the red ones are what need to be fixed
Dinny
Dinny8mo ago
alright
Pobiega
Pobiega8mo ago
once you've fixed the reds, you might want to take a look at the greens too thou 🙂 but red takes priority
Dinny
Dinny8mo ago
ty i will keep that in mind as well omg there’s so much red now
Pobiega
Pobiega8mo ago
yup! Lets just say that just that snippet of "getters and setters" contained quite a bit of literal nonsense code using keywords where they dont belong, and typos in keywords etc
Dinny
Dinny8mo ago
i was following my profs example which is why it looks this way but i’m sure where i went wrong bc our code is quite similar
Jimmacle
Jimmacle8mo ago
professors are teaching people to write get/set methods in C#? Harold
Dinny
Dinny8mo ago
he’s honestly teaching way too much at once like this is a beginner class, i should not be struggling this hard i was doing fine in python and c++
Jimmacle
Jimmacle8mo ago
my point is what they're teaching you isn't how you should be doing it
Dinny
Dinny8mo ago
😨😨😨
Jimmacle
Jimmacle8mo ago
but that tends to happen in CS classes
Dinny
Dinny8mo ago
😭😭 we’re u taught differently were *
Jimmacle
Jimmacle8mo ago
i'm 99% self taught
Dinny
Dinny8mo ago
😨 then how did u teach urself set and get bc clearly im doing it wrong :/
Jimmacle
Jimmacle8mo ago
dropped out of college when i got a job offer
Dinny
Dinny8mo ago
oh wow congrats
Jimmacle
Jimmacle8mo ago
i picked it up at some point
Pobiega
Pobiega8mo ago
same story for me. 99% self taught.
Dinny
Dinny8mo ago
dang that’s p cool i feel if i had a different prof i wouldn’t be struggling this hard
Jimmacle
Jimmacle8mo ago
anyway, the way you should be doing this is with properties, auto-properties specifically https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
Properties - C# Programming Guide - C#
A property in C# is a member that uses accessor methods to read, write, or compute the value of a private field as if it were a public data member.
Dinny
Dinny8mo ago
like i’m doing great in my other coding classes
Pobiega
Pobiega8mo ago
the thing you are doing wrong with getset is that C# already has it built in with something called "properties"
Dinny
Dinny8mo ago
if that’s the case, idky he’s teaching it this way
Pobiega
Pobiega8mo ago
but your weird usage of class, interface, void and return was the problem thou thats why most of us went "wtf"
Dinny
Dinny8mo ago
i’m going wtf 😭
Pobiega
Pobiega8mo ago
the code wasnt just conceptually wrong, it was syntactically wrong too as in, this is so wrong the compiler will not even let it run
Dinny
Dinny8mo ago
his lectures and way of explaining gives me a stroke. i end up having to watch youtube videos after class ugh
Jimmacle
Jimmacle8mo ago
it might help if you try to explain to us why you used some of those keywords in the places you did
Dinny
Dinny8mo ago
i have literally no hope for this class this shouldn’t be that hard because my prof did
Jimmacle
Jimmacle8mo ago
i'm skeptical
Dinny
Dinny8mo ago
one moment
Jimmacle
Jimmacle8mo ago
i mean, i've had professors give me code that won't work right before but not this bad
Dinny
Dinny8mo ago
this is part of his sample code
No description
Dinny
Dinny8mo ago
he’s doing a rectangle and i’m reading items from a menu i had to tweak some things tho
Jimmacle
Jimmacle8mo ago
yes, so your professor did not do what you're doing wrong
Dinny
Dinny8mo ago
sigh
Murten
Murten8mo ago
Still goofy code though.
Jimmacle
Jimmacle8mo ago
yeah, but just "weird practices" compared to "literal nonsense" look at how your professor is defining the get and set methods and try to find the problems with yours
Murten
Murten8mo ago
Tell your proffessor to stop using C# 2.0
Dinny
Dinny8mo ago
😭 Alright one moment ok so i got rid of most of the wrrors but i’m unsure got to fix this o e
Pobiega
Pobiega8mo ago
$codegif
Pobiega
Pobiega8mo ago
post your updated code using a code block, like in this gif
Dinny
Dinny8mo ago
wait how do i put my code in that gif i’ve never done that before
Pobiega
Pobiega8mo ago
just type the characters then paste your code
Dinny
Dinny8mo ago
like i type $code gif and then late my code ?
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Dinny
Dinny8mo ago
oh lmao alr one moment i didnt mean to hit enter
cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



public class Menu
{
private string dishName;
private int dishPrice, dishType;




//const with param
public Menu(string dishName = " ", int dishType = 0, int dishPrice = 0)
{
SetName(dishName);
SetType(dishType);
SetPrice(dishPrice);
}



//GETTERS

/** Returns the name of the dish.
@return: dish name
*/

public string GetName() { return dishName; }


/** Returns the type of dish ordered.
@return: returns either appetizer, entree, or dessert
*/

public int GetType() { return dishType; }

/** Returns the price of the dish.
@return: dish price
*/

public int GetPrice() { return dishPrice; }


//SETTERS

/** Updates name of dish.
@param width: the updated dish name
*/

public void SetName(string dishName) { this.dishName = dishName; }

/** Updates the type of dish.
@param width: the updated dish type
*/
public void SetType(int dishType) { this.dishType = dishType; }

/** Updates the type of dish.
@param width: the updated dish type
*/

public void SetPrice(string dishPrice) { this.dishPrice = dishPrice; }

/** Customizes the output format of the menu.
@return: a string representing the output format of the menu
*/

public override string ToString() { return string.Format(dishName + "(" + dishPrice + ")", GetName(), GetPrice()); }

}
cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



public class Menu
{
private string dishName;
private int dishPrice, dishType;




//const with param
public Menu(string dishName = " ", int dishType = 0, int dishPrice = 0)
{
SetName(dishName);
SetType(dishType);
SetPrice(dishPrice);
}



//GETTERS

/** Returns the name of the dish.
@return: dish name
*/

public string GetName() { return dishName; }


/** Returns the type of dish ordered.
@return: returns either appetizer, entree, or dessert
*/

public int GetType() { return dishType; }

/** Returns the price of the dish.
@return: dish price
*/

public int GetPrice() { return dishPrice; }


//SETTERS

/** Updates name of dish.
@param width: the updated dish name
*/

public void SetName(string dishName) { this.dishName = dishName; }

/** Updates the type of dish.
@param width: the updated dish type
*/
public void SetType(int dishType) { this.dishType = dishType; }

/** Updates the type of dish.
@param width: the updated dish type
*/

public void SetPrice(string dishPrice) { this.dishPrice = dishPrice; }

/** Customizes the output format of the menu.
@return: a string representing the output format of the menu
*/

public override string ToString() { return string.Format(dishName + "(" + dishPrice + ")", GetName(), GetPrice()); }

}
i have squiggles under dishPrice right after it says SetPrice oh wait'hang on
Pobiega
Pobiega8mo ago
You also goofed up the codeblock It seems to me you need to pay more attention to details
Dinny
Dinny8mo ago
i figured it out im trying dawg i am still new at this lol but i got rid of the red squiggles finally
Pobiega
Pobiega8mo ago
Yeah but look
Dinny
Dinny8mo ago
looking
Pobiega
Pobiega8mo ago
You had both a gif literally showing you how to do it, and the verbatim text to write to make a block but you still decided to put cs on a new line instead of directly after the tripple backtick
Dinny
Dinny8mo ago
i clicked the wrong character and i accidentally hit enter i dont think thats a crime
Pobiega
Pobiega8mo ago
then you fix it? discord supports editing
Dinny
Dinny8mo ago
://// ok the embed is there tho now
Pobiega
Pobiega8mo ago
yeah but if you'd fix it, you'd see why we want it done properly
Dinny
Dinny8mo ago
i did tho
Pobiega
Pobiega8mo ago
nope
Pobiega
Pobiega8mo ago
No description
Dinny
Dinny8mo ago
im getting drilled into bc i left a cs in there or is it somehting else i was just following the gif
Pobiega
Pobiega8mo ago
no you are not look at the gif again look where it places the cs look where you placed it these are not the same you've done python, so you should know what significant whitespace is
Dinny
Dinny8mo ago
i was told that that didn't matter in c# tho and in c++ well unless we were working with input an doutput files and*
Pobiega
Pobiega8mo ago
but this is discord, not C#
Dinny
Dinny8mo ago
okay dawg i will just fix it
Pobiega
Pobiega8mo ago
please do
Dinny
Dinny8mo ago
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



public class Menu
{
private string dishName;
private int dishPrice, dishType;




//const with param
public Menu(string dishName = " ", int dishType = 0, int dishPrice = 0)
{
SetName(dishName);
SetType(dishType);
SetPrice(dishPrice);
}



//GETTERS

/** Returns the name of the dish.
@return: dish name
*/

public string GetName() { return dishName; }


/** Returns the type of dish ordered.
@return: returns either appetizer, entree, or dessert
*/

public int GetType() { return dishType; }

/** Returns the price of the dish.
@return: dish price
*/

public int GetPrice() { return dishPrice; }


//SETTERS

/** Updates name of dish.
@param width: the updated dish name
*/

public void SetName(string dishName) { this.dishName = dishName; }

/** Updates the type of dish.
@param width: the updated dish type
*/
public void SetType(int dishType) { this.dishType = dishType; }

/** Updates the type of dish.
@param width: the updated dish type
*/

public void SetPrice(string dishPrice) { this.dishPrice = dishPrice; }

/** Customizes the output format of the menu.
@return: a string representing the output format of the menu
*/

public override string ToString() { return string.Format(dishName + "(" + dishPrice + ")", GetName(), GetPrice()); }

}
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



public class Menu
{
private string dishName;
private int dishPrice, dishType;




//const with param
public Menu(string dishName = " ", int dishType = 0, int dishPrice = 0)
{
SetName(dishName);
SetType(dishType);
SetPrice(dishPrice);
}



//GETTERS

/** Returns the name of the dish.
@return: dish name
*/

public string GetName() { return dishName; }


/** Returns the type of dish ordered.
@return: returns either appetizer, entree, or dessert
*/

public int GetType() { return dishType; }

/** Returns the price of the dish.
@return: dish price
*/

public int GetPrice() { return dishPrice; }


//SETTERS

/** Updates name of dish.
@param width: the updated dish name
*/

public void SetName(string dishName) { this.dishName = dishName; }

/** Updates the type of dish.
@param width: the updated dish type
*/
public void SetType(int dishType) { this.dishType = dishType; }

/** Updates the type of dish.
@param width: the updated dish type
*/

public void SetPrice(string dishPrice) { this.dishPrice = dishPrice; }

/** Customizes the output format of the menu.
@return: a string representing the output format of the menu
*/

public override string ToString() { return string.Format(dishName + "(" + dishPrice + ")", GetName(), GetPrice()); }

}
Pobiega
Pobiega8mo ago
There we go.
Dinny
Dinny8mo ago
is this better ok cool
Pobiega
Pobiega8mo ago
Do you see how much easier that is to read?
Dinny
Dinny8mo ago
yes i do
Pobiega
Pobiega8mo ago
👍
Dinny
Dinny8mo ago
but i had got rid of the squiggles now so i think i am good i just need to work on the errors in my input an doutput file now
Pobiega
Pobiega8mo ago
Okay, now with that fixed.. Your getters and setters look good, ignoring the fact that they should be properties this is how you'd write this code in Java
Dinny
Dinny8mo ago
if my prof taught me to do it the way u mentioned i def would have, otherwise he has not shown us that yet he tends to be more difficult than need be honestly
Pobiega
Pobiega8mo ago
Yeah, not much you can do about that. Follow the profs way for now just know that this isnt how modern C# does it
Dinny
Dinny8mo ago
idk why he isn't teaching us the modern way then that's so strange and he's been using c# fo ryears too
Pobiega
Pobiega8mo ago
by modern, I mean since 2003 btw :p
Dinny
Dinny8mo ago
given that info, idk why he's teaching it the way he is if the way u mentioned is how most people do it but if its's right for now, i'll stick to it hopefully he will teach an updated version as class goes on
Pobiega
Pobiega8mo ago
public class Dish
{
public Dish(string name, int type, int price)
{
Name = name;
Type = type;
Price = price;
}
public string Name { get; set; }
public int Type { get; set; }
public int Price { get; set; }
}
public class Dish
{
public Dish(string name, int type, int price)
{
Name = name;
Type = type;
Price = price;
}
public string Name { get; set; }
public int Type { get; set; }
public int Price { get; set; }
}
this is how this would look written with properties
Dinny
Dinny8mo ago
wth that looks so much cleaner
Pobiega
Pobiega8mo ago
mhm
Dinny
Dinny8mo ago
and nicer to look at
Pobiega
Pobiega8mo ago
thats why properties were invented 🙂
Dinny
Dinny8mo ago
bro my prof is so extra this looks way easier
Pobiega
Pobiega8mo ago
wanna know another thing?
Dinny
Dinny8mo ago
yes go for it
Pobiega
Pobiega8mo ago
the way he has you comment your code thats not how we do that either
Dinny
Dinny8mo ago
😭 this is so aggy how do u comment ur code
Pobiega
Pobiega8mo ago
public class Dish
{
/// <summary>
/// Primary constructor for the class. Does something.
/// </summary>
/// <param name="name">blee</param>
/// <param name="type">blaa</param>
/// <param name="price">bloop</param>
public Dish(string name, int type, int price)
{
Name = name;
Type = type;
Price = price;
}
/// <summary>
/// Gets or sets the name of the dish.
/// </summary>
public string Name { get; set; }
public int Type { get; set; }
public int Price { get; set; }
}
public class Dish
{
/// <summary>
/// Primary constructor for the class. Does something.
/// </summary>
/// <param name="name">blee</param>
/// <param name="type">blaa</param>
/// <param name="price">bloop</param>
public Dish(string name, int type, int price)
{
Name = name;
Type = type;
Price = price;
}
/// <summary>
/// Gets or sets the name of the dish.
/// </summary>
public string Name { get; set; }
public int Type { get; set; }
public int Price { get; set; }
}
when commenting a class, method, constructor, property etc, we use ///, aka summary comments and our tooling supports these
Pobiega
Pobiega8mo ago
look at this:
No description
Pobiega
Pobiega8mo ago
when I hover my cursor over the property, I get to see the comment I wrote this is true even when accessing it from another class etc its how the built in documentation works too
Dinny
Dinny8mo ago
he always makes us use docstring and he said he will take off a lot of points if we fail to use them :///// this all is so backwards for me i truly don't understand why he;s teaching it this way
Jimmacle
Jimmacle8mo ago
probably out of touch with current standards
Pobiega
Pobiega8mo ago
I'm sticking to my original opinion that your prof is an old-school C developer who knows Java
Angius
Angius8mo ago
/// are docblocks in C# /** is, what, PHP?
Dinny
Dinny8mo ago
i believe so bc i am pretty sure he teaches java at our college too
Pobiega
Pobiega8mo ago
C, C++ as well.
Dinny
Dinny8mo ago
he has also been at that college for years so i don't doubt it tho i would have chosen a different prof but he is the only one that teaches c# at my college
Pobiega
Pobiega8mo ago
Just follow his crazy ideas for now but be aware that he is many many years out of date
Dinny
Dinny8mo ago
when i switch to a 4 year uni, i feel i'd learn what u guys are telling me it's gonna be so jarring well thank you guys anyways, i truly appreciate u help and patience.
Pobiega
Pobiega8mo ago
not really its mostly style and missing properties
Dinny
Dinny8mo ago
I will have to keep in mind or next time that my prof teaching style is out of datr
Pobiega
Pobiega8mo ago
the core logic will be the same the thinking will be the same
Dinny
Dinny8mo ago
yeah i agree
Pobiega
Pobiega8mo ago
those are the important parts of programming
Dinny
Dinny8mo ago
but it was still shocking to see
Pobiega
Pobiega8mo ago
syntax is easy to re-learn 🙂
Dinny
Dinny8mo ago
i have faith, I'll just have to keep studying and what not thank you for your input i truly appreciat eit it***
Pobiega
Pobiega8mo ago
good luck!
Dinny
Dinny8mo ago
thank u 😄