List array of array of struct [closed] - c#

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 7 years ago.
Improve this question
First, I declared two structure like the following:
struct m1
{
int
int
char
...
}
and
struct m2
{
int
int
char
...
}
I have two arrays of m1 and m2. I need to map one m1 against to multiple m2. I don't Know the size of both, so after I search I decide to use
List<m1 or m2> = new List<m1 or m2>();
and works fine.
but
List<m1 , m2> = new List<m1 , m2>();
gives me error "require one type argument"
Please help me, how can I do that?
Thanks

List<T> requires one Type it cannot have two types ,what I see is you need to map one m1 against multiple m2, so in this case Dictionary<TKey,TValue> is suitable here:
Dictionary<m1 , List<m2>> = new Dictionary<m1 , List<m2>>();
and now add items in dictionary.

Related

foreach loop with 2 hashset variables 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 last month.
Improve this question
I have the following
variableAs = "A12,B12,C12"
variableBs = "1.54,75.30,55.50"
method (HashSet<string> variableAs, HashSet<double> variableBs)
foreach (var variableA in variableAs)
{
Method here requires the two values, must have preserved datatype
and be in same order, ie A12 with 1.54, B12 with 75.30
}
I have tried zip from this answer but I do not know how it works with hashset arrays,
NOTE the Method i am editing has it has hashset, the actual values are for example only, if there is an error, It must be my understanding of what a hashset is but I cannot change the hashset
You need to first populate your input-strings into some collection:
var As = variableAs.Split(',');
var Bs = variableBs.Split(',');
However a HashSet is not an ordered collection and thus the wrong choice here. You'd need a List or just an array.
Now you may use the mentioned Zip-function to combine the two elements together:
var result = As.Zip(Bs, (a, b) => new { a, b });
See the complete code at this fiddle: https://dotnetfiddle.net/9qTG2E

What does (int) mean 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 6 years ago.
Improve this question
I've encountered sometimes code like this and I am kind of new to programming. I want to find out what's the meaning behind those objects or data type enclosed in parenthesis.
(int)
(datagridview)
(form)
If you see something like this it's called a cast. It's used to explicitly convert a data type to another data type.
double pi = 3.14159;
int my_int = (int)pi;
See this description on casting for more details.

C# How to sort list of objects with string values [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 6 years ago.
Improve this question
I need sort list of string values using IComparable:
10.1, 10.10, 10.11, 10.2, 10.3
Expected result:
10.1, 10.2, 10.3, 10.10, 10.11
Tried to use list.Sort but didn't work
Basically what you want to do is to sort by the number after the decimal point in the string. So, take only that part, convert it to a number and then sort. Using Comparer it will look like
List<string> values = new List<string> { "10.1", "10.10", "10.11", "10.2", "10.3" };
values.Sort((x, y) => int.Parse(x.Split('.')[1]).CompareTo(int.Parse(y.Split('.')[1])));
Or using linq it will look like:
var result = values.OrderBy(value => int.Parse(value.Split('.')[1]));
In the case you want to first sort by the second part and then by the first you can do:
var result = values.OrderBy(value => int.Parse(value.Split('.')[0]))
.ThenBy(value => int.Parse(value.Split('.')[1]))
.ToList();
keep in mind that this solution is naive and assumes all your values have a . and that the 2 parts of it can be parsed into int - If it is not the case then you can use TryParse

c# - Check if object was converted when put into a list? [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 7 years ago.
Improve this question
I am making a game where there are squares (sectors) generated to make a path for a ball to go. There are two types of sectors: Sector and Presector. They are all put into a list of type Sector. How would I check to see if a specific sector in that list was actually a Presector before it was put in?
BTW: Presector is a child class of Sector.
I looked all over the place and couldn't find anything. The as keyword isn't working for me, and Type.IsAssignableFrom isn't either. EDIT: is will not work either, since that just checks if an object is that type.
SAMPLE CODE TIME!
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
Now, we have a list full of two sectors. The second one was casted. How do I find that out using code?
if (objectFromList is Presector)
// Code here..
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
sectors.Add(new Sector());
Presector ps = new Presector();
sectors.Add(ps);
// this returns an array with one element
var x = sectors.OfType<Presector>().ToArray();
// this returns true (the second element IS a Presector)
var hasPresector = sectors.Any(s => s is Presector);
// this returns true (the presector is present in the list)
var containsPs = sectors.Contains(ps);
What's the problem with the 'is' keyword?

Can I add numbers to int value instead of changing it? [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 7 years ago.
Improve this question
Is it possible to add numbers to the total value of an int cell instead of set its value? Correctly I'm using a list and I and things are getting weird.. the list is <int> list and its value is jumping into random numbers without any reason. I think that changing the count type (from list into int) will solve it. (c#. visual studio 2013).
Doesn't look so hard to me...?
int variable = 10; // start with 10 points
variable++; // add 1
variable += 3 // add 3

Categories

Resources