How to access variables declared inside a loop in c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In the below code i have declared an and then assigned it to z. [Please note this is a duplicate]
double an;
for(double z= Amt+Intrest; z>0; z-=MonthlyInstallment)
{
an = z;
}
I want to access the an variable in the below loop.
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment - MonthlyIntrest,//use the an variable here);
}

You cannot relate the two loops directly, because I assume that those have equal iterations.
A suggestion: You could calculate the value a the loop.
// start with the base value.
double an = Amt + Intrest;
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment -MonthlyIntrest, an);
// decrease an with the MonthlyInstallment
an = an - MonthlyInstallment;
}

Related

is a logic to divide a number randomly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Does anyone know a logic to divide a number randomly? I need to do this in C#, but it might just be logic here.
Example:
I want to split 10 = 3 + 4 + 1 + 2
I want to split 30 = 2 + 5 + 8 + 4 + 6 + 5
Pseudo Code:
myNumber = 30; //Number to split
int maximumValue = myNumber; //Maximum value we can get with rand()
int totalValue = 0; //Save our total
while(totalValue < myNUmber)
{
newSplitNumber = rand(0, maximumValue); //Use as needed
totalValue+= newSplitNumber; //Update our total
maximumValue = myNumber - totalValue; //Update our maximum on next iteration
}

Can we add two integer variable names in C# rather than values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c # i'm trying to add two different integer name, is it possible
int variablename;
for(int i=0;i<10;i++)
{
Objectname.variablename+i
}
No, you cant. C# is strongly typed programming language. What you can do, is to use Dictionary for example, like this:
Dictionary<string, int> names = new Dictionary<string,int>();
for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
If I understand you correctly you want to get the name of your variable and then change the name for each instance based on the iteration, thus you want to add a numeric value to the name. You can achieve this as follows:
string testVariable = "";
int i = 1;
// Get the name of your variable. This will result in "testVariable" as value.
string nameOfTestVariable = nameof(testVariable);
// Now add your integer to the name, but add it as a string.
nameOfTestVariable += i.ToString();
//Result, value of nameOfTestVariable: "testVariable1"
To get the datatype of your variable instead:
int a = 0;
int b = 1;
string dataTypesOfVariables = a.GetType().Name;
dataTypesOfVariables += b.GetType().Name;
//Result, value of dataTypesOfVariables: "Int32Int32"

Detecting array change in linq [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Getting a byte array like this [0,0,0,0,0,1,1,1,1,3,3,3,3,0,0,0,0,0]
Does anyone know how to detect a change from 1 to 3 efficiently in linq?
Why Linq? you could achieve this with simple loop.
int previous = array[0];
for(int i=1;i< array.Length;i++)
{
if(Math.Abs(array[i]- previous) > 1) // use appropriate jump
{
//logic
}
previous = array[i];
}
If you are looking for Linq solution, you could do this.
int previous = array[0];
array.FirstOrDefault(x=>
{
var retValue = Math.Abs(x- previous) > 1;
previous = x;
return retValue;
});
If you want to find all change-indexes in the most efficient way:
List<int> changeIndexes = new List<int>();
for(int i = 1; i < array.Length; i++)
if(array[i] != array[i-1])
changeIndexes.Add(i);
Linq is not the best tool when it comes to indexes.
If you want to find only the index where 1 changes to 3 modify the condition accordingly:
if(array[i] == 3 && array[i-1] == 1) ... // break the loop if you only want to find the first

Can't add double values to list dynamically, System.ArgumentOutOfRangeException [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I always getting this following error:
'System.ArgumentOutOfRangeException'
I am receiving double inputs dynamically from a PLC. I am not able to save the inputs inside a list. I dont know why. can anyone help?
string str = stringArray[1];
double value = double.Parse(str, CultureInfo.InvariantCulture);
List<double> list = new List<double>();
List<double> result = new List<double>();
while (true)
{
int i;
for (i = 1; i < 3 - 1; ++i)
{
list.Add(value);
result[i] = (list[i - 1] + list[i] + list[i + 1]) / 3; //The error is here
dataHub.ServerTemp(result);
}
}
for (i = 1; i < 3 - 1; ++i)
is essentially the same as i = 1 (start with 1, but don't go as far as 2)
So what your code is trying to do is:
Add a double (value) to list list
Update the second item in list result (first problem, result is initially empty) with the average of first, second and third item in list (second problem - list has one element).
result[i] does not yet exist. That is why you are getting the out of range error.

Autonumber with alternating number and letters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want an auto-numbering class in c# which would generate numbers of 8 digit length in the following format i.e. 1A2B3C4D..one number followed by one letter.Any suggestions??
Pseudocode for generating such string:
String result = "";
for ( int i = 0; i < 8 ; i++)
{
if ( i % 2 == 0)
{
// random(a,b) returns random value between or equal to a-b
result.append(random(0,9).toString());
}
else
{
result.append(random(65,90).toChar()); // Generating a random value between 65-90 (A-Z in ascii)
}
}
Edit:
Or as Sayse suggested:
String result = "";
for (int i = 0; i< 4; i++)
{
result.append(random(0,9).toString());
result.append(random(65-90).toChar());
}

Categories