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)];
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'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());
}
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 am a beginner to C. I know C and I am little new to objects and classes.
I thought to do some exercises using C#. So I want to encode the each character of a string. So I have planned to convert string (login_name) into char[] array and then loop through the each element of char[] array and perfom little manipulation and give some encoded value to the each character. Finally assign these values to another char[] sery and convert back this char[] sery to string.
I have written the basic idea. Take a look at this
char[] array = login_name.ToCharArray(); // Converted string to char array
char[] sery = null; //created a new array.is it neccessary to specify the size?
// convert the characters to int and perfom some opeartion;
// here i have ex-ored with 123
int a =((int)array[0]) ^ 123;
// now convert the encoded value to char and assign it to each element of char[] sery
sery[0] = (char) a; but this statement gives run time error
Gives a run time error: create a new instance of char.
What does it mean?
You need to define the size of the array, otherwise in current form your sery is null
char[] sery = new char[array.Length]; //if the elements number would be same as array
Or you can use List<T>
List<char> seryList = new List<char>();
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 am currently working on a console text analysis program for an assignment.
My problem is, I need to save all user entered words above 7 letters to a text file. The user can enter words by typing in their paragraph or by loading from a text file.
Any ideas on how I can do this ?
Thanks for any help in advance
Without giving you the code, think about what you really need to do.
Read in the entire text.
Split the text based on spaces / punctuation to identify each word. This will be stored in an array.
Test each split string's length.
Write the results to a file.
I'm not sure how you're getting the user-entered words, but you can do a simple LINQ statement to get the words greater than 7 letters:
//get all words into an array (wordArray)
var bigWords = wordArray.Where(w => w.Length > 7).ToArray();
Then you do something with the bigWords array.
I'm not going to do your assignment for you.
You should check functions such as String.Split, String.Length and String.Substring()
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.
Im struggling to get the right syntax to use in c# for the vb function FormatNumber...i will say that 'iPremium' is an object as it returns data from a tableadapter.
the value that 'ipremium' holds is 943.4000 and the idea is to only have two decimal places after the '.', i would hope this is acheivable using the right syntax but unfortunately not being a c# expert, it could take a while to figure this out.
here's the vb code:
iPremium = FormatNumber(iPremium, 2, TriState.True)
any idea's on how this is acheivable?
thanks for any idea's and suggestions and excuse the ignorance if this is not worded correctly
var formattedNumber = iPremium.ToString("0.00");
or, if you wish to round the number, instead of just chopping-off precision:
var formattedNumber = Math.Round(iPremium, 2).ToString("0.00").Dump();
Here's a list of the various formats you can use with ToString: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
var roundedNumber = Math.Round(iPremium, 2);
var formattedNumber = String.Format("{0:#.##}", roundedNumber);
I assume the TableAdapter returns a Datatable which can also be used strongly typed:
// first row as example (add using.System.Linq)
double value = table.AsEnumerable().First().Field<double>("iPremium");
Now you can use String.Format or just ToString with a custom format:
string result = value.ToString("0.00");