Null exception when trying to use a list [closed] - c#

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 have StartingMap that inherits from Map. Here is what I am trying to do:
Map m_map;
List<Map> m_versions;
m_versions.add(new StartingMap(...)); // create null reference exeption
m_map= new StartingMap(...); // no error and load the map perfectly
Why do I get an error with the first one and not the second one ? I am doing the same thing.

You must instantiate m_versions, like
m_versions = new List<Map>();

you need to initialize the m_versions:
m_versions = new List<Map>();
before you can use it and add items to it.

You need to instantiate List before you add any items to the collection. In the second example you are just calling the constructor of StartingMap completely different things.
So before you can add any items to your list you need to:
m_versions = new List<Map>()

Related

'Foo' is a 'namespace' but is used like a 'type' [closed]

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'm building a library that's called "Fieldsearch" and I need people to invoke it like this:
FieldSearch x = new FieldSearch();
Problem is, if my solution is called FieldSearch and my class library is called FieldSearch.Lib, I can't call the actual class FieldSearch.
'Foo' is a 'namespace' but is used like a 'type'
What do you suggest I do for this case, sans calling my actual class something else?
Generally the best practice is to use the plural form for namespaces and the singular for classes therein. For example, System.Windows.Forms contains a class called Form.
So in this example, your namespace could be called FieldSearches (or CompanyName.ProductName.FieldSearches if you want to use that convention). Now the "FieldSearch" class won't match the namespace.

Iterate over collections union [closed]

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 2 instances of IEnumerable<string> and need to iterate over both of them. Is it possible to iterate within single foreach?
Have a look at Enumerable.Concat(). I think it will do what you want:
foreach (string str in Enumerable.Concat(collection1, collection2))
{
}
Or
foreach (string str in collection1.Concat(collection2))
{
}
Union will not work unless you want to iterate over the set union of the two IEnumerable. The set union is defined as the unification of both, without duplicates. So if you rely on iterating over duplicates, it will not work, you'll have to use Concat for that.

Conditional record type with FileHelpers [closed]

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 11 years ago.
I'm trying to parse a file using the Filehelpers library. My file looks like this:
000001,"A",123,456
000002,"B","ABC","XYZ"
000003,"B","DEF","XYZ"
000004,"B","HIJ","XYZ"
My file contains rows that have different column definitions, where the 'type' of row is defined by the character in the second column. i.e. In the sample above I have an "A" row followed by three "B" rows.
Filehelpers requires that I pass the CLR type used to define a row when I instantiate the file helpers engine, or us the generic version as below.
FileHelperEngine<ARecord> engine = new FileHelperEngine<ARecord>()
This means I'm restricted to a single type to define every row in my file.
Is there any way I can parse a file like this and conditionally specify the record type based on a part of the given row?
found it. http://www.filehelpers.com/example_multirecords.html

How can I tell if a constructor was called by another constructor or not? [closed]

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 11 years ago.
When chaining constructors, in C#, how can I easily tell whether or not the constructor was called directly, or was called by another constructor using this?
public Test() : this(string.Empty, string.Empty) {}
public Test(string helloworld) : this(helloworld, string.Empty){}
public Test(string helloworld, string goodbyeworld)
{
//do work
}
If for some reason you really NEED to do this (and you pretty much never need to) this can be accomplished by making your "Master" constructor private or protected and adding another argument that indicates which other constructor was used.
I realize this is sort of a ridiculous answer but the problem is kind of ridiculous as well.
It isn't perfectly accurate, but you can check the call stack. See this question for more info
How can I find the method that called the current method?

explicitly casting generics in C# 4 [closed]

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 11 years ago.
Can anyone help me in casting generic collection in c# 4.
Here is the code snippet.
GridView1.DataSource = dataServiceColl.Select(t => t.product_desc="EdibileItem")
It is throwing up runtime error at the below line,
Gridview1.Databind();
Saying it is a HTTP Exception.
I think it should be a simple type cast.
Thanks,
Kris.
Use
t => t.product_desc=="EdibileItem"
HTTP Exception? That has nothing to do with casting.
More importantly, why are you assigning "EdibleItem" to t.product_desc here?
Select(t => t.product_desc="EdibileItem")
Did you meant == instead of =? If so, would a Where be more appropriate than a Select?
I think it all boils down to: what are you trying to achieve, exactly?

Categories