How to get the first value of a variable in C# without using array [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Main()
{
int X=10;
X=20;
X=30;
Console.WriteLine ("X :"+X);//will print 30.
}
How to get the first value of X without using array.
I want the result as
X=10

Imagine you have a plate X and you put potatoes on it 10
int X=10;
then you remove the potatoes and put salad 20 on it
X=20;
then you remove the salad and put a steak 30 on it
X=30;
and now you ask yourself how do I serve potatoes to your guest. You guest will receive the steak because it is the last value that you put on the plate.
Console.WriteLine ("X :"+X);//will print 30.
I would suggest to use a List. It would be the equivalent of a plate with a memory.
List<int> x_es = new List<int>();
x_es.Add(10);
x_es.Add(20);
x_es.Add(30);
Now you can serve whatever you have placed already before on your plate
x_es[0];
If you really want to avoid array or List you need further plates/variables to store the content temporarily:
int X=10;
int mem_1 = X;
X=20;
int mem_2 = X;
X=30;
Console.WriteLine ("X :"+mem_1);//will print 10.

Related

Why for loop is acting like while loop in C# Arrays? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
this for loop inside arrays is working like while and printing only the second index.
what should i do to make it print only even indexes?
You have inverted the += operator in your for loop. You're saying i = +2 which is the same as i = 2
The correct code would be:
for (int i = 0; i < cars.Length; i += 2)
{
Console.WriteLine(cars[i]);
}

Cant use decimals when defining a value. C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I have these two lines:
Int32 val3 = 8;
Int32 val4 = 0.66;
The first line works, but the second does not. I don't know why, and I don't know how I would go to fix this or what to search for.
Try using
double val4 = 0.66
Int is for numbers without decimal points...
0.66 is not int32, it is float because it has dot (0.66), only natural numbers are Int
simply use
float val4 = 0.66f
or
var val4 = 0.66
C# recognize it correctly as float at compile time

Loop not going inside when the value is same [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
you can see that value is 74 but its not going inside the if why?
Because you have a space in TEMP. " 74". The two values are not equal because the second one TEMP2 is "74" without the space.
Try using Trim on your values to remove whitespace:
string temp = ids[i].Trim();
string temp2 = raditem.Value.ToString().Trim();
Also, always use the .Equals overload for the String class, as you can control the culture for the string comparison. In this case it doesn't matter because they're numerals but it could matter if you want to compare to alphabetical strings where you want "A" to equal "a" and so forth.
Furthermore, if you are sure your array contains only numbers, I could recommend converting the values to numbers before comparing them.
// one way to validate is wrap this in a try-catch and handle input format error.
int temp = Convert.ToInt32(ids[i].Trim());
That way if you have a case where there aren't numbers, you could validate and complain to the user or whatever you want.

how do i display how many digits the number has? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

Byte Array to Double in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a program set up that reads a double value into byte array using C#.
The value is set to 1.0 but the byte array result I get is 63 and 128.
for example:
Byte[] array; // gets result
//result I get...
the 0th value - 63
the 1st value - 128
How do I convert these values back to double (1.0)?
BitConverter.ToDouble(array, 0);
P.S. it will work if you used something like
BitConverter.GetBytes(1.0)
to pack double into array. I can't understand what do you mean by 0th value and 1st value? If your byte array contains only two bytes - it's not double.

Categories

Resources