This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to make a c# program with a main() method holds an array of 8 integers and the second method asks the user for numbers that fill the array.
I'm lost on how to get the second method to pass the integers into the array in the main method.
My hunch is that you don't know how to work with parameters or return values.(How methods communicate together).If it's the case, you should really read about it.I'm going to imply you are using a console application. Look at this :
private void SomeMethod()
{
int[] myArray = new int[8];
for(int i = 0; i < myArray.Lenght - 1; i++)
myArray[i] = GetInputNumber();
}
private int GetInputNumber()
{
return Convert.ToInt32(Console.ReadLine());
}
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've been while out of C# and MVC. And I am really struggling with the following error, I really don't see it. I have a list of restrictions and i want to add their keys to an string[].
int cntr = 0;
//loop through restrictions and add to array
foreach (var Restriction in this.admingroupRepository.Context.AdminRestrictions.ToList())
{
currentRestrictionKeys[cntr] = Restriction.Key;
cntr += 1;
}
This is the error i get on the cntr += 1 line:
Index was outside the bounds of the array.
I don't understand where this comes from, the foreach breaks before the cntr is out of the array's bounds right?
You have allocated too little space for currentRestrictionKeys. But you don't really need to preallocate it at all; you can just use a trivial projection with LINQ:
var currentRestrictionKeys = this.admingroupRepository.Context.AdminRestrictions
.Select(r => r.Key)
.ToArray();
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I need to put a comma before the last char of a string.
For example:
Input: 101919 = Ouput: 10191,9
What is the best way to do that?
if (input.Length > 0) { input = input.Insert(input.Length - 1, ","); }
With the insert method
strTarget = strTarget.Insert( srtrTarget.Length -1, ",");
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 6 string arrays that contain a series of characters. What I would like to be able to do is random pick an array and once its picked afterwards, will produce a final string response. Do I need a list to do this or is there another way?
All of this is just an small array exercise using a random variable.
I should specify that this is a console application.
A simple option would be: Make an array of the arrays. Pick a random index to get one of the arrays. Create a reference to this and then pick each member as needed.
//let say u have an array of string
string[] myarr = new string[] { "str1", "str3", "str3", "str4", "str5", "str6"};
Random rnd = new Random();
// you dont need a list, simply pick one rnd element from array
string myRandomPickedString = myarr[rnd.Next(0, myarr.Length - 1)];
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need to convert a char to an int and back.
Here's what I'm using to convert the char to an int:
(char)65 // Returns 'a'
Here's what I'm TRYING to use to convert it back:
(int)'a' // Returns 97
Any ideas what I can do?
try this,
char x = 'a';
int y = (int)x;
65 is the character code for a capital 'A'. 97 is a lower case 'a'.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What I need is to order IQueriable (the result of LINQ to SQL method) and return first count elements if count is defined. I use the following code:
IOrderedQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0) list = list.Take(count).OrderByDescending(item=>item.Time);
It works fine, but I don't like calling OrderByDescending twice. Can I make it any better without breaking the order and also without double-ordering?
There is absolutely no need for the second OrderByDescending. Simply remove it:
IQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0)
list = list.Take(count);