How to control the maximum value of variables?How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?Using global variables in a functionHow to declare global variables in Android?How can I get the application's path in a .NET console application?Get int value from enum in C#How to loop through all enum values in C#?How do I use extern to share variables between source files?How do I convert a String to an int in Java?Is there a reason for C#'s reuse of the variable in a foreach?
What kind of environment would favor hermaphroditism in a sentient species over regular, old sexes?
Why is Drogon so much better in battle than Rhaegal and Viserion?
How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5
Non-African Click Languages
Why do academics prefer Mac/Linux?
A person lacking money who shows off a lot
Single word that parallels "Recent" when discussing the near future
What is this rubber on gear cables
How to know the path of a particular software?
Why are lawsuits between the President and Congress not automatically sent to the Supreme Court
Failing students when it might cause them economic ruin
Pedaling at different gear ratios on flat terrain: what's the point?
Cuban Primes
Why did nobody know who the Lord of this region was?
Solenoid fastest possible release - for how long should reversed polarity be applied?
Why doesn't Iron Man's action affect this person in Endgame?
How can I fix the label locations on my tikzcd diagram?
How does this piece of code determine array size without using sizeof( )?
Why aren't satellites disintegrated even though they orbit earth within their Roche Limits?
Can EU citizens work in Iceland?
Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?
Divisor Rich and Poor Numbers
Why can't I share a one use code with anyone else?
What color to choose as "danger" if the main color of my app is red
How to control the maximum value of variables?
How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?Using global variables in a functionHow to declare global variables in Android?How can I get the application's path in a .NET console application?Get int value from enum in C#How to loop through all enum values in C#?How do I use extern to share variables between source files?How do I convert a String to an int in Java?Is there a reason for C#'s reuse of the variable in a foreach?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
As the title says, I would like to set the maximum value of the skill, stam and luck integers to the value of the related *Max integers. The *Max int values are set randomly during the start up of the program and the regular values are changed throughout the running of the program. There may be a few instances where the *Max value gets increased or decreased during play.
public static int skillMax = 0;
public static int stamMax = 0;
public static int luckMax = 0;
public static int skill = skillMax;
public static int stam = stamMax;
public static int luck = luckMax;
As my knowledge of C# is still in its infancy, I have not tried much. However I have searched far and wide on the internet however and not been able to find anything except for the MinValue and MaxValue fields and this piece of code with no explanation:
protected int m_cans;
public int Cans
get return m_cans;
set
m_cans = Math.Min(value, 10);
Thanks in advance for any advice you throw my way!
c# int global-variables console-application
add a comment |
As the title says, I would like to set the maximum value of the skill, stam and luck integers to the value of the related *Max integers. The *Max int values are set randomly during the start up of the program and the regular values are changed throughout the running of the program. There may be a few instances where the *Max value gets increased or decreased during play.
public static int skillMax = 0;
public static int stamMax = 0;
public static int luckMax = 0;
public static int skill = skillMax;
public static int stam = stamMax;
public static int luck = luckMax;
As my knowledge of C# is still in its infancy, I have not tried much. However I have searched far and wide on the internet however and not been able to find anything except for the MinValue and MaxValue fields and this piece of code with no explanation:
protected int m_cans;
public int Cans
get return m_cans;
set
m_cans = Math.Min(value, 10);
Thanks in advance for any advice you throw my way!
c# int global-variables console-application
add a comment |
As the title says, I would like to set the maximum value of the skill, stam and luck integers to the value of the related *Max integers. The *Max int values are set randomly during the start up of the program and the regular values are changed throughout the running of the program. There may be a few instances where the *Max value gets increased or decreased during play.
public static int skillMax = 0;
public static int stamMax = 0;
public static int luckMax = 0;
public static int skill = skillMax;
public static int stam = stamMax;
public static int luck = luckMax;
As my knowledge of C# is still in its infancy, I have not tried much. However I have searched far and wide on the internet however and not been able to find anything except for the MinValue and MaxValue fields and this piece of code with no explanation:
protected int m_cans;
public int Cans
get return m_cans;
set
m_cans = Math.Min(value, 10);
Thanks in advance for any advice you throw my way!
c# int global-variables console-application
As the title says, I would like to set the maximum value of the skill, stam and luck integers to the value of the related *Max integers. The *Max int values are set randomly during the start up of the program and the regular values are changed throughout the running of the program. There may be a few instances where the *Max value gets increased or decreased during play.
public static int skillMax = 0;
public static int stamMax = 0;
public static int luckMax = 0;
public static int skill = skillMax;
public static int stam = stamMax;
public static int luck = luckMax;
As my knowledge of C# is still in its infancy, I have not tried much. However I have searched far and wide on the internet however and not been able to find anything except for the MinValue and MaxValue fields and this piece of code with no explanation:
protected int m_cans;
public int Cans
get return m_cans;
set
m_cans = Math.Min(value, 10);
Thanks in advance for any advice you throw my way!
c# int global-variables console-application
c# int global-variables console-application
edited Mar 23 at 16:14
Olivier Jacot-Descombes
71k1092144
71k1092144
asked Mar 23 at 15:37
AelariAelari
165
165
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Explanation for the code: Cans
is a property. Properties provide controlled access to class or struct fields (variables). They consist of two methods called get
to return a value and set
to assign the value. A property can also have only a getter or only a setter.
The property Cans
stores its value in a so called backing field. Here m_cans
. The setter gets the new value through the keyword value
.
Math.Min(value, 10)
returns the minimum of the two parameters. I.e., for example, if value
is 8, then 8 is assigned to m_cans
. If value
is 12, then 10 is assigned to m_cans
.
You can use this property like this
var obj = new MyCalss(); // Replace by your real class or struct name.
obj.Cans = 20; // Calls the setter with `value` = 20.
int x = obj.Cans; // Calls the getter and returns 10;
Properties help to implement the principle of Information hiding.
You can easily adapt this example your variables. Often class level variables (fields) are prepended with _
to differentiate them from local variables, i.e. variables declared in methods. Properties are written in PascalCase.
private static int _skillMax; // Fields are automatically initialized to the default
// value of their type. For `int` this is `0`.
public static int SkillMax
get return _skillMax;
set
_skillMax = value;
_skill = _skillMax; // Automatically initializes the initial value of Skill.
// At program start up you only need to set `SkillMax`.
private static int _skill;
public static int Skill
get return _skill;
set _skill = Math.Min(value, _skillMax);
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
add a comment |
Create methods to update values
private static void UpdateSkill(int newValue)
skill = newValue;
skillMax = newValue > skillMax ? newValue : skillMax;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315421%2fhow-to-control-the-maximum-value-of-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Explanation for the code: Cans
is a property. Properties provide controlled access to class or struct fields (variables). They consist of two methods called get
to return a value and set
to assign the value. A property can also have only a getter or only a setter.
The property Cans
stores its value in a so called backing field. Here m_cans
. The setter gets the new value through the keyword value
.
Math.Min(value, 10)
returns the minimum of the two parameters. I.e., for example, if value
is 8, then 8 is assigned to m_cans
. If value
is 12, then 10 is assigned to m_cans
.
You can use this property like this
var obj = new MyCalss(); // Replace by your real class or struct name.
obj.Cans = 20; // Calls the setter with `value` = 20.
int x = obj.Cans; // Calls the getter and returns 10;
Properties help to implement the principle of Information hiding.
You can easily adapt this example your variables. Often class level variables (fields) are prepended with _
to differentiate them from local variables, i.e. variables declared in methods. Properties are written in PascalCase.
private static int _skillMax; // Fields are automatically initialized to the default
// value of their type. For `int` this is `0`.
public static int SkillMax
get return _skillMax;
set
_skillMax = value;
_skill = _skillMax; // Automatically initializes the initial value of Skill.
// At program start up you only need to set `SkillMax`.
private static int _skill;
public static int Skill
get return _skill;
set _skill = Math.Min(value, _skillMax);
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
add a comment |
Explanation for the code: Cans
is a property. Properties provide controlled access to class or struct fields (variables). They consist of two methods called get
to return a value and set
to assign the value. A property can also have only a getter or only a setter.
The property Cans
stores its value in a so called backing field. Here m_cans
. The setter gets the new value through the keyword value
.
Math.Min(value, 10)
returns the minimum of the two parameters. I.e., for example, if value
is 8, then 8 is assigned to m_cans
. If value
is 12, then 10 is assigned to m_cans
.
You can use this property like this
var obj = new MyCalss(); // Replace by your real class or struct name.
obj.Cans = 20; // Calls the setter with `value` = 20.
int x = obj.Cans; // Calls the getter and returns 10;
Properties help to implement the principle of Information hiding.
You can easily adapt this example your variables. Often class level variables (fields) are prepended with _
to differentiate them from local variables, i.e. variables declared in methods. Properties are written in PascalCase.
private static int _skillMax; // Fields are automatically initialized to the default
// value of their type. For `int` this is `0`.
public static int SkillMax
get return _skillMax;
set
_skillMax = value;
_skill = _skillMax; // Automatically initializes the initial value of Skill.
// At program start up you only need to set `SkillMax`.
private static int _skill;
public static int Skill
get return _skill;
set _skill = Math.Min(value, _skillMax);
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
add a comment |
Explanation for the code: Cans
is a property. Properties provide controlled access to class or struct fields (variables). They consist of two methods called get
to return a value and set
to assign the value. A property can also have only a getter or only a setter.
The property Cans
stores its value in a so called backing field. Here m_cans
. The setter gets the new value through the keyword value
.
Math.Min(value, 10)
returns the minimum of the two parameters. I.e., for example, if value
is 8, then 8 is assigned to m_cans
. If value
is 12, then 10 is assigned to m_cans
.
You can use this property like this
var obj = new MyCalss(); // Replace by your real class or struct name.
obj.Cans = 20; // Calls the setter with `value` = 20.
int x = obj.Cans; // Calls the getter and returns 10;
Properties help to implement the principle of Information hiding.
You can easily adapt this example your variables. Often class level variables (fields) are prepended with _
to differentiate them from local variables, i.e. variables declared in methods. Properties are written in PascalCase.
private static int _skillMax; // Fields are automatically initialized to the default
// value of their type. For `int` this is `0`.
public static int SkillMax
get return _skillMax;
set
_skillMax = value;
_skill = _skillMax; // Automatically initializes the initial value of Skill.
// At program start up you only need to set `SkillMax`.
private static int _skill;
public static int Skill
get return _skill;
set _skill = Math.Min(value, _skillMax);
Explanation for the code: Cans
is a property. Properties provide controlled access to class or struct fields (variables). They consist of two methods called get
to return a value and set
to assign the value. A property can also have only a getter or only a setter.
The property Cans
stores its value in a so called backing field. Here m_cans
. The setter gets the new value through the keyword value
.
Math.Min(value, 10)
returns the minimum of the two parameters. I.e., for example, if value
is 8, then 8 is assigned to m_cans
. If value
is 12, then 10 is assigned to m_cans
.
You can use this property like this
var obj = new MyCalss(); // Replace by your real class or struct name.
obj.Cans = 20; // Calls the setter with `value` = 20.
int x = obj.Cans; // Calls the getter and returns 10;
Properties help to implement the principle of Information hiding.
You can easily adapt this example your variables. Often class level variables (fields) are prepended with _
to differentiate them from local variables, i.e. variables declared in methods. Properties are written in PascalCase.
private static int _skillMax; // Fields are automatically initialized to the default
// value of their type. For `int` this is `0`.
public static int SkillMax
get return _skillMax;
set
_skillMax = value;
_skill = _skillMax; // Automatically initializes the initial value of Skill.
// At program start up you only need to set `SkillMax`.
private static int _skill;
public static int Skill
get return _skill;
set _skill = Math.Min(value, _skillMax);
edited Mar 23 at 20:01
answered Mar 23 at 15:50
Olivier Jacot-DescombesOlivier Jacot-Descombes
71k1092144
71k1092144
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
add a comment |
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
Took me a moment to understand how to implement it correctly. But once I did, it has worked perfectly! Thank you so much!
– Aelari
Mar 24 at 4:38
add a comment |
Create methods to update values
private static void UpdateSkill(int newValue)
skill = newValue;
skillMax = newValue > skillMax ? newValue : skillMax;
add a comment |
Create methods to update values
private static void UpdateSkill(int newValue)
skill = newValue;
skillMax = newValue > skillMax ? newValue : skillMax;
add a comment |
Create methods to update values
private static void UpdateSkill(int newValue)
skill = newValue;
skillMax = newValue > skillMax ? newValue : skillMax;
Create methods to update values
private static void UpdateSkill(int newValue)
skill = newValue;
skillMax = newValue > skillMax ? newValue : skillMax;
answered Mar 23 at 16:06
Dimple GoyalDimple Goyal
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315421%2fhow-to-control-the-maximum-value-of-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown