C
C#14mo 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
Angius14mo ago
$code
MODiX
MODiX14mo 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
Angius14mo ago
They're correct in that they will work But... they're useless C# has properties for that It ain't Java
Jimmacle
Jimmacle14mo ago
yeah you'd never see this in normal C# code you'd just use autoproperties
Pobiega
Pobiega14mo ago
The "constructor" is also uh... Not a constructor. I don't think this even compiled
Jimmacle
Jimmacle14mo ago
looks like it should, the formatting is just messed up
Pobiega
Pobiega14mo ago
GetName is void and returns a string.
Jimmacle
Jimmacle14mo ago
oh i see
Pobiega
Pobiega14mo ago
This does not compile.
Jimmacle
Jimmacle14mo ago
my eyes must automatically block out impossible code
Murten
Murten14mo ago
Not with that attitude. (Also, returns does not work as a substitute for return)
Pobiega
Pobiega14mo ago
this strikes me as (invalid) java code written by someone who learned C at uni
Murten
Murten14mo 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
Pobiega14mo ago
interface 😄 @Dinny please re-join us, we need feedback on our feedback
Jimmacle
Jimmacle14mo 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
Murten14mo ago
I think the getters and setters should be the least of his concern.
Dinny
DinnyOP14mo 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
Jimmacle14mo ago
okay, but this file doesn't compile it's not valid C#
Dinny
DinnyOP14mo ago
well that’s the assignment anyways
Jimmacle
Jimmacle14mo ago
did you try it before asking for help?
Dinny
DinnyOP14mo ago
well yes that’s
Jimmacle
Jimmacle14mo ago
your IDE should be screaming at you
Dinny
DinnyOP14mo ago
why i’m here it’s not that’s why i’m here
Jimmacle
Jimmacle14mo ago
what editor are you using?
Dinny
DinnyOP14mo ago
it’s saying no syntax errors no warnings or anything i’m using visual studios
Jimmacle
Jimmacle14mo ago
either way the compiler will output errors if you try to run the code
Dinny
DinnyOP14mo ago
it’s not outputting anything i’m trying to put things in an output file
Pobiega
Pobiega14mo ago
do you have a project?
Jimmacle
Jimmacle14mo ago
okay, so let's start from the very beginning yes, what pob asked
Dinny
DinnyOP14mo ago
yes
Murten
Murten14mo ago
doubt
Jimmacle
Jimmacle14mo ago
so you have a .sln, .csproj, and one or more .cs files?
Dinny
DinnyOP14mo ago
.cs i have two of those
Jimmacle
Jimmacle14mo ago
that's not a project
Dinny
DinnyOP14mo ago
oh ugh this shii is so hard i’m gonna throw up
Jimmacle
Jimmacle14mo 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
DinnyOP14mo ago
i don’t have that option on VS :/ one second
Pobiega
Pobiega14mo ago
No description
Dinny
DinnyOP14mo ago
i went there i had to go all the way out
Pobiega
Pobiega14mo ago
No description
Dinny
DinnyOP14mo ago
yes i’m using that
Pobiega
Pobiega14mo ago
okay Well, your first file clearly didn't
Dinny
DinnyOP14mo ago
didn’t what
Pobiega
Pobiega14mo ago
have a project file
Dinny
DinnyOP14mo ago
i’m not sure how tho i went through the same steps
Pobiega
Pobiega14mo ago
C# and .NET is entirely project based. You never open a folder or a file directly
Dinny
DinnyOP14mo ago
for both files
Pobiega
Pobiega14mo ago
you open the project file
Jimmacle
Jimmacle14mo ago
can you send a screenshot of your solution explorer window? with your project open
Dinny
DinnyOP14mo ago
one moment
Dinny
DinnyOP14mo ago
sorry the picture would not load for some reason
No description
Dinny
DinnyOP14mo ago
it’s working now tho
Jimmacle
Jimmacle14mo ago
your program or the picture?
Dinny
DinnyOP14mo ago
the picture
Jimmacle
Jimmacle14mo ago
okay, so what happens if you click the run button in the toolbar at the top of VS
Dinny
DinnyOP14mo 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
Jimmacle14mo ago
right now i'm focused on making sure your IDE is working correctly
Dinny
DinnyOP14mo ago
alright
Jimmacle
Jimmacle14mo 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
DinnyOP14mo ago
yes
Jimmacle
Jimmacle14mo ago
alright, seems like it's fine now
Dinny
DinnyOP14mo ago
i see a couple green squiggles as well
Jimmacle
Jimmacle14mo ago
those are just suggestions, they won't stop your code from compiling the red ones are what need to be fixed
Dinny
DinnyOP14mo ago
alright
Pobiega
Pobiega14mo ago
once you've fixed the reds, you might want to take a look at the greens too thou 🙂 but red takes priority
Dinny
DinnyOP14mo ago
ty i will keep that in mind as well omg there’s so much red now
Pobiega
Pobiega14mo 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
DinnyOP14mo 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
Jimmacle14mo ago
professors are teaching people to write get/set methods in C#? Harold
Dinny
DinnyOP14mo 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
Jimmacle14mo ago
my point is what they're teaching you isn't how you should be doing it
Dinny
DinnyOP14mo ago
😨😨😨
Jimmacle
Jimmacle14mo ago
but that tends to happen in CS classes
Dinny
DinnyOP14mo ago
😭😭 we’re u taught differently were *
Jimmacle
Jimmacle14mo ago
i'm 99% self taught
Dinny
DinnyOP14mo ago
😨 then how did u teach urself set and get bc clearly im doing it wrong :/
Jimmacle
Jimmacle14mo ago
dropped out of college when i got a job offer
Dinny
DinnyOP14mo ago
oh wow congrats
Jimmacle
Jimmacle14mo ago
i picked it up at some point
Pobiega
Pobiega14mo ago
same story for me. 99% self taught.
Dinny
DinnyOP14mo ago
dang that’s p cool i feel if i had a different prof i wouldn’t be struggling this hard
Jimmacle
Jimmacle14mo 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
DinnyOP14mo ago
like i’m doing great in my other coding classes
Pobiega
Pobiega14mo ago
the thing you are doing wrong with getset is that C# already has it built in with something called "properties"
Dinny
DinnyOP14mo ago
if that’s the case, idky he’s teaching it this way
Pobiega
Pobiega14mo ago
but your weird usage of class, interface, void and return was the problem thou thats why most of us went "wtf"
Dinny
DinnyOP14mo ago
i’m going wtf 😭
Pobiega
Pobiega14mo 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
DinnyOP14mo ago
his lectures and way of explaining gives me a stroke. i end up having to watch youtube videos after class ugh
Jimmacle
Jimmacle14mo ago
it might help if you try to explain to us why you used some of those keywords in the places you did
Dinny
DinnyOP14mo ago
i have literally no hope for this class this shouldn’t be that hard because my prof did
Jimmacle
Jimmacle14mo ago
i'm skeptical
Dinny
DinnyOP14mo ago
one moment
Jimmacle
Jimmacle14mo ago
i mean, i've had professors give me code that won't work right before but not this bad
Dinny
DinnyOP14mo ago
this is part of his sample code
No description
Dinny
DinnyOP14mo ago
he’s doing a rectangle and i’m reading items from a menu i had to tweak some things tho
Jimmacle
Jimmacle14mo ago
yes, so your professor did not do what you're doing wrong
Dinny
DinnyOP14mo ago
sigh
Murten
Murten14mo ago
Still goofy code though.
Jimmacle
Jimmacle14mo 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
Murten14mo ago
Tell your proffessor to stop using C# 2.0
Dinny
DinnyOP14mo 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
Pobiega14mo ago
$codegif
Pobiega
Pobiega14mo ago
post your updated code using a code block, like in this gif
Dinny
DinnyOP14mo ago
wait how do i put my code in that gif i’ve never done that before
Pobiega
Pobiega14mo ago
just type the characters then paste your code
Dinny
DinnyOP14mo ago
like i type $code gif and then late my code ?
MODiX
MODiX14mo 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
DinnyOP14mo 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
Pobiega14mo ago
You also goofed up the codeblock It seems to me you need to pay more attention to details
Dinny
DinnyOP14mo 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
Pobiega14mo ago
Yeah but look
Dinny
DinnyOP14mo ago
looking
Pobiega
Pobiega14mo 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
DinnyOP14mo ago
i clicked the wrong character and i accidentally hit enter i dont think thats a crime
Pobiega
Pobiega14mo ago
then you fix it? discord supports editing
Dinny
DinnyOP14mo ago
://// ok the embed is there tho now
Pobiega
Pobiega14mo ago
yeah but if you'd fix it, you'd see why we want it done properly
Dinny
DinnyOP14mo ago
i did tho
Pobiega
Pobiega14mo ago
nope
Pobiega
Pobiega14mo ago
No description
Dinny
DinnyOP14mo ago
im getting drilled into bc i left a cs in there or is it somehting else i was just following the gif
Pobiega
Pobiega14mo 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
DinnyOP14mo 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
Pobiega14mo ago
but this is discord, not C#
Dinny
DinnyOP14mo ago
okay dawg i will just fix it
Pobiega
Pobiega14mo ago
please do
Dinny
DinnyOP14mo 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
Pobiega14mo ago
There we go.
Dinny
DinnyOP14mo ago
is this better ok cool
Pobiega
Pobiega14mo ago
Do you see how much easier that is to read?
Dinny
DinnyOP14mo ago
yes i do
Pobiega
Pobiega14mo ago
👍
Dinny
DinnyOP14mo 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
Pobiega14mo 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
DinnyOP14mo 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
Pobiega14mo 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
DinnyOP14mo 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
Pobiega14mo ago
by modern, I mean since 2003 btw :p
Dinny
DinnyOP14mo 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
Pobiega14mo 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
DinnyOP14mo ago
wth that looks so much cleaner
Pobiega
Pobiega14mo ago
mhm
Dinny
DinnyOP14mo ago
and nicer to look at
Pobiega
Pobiega14mo ago
thats why properties were invented 🙂
Dinny
DinnyOP14mo ago
bro my prof is so extra this looks way easier
Pobiega
Pobiega14mo ago
wanna know another thing?
Dinny
DinnyOP14mo ago
yes go for it
Pobiega
Pobiega14mo ago
the way he has you comment your code thats not how we do that either
Dinny
DinnyOP14mo ago
😭 this is so aggy how do u comment ur code
Pobiega
Pobiega14mo 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
Pobiega14mo ago
look at this:
No description
Pobiega
Pobiega14mo 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
DinnyOP14mo 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
Jimmacle14mo ago
probably out of touch with current standards
Pobiega
Pobiega14mo ago
I'm sticking to my original opinion that your prof is an old-school C developer who knows Java
Angius
Angius14mo ago
/// are docblocks in C# /** is, what, PHP?
Dinny
DinnyOP14mo ago
i believe so bc i am pretty sure he teaches java at our college too
Pobiega
Pobiega14mo ago
C, C++ as well.
Dinny
DinnyOP14mo 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
Pobiega14mo ago
Just follow his crazy ideas for now but be aware that he is many many years out of date
Dinny
DinnyOP14mo 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
Pobiega14mo ago
not really its mostly style and missing properties
Dinny
DinnyOP14mo ago
I will have to keep in mind or next time that my prof teaching style is out of datr
Pobiega
Pobiega14mo ago
the core logic will be the same the thinking will be the same
Dinny
DinnyOP14mo ago
yeah i agree
Pobiega
Pobiega14mo ago
those are the important parts of programming
Dinny
DinnyOP14mo ago
but it was still shocking to see
Pobiega
Pobiega14mo ago
syntax is easy to re-learn 🙂
Dinny
DinnyOP14mo 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
Pobiega14mo ago
good luck!
Dinny
DinnyOP14mo ago
thank u 😄
Want results from more Discord servers?
Add your server