C
C#2mo ago
Plonk

Hell

I understand that height and weight are local, any way to make them global?
No description
32 Replies
Marvin
Marvin2mo ago
You can declare them as instance variables so they atleast available in the entire class. And consider sharing code either as text or actual screenshot of the computer.
Plonk
Plonk2mo ago
how do i do that?
Marvin
Marvin2mo ago
you can for example use
C#
public class ClassName
{
private double Height { get; set; }
private double Weight { get; set; }
}
C#
public class ClassName
{
private double Height { get; set; }
private double Weight { get; set; }
}
or if you prefer fields over properties:
C#
public class ClassName
{
private double Height;
private double Weight;
}
C#
public class ClassName
{
private double Height;
private double Weight;
}
Plonk
Plonk2mo ago
Okay and how do i use this to refer to the height snd weight in the button click
Marvin
Marvin2mo ago
you assign the local variables to the instance properties/fields
Plonk
Plonk2mo ago
I apologise but i am confused i dknt understand what the second example is dojng i created the class and now what?
Marvin
Marvin2mo ago
C#
public class ClassName {

public double Height { get; set; }
public double Weight { get; set; }

public void textBox1_Changed() {

double height = Convert.ToDouble("your text");
Height = height / 39.37d;

}

public void textBox2_Changed() {

double weight = Convert.ToDouble("your text");
Weight = weight / 2.205d;

}

public void Third() {

Calculate(Height, Weight); // you can also just use the instance variables inside calculate directly if preferred

}

public void Calculate(double height, double weight) {

}

}
C#
public class ClassName {

public double Height { get; set; }
public double Weight { get; set; }

public void textBox1_Changed() {

double height = Convert.ToDouble("your text");
Height = height / 39.37d;

}

public void textBox2_Changed() {

double weight = Convert.ToDouble("your text");
Weight = weight / 2.205d;

}

public void Third() {

Calculate(Height, Weight); // you can also just use the instance variables inside calculate directly if preferred

}

public void Calculate(double height, double weight) {

}

}
You have to declare them in the class your event handlers are in
Plonk
Plonk2mo ago
Im sorry but i still dont understand ill work it out thanks for your help I did it thank you so much Could you just explain what the Get set means?
Marvin
Marvin2mo ago
thats auto implementing properties. using this shortcut you basically get the following out of the box in comparison of a field backed property:
C#
private double _Weight;
public double Weight {
get {
return _Weight;
}
set {
_Weight = value;
}
}
C#
private double _Weight;
public double Weight {
get {
return _Weight;
}
set {
_Weight = value;
}
}
you can also just do, without the get, set. Might be easier for a beginner
C#
public double Weight;
C#
public double Weight;
Plonk
Plonk2mo ago
However now when i do calculations in the void calculates i get the result of Nan
Plonk
Plonk2mo ago
No description
Marvin
Marvin2mo ago
in your code shown above, you would first need to change text of both text boxes to actually set the variables otherweise weight will be 0, which leads to division by 0
Plonk
Plonk2mo ago
i see so the text inputted in the text box isnt being used for the calculation?
Marvin
Marvin2mo ago
it should, can you show the code now?
Plonk
Plonk2mo ago
which part?
Marvin
Marvin2mo ago
where you set height and width but the entire class
Plonk
Plonk2mo ago
its the same as before i just added the get set part.
Plonk
Plonk2mo ago
No description
Marvin
Marvin2mo ago
see, you never assign them to the isntance variables, in your case you need to do Height = height and Weight = weight; in the corresponding methods.
Plonk
Plonk2mo ago
amazing got jt it works thanks just got some logic errors to take care of
Marvin
Marvin2mo ago
good job
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2mo ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Plonk
Plonk2mo ago
whats the huge deal with a photo?
Mako
Mako2mo ago
- You cannot execute the code in a photo unless you type it out, which is fine if your function is a few lines long, and even then depends on the code complexity - Some people can adjust their font size to read code. You can zoom on a picture but it may get blurry. Then there's the fact that some cameras flash portion of the image, which makes it harder to read for everyone - Photos come across as low effort
Pobiega
Pobiega2mo ago
$screenshot
MODiX
MODiX2mo ago
If you want to share code, please copy and paste the code into a markdown code block in your discord post, or paste the code into https://paste.mod.gg/ and share a link to it in your discord post. If want to share a screenshot, please do not take a photo of your computer screen with your phone or camera. Instead see https://www.take-a-screenshot.org/ for how to take a screen capture on your device. You may want to crop the image before pasting it into your discord message.
Kiel
Kiel2mo ago
I mean to be fair if they are for example using a school computer where discord is blocked or cannot be installed there's literally no way for them to share it outside of a picture
Pobiega
Pobiega2mo ago
No there isn't. You can absolutely still go to a pastebin site or similar Also, please consider that noone is entitled to help. They can ask for it, and 99% do get it - but there is no entitlement and a bare minimum of effort is required to get it.
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server