C
C#4mo ago
Plonk

Hell

I understand that height and weight are local, any way to make them global?
No description
32 Replies
Marvin
Marvin4mo 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
PlonkOP4mo ago
how do i do that?
Marvin
Marvin4mo 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
PlonkOP4mo ago
Okay and how do i use this to refer to the height snd weight in the button click
Marvin
Marvin4mo ago
you assign the local variables to the instance properties/fields
Plonk
PlonkOP4mo ago
I apologise but i am confused i dknt understand what the second example is dojng i created the class and now what?
Marvin
Marvin4mo 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
PlonkOP4mo 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
Marvin4mo 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
PlonkOP4mo ago
However now when i do calculations in the void calculates i get the result of Nan
Plonk
PlonkOP4mo ago
No description
Marvin
Marvin4mo 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
PlonkOP4mo ago
i see so the text inputted in the text box isnt being used for the calculation?
Marvin
Marvin4mo ago
it should, can you show the code now?
Plonk
PlonkOP4mo ago
which part?
Marvin
Marvin4mo ago
where you set height and width but the entire class
Plonk
PlonkOP4mo ago
its the same as before i just added the get set part.
Plonk
PlonkOP4mo ago
No description
Marvin
Marvin4mo 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
PlonkOP4mo ago
amazing got jt it works thanks just got some logic errors to take care of
Marvin
Marvin4mo ago
good job
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX4mo 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
PlonkOP4mo ago
whats the huge deal with a photo?
Mako
Mako4mo 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
Pobiega4mo ago
$screenshot
MODiX
MODiX4mo 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
Kiel4mo 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
Pobiega4mo 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 User4mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server