creating a default string in my string List - c#

So, I'm working with c# and I'm trying to create a Bar graph in the console using strings. So I created a 2D list and gave each slot a default "empty" image.
List<List<string>> chart = new List<List<string>>() {"| |"};
but when i wrote it out, i got these two error messages in Visual studios
Error 1 Argument 1: cannot convert from 'string' to 'System.Collections.Generic.List<string>'
Error 2 The best overloaded Add method 'System.Collections.Generic.List<System.Collections.Generic.List<string>>.Add(System.Collections.Generic.List<string>)' for the collection initializer has some invalid arguments

The right syntax is
List<List<string>> chart = new List<List<string>>() { new List<String>() { "| |" }};
since you're creating list of list of string (2D list as you've put it), not just list of string.

When u need to create default string in your List try this:
List<string> chart = new List<string>() {"| |"};
or if u need initialize default string in your List of List<string> try
List<List<string>> chart = new List<List<string>>() { new List<string> { "| |" } };

You need to add List as parameter
List<List<string>> chart = new List<List<string>>() { new List<string>{"| |","| |"}};

Related

How to generate a random list of strings from an array

I want to generate a random list of 5 string values from an array of string.
type options. I have an string[] called 'Items':
private static string[] Items = new[]
{
"Widgets", "Wotsits", "Grommits"
};
Using the options in this array, I want to instantiate a List<string> collection with 5 random strings. I am trying to do it like this:
public List<string> List()
{
var r = new Random();
return Enumerable.Range(1, 5).Select(index => new List<string>()
{
Items[r.Next(Items.Length)]
});
}
I cannot get it to work. One problem I have is I use Enumerable.Range but this creates a type error which I have been unable to solve with .ToList().
Is there a way to do it?
Inside your Select statement you are creating a new list for each iteration, each list with one random element. Just remove the new List<string>(){...} part and simply write Items[rng.Next(Items.Length)].
This way you will get a List<string> instead of a List<List<string>>.

c# Discord.net How to conver List<string> to List<SocketGuildUser>

im having trouble with converting my list type from string to SocketGuildUser as I cannot use cast and SocketGuildUser cannot be used as method. The outcome should take everything stored in "b" as string and convert it into "a" as SocketGuildUser for discord bot.
Code is :
Line 1 List<string> b = new List<string>();
Line 2 List<SocketGuildUser> a = new List<SocketGuildUser>() b;
Error is on 2nd line :
Error CS1002 ; expected
Enumerate through all elements in the string collection, and convert the string into whatever it is that the SocketGuildUser class expects and add it to a new object instance in the SocketGuildUser collection.
List<string> b = new List<string>();
List<SocketGuildUser> a = new List<SocketGuildUser>();
foreach (string str in b)
{
var user = new SocketGuildUser();
user.Foo = str;
b.Add(user);
}

How to use items of an array as names for other variables c#

I have a list of names in an array and i would like to use these names to assign them to new lists like bellow:
var list = new string[]{"bot1","bot2","bot3"};
List<string> list[0] = new List<string>();
but i am getting the error: a local variable or function named 'list' is already defined in this scope.
is there a work around !!?
your input will be greatly appreciated.
I think you can store your bots in dictionary:
var bots = new Dictionary<string,List<string>>();
bots[name] = new List<string>();
bots[name].Add("some str");
If you only need a integer as key than you can also use this solution.
List<List<string>> list = new List<List<string>>();
list.Add(new List<string>{ {"A"} });
list[0][0] = "..";

How to add data from a list to a new[]

