It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to increment a integer variable itself in C#?
i need something the variables as
integer1
integer2
integer3
integer4
integer5
In a for loop when it is looped i want the variables gets incremented. How can i get?
Dynamic identifier names are not possible in C# and any other .NET language.
You can use an array or generic list instead - these will hold a collection of your type and allow you to iterate over it.
var intArr = new int[5];
var intList = new List<int>(5);
foreach(var num in intList)
{
// do something with num
}
What you really want is an array:
var ints = new int[5]; // declares an array of five integers, ints[0] to ints[4]
for (int i = 0; i < 5; i++) {
// do something with ints[i]
}
For a more in-depth introduction, have a look at the following tutorial on MSDN. It's for an old C# version, but the concepts still apply:
Arrays Tutorial
You can't change variable names dynamically in C#.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am creating an own generic collection class in C#, and Im working on an array that stores a list that should have an predefined length (It should start with a length of 2). Now I have no idea on how to make it start with a length of 2. For example, if I create an array:
private int[] numbers = new int[20];
How do I predefined it length so it starts with a length of 2?
I see no reason to do that. However you can create arrays with length 2 ->
private int[] numbers = new int[2];.
Also, you can use List<T> to do just that and forget about memory management? :-)
Just use one of the build in classes. They will hadle everything for you avoiding errors.
List<int> numbers = new List<int>();
numbers.Add(123);
int i = numbers[0];
int[] asArray = numbers.ToArray();
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is there a way to reset these variables quicker like at the time of declaration?
Declaration:
int numa1, numa2, numa3, numd1, numd2, numd3;
Reset afterwards:
numa1 = 0;
.
.
.
numd3 = 0;
Because I will change these variables, but then I need to reset them as 0, OK?
Replying the comments below:
Sorry, I didn't change "Reset afterward" to "Initializes afterward". Someone else did that.
Sorry, but you cannot change the values of the variables like:
numa1, numa2, numa3, numd1, numd2, numd3 = 0;
I tried and I received Error 1, 2, 4.
By quicker if you mean faster, I donot think there is any better alternative.
If you mean reducing some lines,
You can choose either
int numa1=0, numa2=0, numa3=0, numd1=0, numd2=0, numd3 = 0;
or
int numa1, numa2, numa3, numd1, numd2, numd3 = 0;
numa1= numa2= numa3= numd1= numd2= numd3;
I guess that you should have used two arrays, I never had a function that used so many variables with such names (sequential numbers).
If you'd use arrays, Your code will look like this :
int[] numa = new [] {0,0,0,0};
int[] numd = new [] {0,0,0,0};
But this is up to you.
For more information about arrays
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I round up values like this:
1.001 => 2
3.3 => 4
Means if the number has fractional part than i want the smallest integer number greater than the number ?
I used Math.Ceiling() but is not helping. How can i do this ?
Math.Ceiling will work. can you tell what its not working ? in term of any errors or returned result.
var returnVal=Math.Ceiling(yourValue);
Use Math.Ceiling() method.
Returns the smallest integer greater than or equal to the specified
number.
var i = Math.Ceiling(1.001);
var j = Math.Ceiling(3.3);
Console.WriteLine(i);
Console.WriteLine(j);
Output:
2
4
Math.Ceiling(value);
Should work.
double x;
x = Math.Ceiling(5.2) ;//Result; 6
x = Math.Ceiling(5.7) ;//Result; 6
x = Math.Ceiling(-5.2) ;//Result;-5
x = Math.Ceiling(-5.7) ;//Result;-5
This is a simple example. How can't you use it? Maybe you miss to assign a variable to
Math.Ceiling();
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have this JavaScript file that I need to write in C#. I've done many but stuck on it.
Could some body help me with this?
var biginteger=new bigint(convert);
var mod = bigint_mod(biginteger, new bigint(97));
var y = bigint_number(mod);
I am unable to interpret these JavaScript functions.
using System.Numerics;
BigInteger dividend= new BigInteger(1222222);
BigInteger mod;
BigInteger.DivRem(dividend, new BigInteger(97), out mod);
//mod will have the result
Or
BigInteger dividend = BigInteger.Parse("3243434343434434");
BigInteger divisor = BigInteger.Parse("97");
BigInteger mod;
BigInteger.DivRem(dividend,divisor, out mod);
First you need a reference to the assembly System.Numerics.dll which is part of the BCL since 2010 (.NET 4.0 and later).
The use something like:
using System.Numerics
...
var bigInteger = (BigInteger)convert; // or BigInteger.Parse(convert); not sure what type convert is
var y = bigInteger % 97;
Surely, the BigInteger struct overloads the % operator. And the literal 97 will be implicitly converted to a BigInteger.
If you don't need a new variable, you can also say: bigInteger %= 97;.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
txt file that contains several columns of numbers, every column is saved in a double array, what I want to do is get the average of a specific column, but to do that I have to convert the array to a list and then began to calculate. I have this code so far:
List<double> 1 = new List<double>(NumSepaERG);
List<double> 12 = NumSepaERG.ToList();
But i get and error of Invalid Expression term double
Variable names can't start with a numeric character. Change to something like:
List<double> list1 = new List<double>(NumSepaERG);
but you can compute an average using Linq without converting to a list:
double average = NumSepaERG.Average();
If NumSepaERG is a jagged array (an array of arrays), the syntax would be:
double average = NumSepaERG[i].Average();
where i is between 0 and the number of arrays - 1;
I think it's a syntax error, you can't have numbers as variable names. You actually don't even need to make it a list.
double average = NumSepERG.Average();