C# Array declaration with commas [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Why can't I just re-declare an array with commas?
static void Main(string[] args)
{
short[] arr = new short[6] { 1,1,1,1,1,1 };
if(1)
{
arr = {1,0,0,1,1,0}; // this line doesn't work
}
}

The initialization expression is not {1,0,0,1,1,0}
The initialization expression must be new short[6] { 1,1,1,1,1,1 }
So, essentially, the statement of your question is the answer to your question.

This syntax : short[] arr = {1, 0, 0, 1, 1, 0};
is called array initialization syntax and it works only in declaration.
why ?
As the guy here wrote, it just how Microsoft guys choose to implement for some reason.

Related

Search Array and get substring result [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 2 years ago.
Improve this question
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.
Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));

C# Defining the size of an array contained in an arraylist [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 6 years ago.
Improve this question
I define the arraylist on my form2, sending it using the constructor to form 3, where it is filled. However, I want the internal array's size to be user defined.
How would i go about doing this?
Looks like this for now, but it doesn't work.
private void bCapturar2_Click(object sender, EventArgs e)
{
int k=0;
k=int.Parse(textBox1.Text);
((paciente)Datos[i]).num_asist = k;
lAsistentes.Visible = true;
tbNom_Asist.Visible = true;
((paciente)Datos[i]).asistentes = new string[((paciente)Datos[i]).num_asist];
bCapturar2.Visible = false;
}
You can set the capacity for your ArrayList on declaration
var tenItemArrayList = new ArrayList(10);
If asistentes is an ArrayList, you still can change the value for capacity that way...
((paciente)Datos[i]).asistentes.Capacity = ((paciente)Datos[i]).num_asist;
However the new capacity cannot be less than the current. Otherwise you'll get an exception.

Concatenate elements of a list in c# [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 wanted to try some examples in C# using collections and generics but I am stuck at this example.
This is my code in c#,
public List<string> CurrentCount(int week, int year)
{
List<string> lst = new List<string>();
lst.Add("current:");
lst.Add("10");
lst.Add("target:");
lst.Add("15");
return lst;
}
It gives me result like this :
["current:","10","target:","15"]
But I want it like this :
["current": 10,"target": 15] or
["current": "10","target": "15"]
Any ideas will be helpful.
Thanks.
You want a Dictionary, not a List.
var dict = new Dictionary<string, int>();
dict.Add("current", 10);
dict.Add("target", 15);

Cannot apply indexing with [] to an expression of type? [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 have got this error
Error 3 Cannot apply indexing with [] to an expression of type
my problem here
if (client["banhours"] == 0)
{
client["banhours"] = -1;
client["banreason"] = "Infinite time.";
client["banstamp"] = DateTime.Now.AddYears(100);
}
if (Account.State == Database.AccountTable.AccountState.Banned)
{
if (client["banhours"] != -1)
{
DateTime banStamp = client["banstamp"];
if (DateTime.Now > banStamp.AddDays(((int)client["banhours"]) / 24).AddHours(((int)client["banhours"]) % 24))
Account.State = Database.AccountTable.AccountState.Player;
}
}
client is >>>
Client.GameClient client;
Have you tried client.banhours or client.banreason?
If Client.GameClient is a class and those are properties or fields, they must not be accessed like an array or dictionary.

C# Multi Spintax - Spin Text [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 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

Categories

Resources