I have a list o that has strings:
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
I want to add those strings to this:
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
};
How would I use the data from the list o and insert it into lines?
Array are be nature, fixed-length. You need to create a new array, and assign it to lines.
lines = lines.Concat(o).ToArray();
Alternately,
lines = o.AddRange(lines).ToArray();
UPDATE: Fixed dumb mistake.
Since Lines is already an array, you'll need to merge the values into your list first:
foreach (var item in lines)
o.Add(item);
Then change o to an Array:
o.ToArray(); ///returns String[] with all three values.
You can also use .concat() as others have pointed out, which will internally do the same.
An additional thing to consider is the lines variable has an Array(T) type which is a fixed size, so you must either allocate enough space to hold all the data or copy the data into a new construct.
If you allocated enough space for lines to hold all the data then it looks something like this:
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new string[3] { "Meeting#19:00:00#20:30:00#Conference", null, null };
// Copy the data from o to the end of lines
o.CopyTo(lines, 1); // Start a 1 to not overwrite the existing data
See also:
CopyTo(T[] array)
Otherwise if you have two different data sources that you want to pool into a new construct then I recommend using the Concat method. This will combine the IEnumerable(T) types which you can use ToArray or ToList to give the data the right container.
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
}.Concat(o).ToArray();
Be sure you know which container you want to use.
You can concatenate the values using Enumerable.Concat :
lines = O.Concat(lines).ToArray();
Simply this
lines = lines.Concat(o).ToArray();
Try this:
List<string> myList = new List<string>()
{
"My First String in List",
"My Second String in List"
};
string[] myArray = new string[] { "My Array First String" };
List<string> myArrayList = new List<string>();
foreach (var item in myArray)
{
myArrayList.Add(item);
}
foreach (var item in myList)
{
myArrayList.Add(item);
}
foreach (var item in myArrayList)
{
Console.WriteLine(item);
}
Console.Read();

C# Declare multiple dynamically named variables [duplicate]

This question already has answers here:
How do I name variables dynamically in C#?
(9 answers)
Closed 8 years ago.
I'm not sure of the wording of what I'm looking for so I apologize if this has been answered since I'm new to C#.
What I'm trying to do is create multiple dynamically-named Lists based off of "i".
A code snippet would like this:
List<string> infoForUserSessions = new List<string>();
// Code that adds data to infoForUserSessions
for (int i = 0; i < infoForUserSessions.Count; i++){
// I want to initialize multiple List variables based off of how many users were found in my "infoForUserSessions" List.
List<string> user[i];
}
I was hoping it would create new Lists named:
user1
user2
user3
etc.
Update Sorry all for being so confusing. You guys swarmed with answers! Let me be more specific. I'm practicing string output from a Console application such as using "PsExec \localhost qwinsta". The output would look like this:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console mariob 1 Active
rdp-tcp 65536 Listen
Each line is stored in the List "infoForUserSessions" so the data looks like:
infoForUserSessions[0] = services 0 Disc
infoForUserSessions[1] = >console mariob 1 Active
infoForUserSessions[2] = rdp-tcp 65536 Listen
I then have code to pick out the important text out of each array index:
string[] tempStringArray;
List<string> allUsersAndIDs = new List<string>();
char[] delimiters = new char[] { ' ' };
foreach (string line in infoForUserSessions)
{
tempStringArray = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < tempStringArray.Length; i++)
{
// This is where I was thinking of some logic to create a new array for each user so I could store the separate parts of a string into this new array
// Something like (which would be from the Lists above in my original message--this is based off of how many users were stored in the original infoForUserSessions List:
// user1.Add(i);
}
}
I'm still working out the logic but I figured I wanted the output to be something dynamic based off of two factors:
How many "users" (strings) were stored in the List infoForUserSessions
Individual user arrays/Lists that have their own index values of:
.
user1[0] = "services";
user1[1] = "0";
user1[2] = "Disc";
user2[0] = ">console";
user2[1] = "mariob";
user2[2] = "1";
user2[2] = "Active";
Don't try to use dynamically named variables. That's simply not how variables work in C#.
Use an array of lists:
List<string>[] user = new List<string>[infoForUserSessions.Count];
for (int i = 0; i < infoForUserSessions.Count; i++) {
user[i] = new List<string>();
}
If the number of sessions can change, you would use a List<List<string>> instead, so that you can add lists to it when you add items to the infoForUserSessions list.
You can use Dictionary<String, List<string>>
Dictionary associate a key to a value, like an physical Dictionary book.
Dictionary<String, List<string>> myDict = new Dictionary<String, List<string>>();
for (int i = 0; i < infoForUserSessions.Count; ++i){
myDict.add("user" + i, new List<string>());
}
Here is an exemple how to use Dictionary :
Dictionary<String, String> myDict = new Dictionary<String, String>();
//Line below will add both KEY and a VALUE to the dictionary, BOTH are linked one to eachother
myDict.add("apple", "Apple is a brand");
//This above line return "Apple is a brand"
myDict["apple"];

Categories