How to iterate through variables by reference c#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
In one class I have a bunch of class variables that hold the position of game pieces. After a move I need to iterate through those variables and change each of them.
The problem I ran into was that by simply making a list and adding these variables, their values get passed into the list and changing the list elements does not change the variables.
int _var1;
int _var2;
int _var3;
List<int> varList = new List<int>() { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i]++;
}
Something like this does not work.
I feel like I'm missing some really simple oop concepts here but I can't think of how to iterate through all these variables without doing something more complicated than feels necessary for just one method.
c# list oop reference
add a comment |
In one class I have a bunch of class variables that hold the position of game pieces. After a move I need to iterate through those variables and change each of them.
The problem I ran into was that by simply making a list and adding these variables, their values get passed into the list and changing the list elements does not change the variables.
int _var1;
int _var2;
int _var3;
List<int> varList = new List<int>() { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i]++;
}
Something like this does not work.
I feel like I'm missing some really simple oop concepts here but I can't think of how to iterate through all these variables without doing something more complicated than feels necessary for just one method.
c# list oop reference
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
You say these variables are inside a class right? In that case make aList<YourClass>and interate through that instead.
– Loocid
Nov 23 '18 at 6:21
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38
add a comment |
In one class I have a bunch of class variables that hold the position of game pieces. After a move I need to iterate through those variables and change each of them.
The problem I ran into was that by simply making a list and adding these variables, their values get passed into the list and changing the list elements does not change the variables.
int _var1;
int _var2;
int _var3;
List<int> varList = new List<int>() { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i]++;
}
Something like this does not work.
I feel like I'm missing some really simple oop concepts here but I can't think of how to iterate through all these variables without doing something more complicated than feels necessary for just one method.
c# list oop reference
In one class I have a bunch of class variables that hold the position of game pieces. After a move I need to iterate through those variables and change each of them.
The problem I ran into was that by simply making a list and adding these variables, their values get passed into the list and changing the list elements does not change the variables.
int _var1;
int _var2;
int _var3;
List<int> varList = new List<int>() { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i]++;
}
Something like this does not work.
I feel like I'm missing some really simple oop concepts here but I can't think of how to iterate through all these variables without doing something more complicated than feels necessary for just one method.
c# list oop reference
c# list oop reference
asked Nov 23 '18 at 6:13
Grant WaltonGrant Walton
1014
1014
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
You say these variables are inside a class right? In that case make aList<YourClass>and interate through that instead.
– Loocid
Nov 23 '18 at 6:21
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38
add a comment |
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
You say these variables are inside a class right? In that case make aList<YourClass>and interate through that instead.
– Loocid
Nov 23 '18 at 6:21
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
You say these variables are inside a class right? In that case make a
List<YourClass> and interate through that instead.– Loocid
Nov 23 '18 at 6:21
You say these variables are inside a class right? In that case make a
List<YourClass> and interate through that instead.– Loocid
Nov 23 '18 at 6:21
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38
add a comment |
3 Answers
3
active
oldest
votes
Value types are copied when you add them to List. List allocates different memory.
Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.
If you want to pass them by reference you need to wrap it in object:
public class ValueStorage
{
public int Value { get; set; }
}
class Program
{
static void Main(string args)
{
var _var1 = new ValueStorage() { Value = 0 };
var _var2 = new ValueStorage() { Value = 1 };
var _var3 = new ValueStorage() { Value = 2 };
var varList = new List<ValueStorage> { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i].Value++;
}
}
}
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
add a comment |
you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.
something like this.
List<MyClass> obj_list = get_the_list();
foreach( MyClass obj in obj_list ){ obj.property = 42;}
add a comment |
This is just how value types work. Boxing wont help either
From the specs 1.3 Types and Variables
When a value of a value type is converted to type object, an object
instance, also called a “box,” is allocated to hold the value, and the
value is copied into that box. Conversely, when an object reference is
cast to a value type, a check is made that the referenced object is a
box of the correct value type, and, if the check succeeds, the value
in the box is copied out.
- You will need to store them in a reference type
- or another trivial way is to use an array of pointers (
int*). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type
Example
private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;
...
var varList = new { &_var1, &_var2, &_var3 };
...
foreach (var value in varList)
*value += 1;
Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);
Output
2
3
4
Additional Resources
Value Types (C# Reference)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
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%2f53441462%2fhow-to-iterate-through-variables-by-reference-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Value types are copied when you add them to List. List allocates different memory.
Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.
If you want to pass them by reference you need to wrap it in object:
public class ValueStorage
{
public int Value { get; set; }
}
class Program
{
static void Main(string args)
{
var _var1 = new ValueStorage() { Value = 0 };
var _var2 = new ValueStorage() { Value = 1 };
var _var3 = new ValueStorage() { Value = 2 };
var varList = new List<ValueStorage> { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i].Value++;
}
}
}
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
add a comment |
Value types are copied when you add them to List. List allocates different memory.
Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.
If you want to pass them by reference you need to wrap it in object:
public class ValueStorage
{
public int Value { get; set; }
}
class Program
{
static void Main(string args)
{
var _var1 = new ValueStorage() { Value = 0 };
var _var2 = new ValueStorage() { Value = 1 };
var _var3 = new ValueStorage() { Value = 2 };
var varList = new List<ValueStorage> { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i].Value++;
}
}
}
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
add a comment |
Value types are copied when you add them to List. List allocates different memory.
Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.
If you want to pass them by reference you need to wrap it in object:
public class ValueStorage
{
public int Value { get; set; }
}
class Program
{
static void Main(string args)
{
var _var1 = new ValueStorage() { Value = 0 };
var _var2 = new ValueStorage() { Value = 1 };
var _var3 = new ValueStorage() { Value = 2 };
var varList = new List<ValueStorage> { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i].Value++;
}
}
}
Value types are copied when you add them to List. List allocates different memory.
Even though it's better not to have side effects, function should return Tuple of values and you set them explicitly.
If you want to pass them by reference you need to wrap it in object:
public class ValueStorage
{
public int Value { get; set; }
}
class Program
{
static void Main(string args)
{
var _var1 = new ValueStorage() { Value = 0 };
var _var2 = new ValueStorage() { Value = 1 };
var _var3 = new ValueStorage() { Value = 2 };
var varList = new List<ValueStorage> { _var1, _var2, _var3 };
for (int i = 0; i < varList.Count; i++)
{
varList[i].Value++;
}
}
}
edited Nov 23 '18 at 6:38
answered Nov 23 '18 at 6:34
Access DeniedAccess Denied
5,13921844
5,13921844
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
add a comment |
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
@SimplyGed you are right, fixed this part. Thank you for your comment.
– Access Denied
Nov 23 '18 at 6:41
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
No problem. I'll delete my comment as it no longer makes sense :-)
– Simply Ged
Nov 23 '18 at 6:50
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen Maybe I'm missing something but I have no clue how to do the same without pointers unsafe code and type casts. If you know better way post it as an answer and I will upvote it.
– Access Denied
Nov 23 '18 at 7:07
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
@BrianRasmussen boxing wont help, unboxing creates another copy
– TheGeneral
Nov 23 '18 at 7:13
add a comment |
you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.
something like this.
List<MyClass> obj_list = get_the_list();
foreach( MyClass obj in obj_list ){ obj.property = 42;}
add a comment |
you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.
something like this.
List<MyClass> obj_list = get_the_list();
foreach( MyClass obj in obj_list ){ obj.property = 42;}
add a comment |
you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.
something like this.
List<MyClass> obj_list = get_the_list();
foreach( MyClass obj in obj_list ){ obj.property = 42;}
you can make a class and put your Var1,Var2 and Var3 in it and initialize and object and call them in a loop.
something like this.
List<MyClass> obj_list = get_the_list();
foreach( MyClass obj in obj_list ){ obj.property = 42;}
edited Nov 23 '18 at 7:12
TheGeneral
38.4k84573
38.4k84573
answered Nov 23 '18 at 6:40
Arsalan Ali ShahArsalan Ali Shah
9
9
add a comment |
add a comment |
This is just how value types work. Boxing wont help either
From the specs 1.3 Types and Variables
When a value of a value type is converted to type object, an object
instance, also called a “box,” is allocated to hold the value, and the
value is copied into that box. Conversely, when an object reference is
cast to a value type, a check is made that the referenced object is a
box of the correct value type, and, if the check succeeds, the value
in the box is copied out.
- You will need to store them in a reference type
- or another trivial way is to use an array of pointers (
int*). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type
Example
private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;
...
var varList = new { &_var1, &_var2, &_var3 };
...
foreach (var value in varList)
*value += 1;
Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);
Output
2
3
4
Additional Resources
Value Types (C# Reference)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
add a comment |
This is just how value types work. Boxing wont help either
From the specs 1.3 Types and Variables
When a value of a value type is converted to type object, an object
instance, also called a “box,” is allocated to hold the value, and the
value is copied into that box. Conversely, when an object reference is
cast to a value type, a check is made that the referenced object is a
box of the correct value type, and, if the check succeeds, the value
in the box is copied out.
- You will need to store them in a reference type
- or another trivial way is to use an array of pointers (
int*). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type
Example
private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;
...
var varList = new { &_var1, &_var2, &_var3 };
...
foreach (var value in varList)
*value += 1;
Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);
Output
2
3
4
Additional Resources
Value Types (C# Reference)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
add a comment |
This is just how value types work. Boxing wont help either
From the specs 1.3 Types and Variables
When a value of a value type is converted to type object, an object
instance, also called a “box,” is allocated to hold the value, and the
value is copied into that box. Conversely, when an object reference is
cast to a value type, a check is made that the referenced object is a
box of the correct value type, and, if the check succeeds, the value
in the box is copied out.
- You will need to store them in a reference type
- or another trivial way is to use an array of pointers (
int*). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type
Example
private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;
...
var varList = new { &_var1, &_var2, &_var3 };
...
foreach (var value in varList)
*value += 1;
Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);
Output
2
3
4
Additional Resources
Value Types (C# Reference)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
This is just how value types work. Boxing wont help either
From the specs 1.3 Types and Variables
When a value of a value type is converted to type object, an object
instance, also called a “box,” is allocated to hold the value, and the
value is copied into that box. Conversely, when an object reference is
cast to a value type, a check is made that the referenced object is a
box of the correct value type, and, if the check succeeds, the value
in the box is copied out.
- You will need to store them in a reference type
- or another trivial way is to use an array of pointers (
int*). (This is assuming an array is suitable), this would allow functional transformations without the need to encase them in another type
Example
private int _var1 = 1;
private int _var2 = 2;
private int _var3 = 3;
...
var varList = new { &_var1, &_var2, &_var3 };
...
foreach (var value in varList)
*value += 1;
Console.WriteLine(_var1);
Console.WriteLine(_var2);
Console.WriteLine(_var3);
Output
2
3
4
Additional Resources
Value Types (C# Reference)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
edited Nov 23 '18 at 7:16
answered Nov 23 '18 at 6:51
TheGeneralTheGeneral
38.4k84573
38.4k84573
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%2f53441462%2fhow-to-iterate-through-variables-by-reference-c-sharp%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
Why do you want to iterate and set the values of the variables? Variables in your class will always be fixed.
– Ipsit Gaur
Nov 23 '18 at 6:17
You say these variables are inside a class right? In that case make a
List<YourClass>and interate through that instead.– Loocid
Nov 23 '18 at 6:21
First of all, it won't allow to use unassigned variables in List. Why you want do this unnecessary stuff?
– Gaurang Dave
Nov 23 '18 at 6:27
@Loocid I tried to execute this code and VS shows me some red lines with error "Use of unassigned variable _var1"
– Gaurang Dave
Nov 23 '18 at 6:31
@GaurangDave Sorry, you're right... Don't know what I was thinking of then.
– Loocid
Nov 23 '18 at 6:38