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 5 years ago.
Improve this question
I am starting to learn c# and I came across this error
Cannot convert implicitly convert Animal into
System.Collections.ArrayList
This is the code
private Animal adoptedPets;
public Animal AdoptedPets
{
get { return adoptedPets;}
set {adoptedPets = value;}
}
I am trying to set a list of Animals to this property of my object. I tried to cast my list like that (ArrayList) adoptedPets, but it didn't work and gave me the above error.
If you want adoptedPets to be a List<Animal> and not just a single Animal, you should declare it as such. Best would be to declare it a s
private IList<Animal> adoptedPets;
public IList<Animal> AdoptedPets
{
get { return adoptedPets;}
set {adoptedPets = value;}
}
Note the IListinstead of List. You could also use IEnumerable. It is a good pracice so that any kind of list can be assigned.
Now, I am assuming that you want your adoptedPets to store a list of Animals . But you are not precise at all in your question.
Related
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 3 years ago.
Improve this question
hi i am want to make custom conman validation using c# extension method for all datatype please give me some idea about it and some examples?
Thanks
I think you do not know about variables in c#.
Please see more about variables in this link, because all types inherit from System.Object
enter link description here
For example, this code maybe solve your problem, but I don't understand for what purpose...
public static class Test
{
public static bool UserCheck(this object a)
{
return a != null;//ha-ha
}
}
public class SomeProgramm
{
public void main()
{
int a = 0;
a.UserCheck();
object b = new object();
b.UserCheck();
}
}
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
List<Redemption> r = Redemption.Get();
public void Convert(List<T> list)
{
}
Say r contains a list of Redemption business objects, is there a way of using generics to pass that list into the generic function for processing? The reason I need generics is because it must be able to process list of other types such as members.
You could try this:
public void convert<T>(List<T> list)
{
}
This is now a generic method. For more information about this, please have a look here.
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 just wondering How to create a custom helper to cast specific class object without using CLR support casting feature? for example I have a class with name MyClass
MyClass
property int id
property string name
property list Items
End Class
I want to convert it like
MyClass myclassobj = MyCast(obj)
function MyCast(object obj) as MyClass
'Some Code here
End function
I don't want to use DirectCast and TryCast here.
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 9 years ago.
Improve this question
Just a few questions regarding best practices in c#:
Any reason why I'd prefer to do:
var list = new List<string>();
object[] array = list.ToArray<object>();
comboBox.AddRange(array);
instead of:
var list = new List<string>();
comboBox.AddRange(list.ToArray<object>());
Also any reason why I'd prefer to do:
class myClass
{
private string _hello;
public string Hello
{
get {return _hello;}
set {_hello = value;}
}
}
instead of:
class myClass
{
public string Hello;
}
Your first example just creates an intermediate variable to hold the converted array - if you don't need the array later on then logically they're equivalent.
Your second question is a more significant difference. Properties have many advantages over fields, including potential logic in the get/set accessors, binding to UI controls (most controls can bind to properties but not fields.
In general any public data members should be implemented as properties instead of fields. Non-public data members can be implemented as either.
There are lots of answers on SO that answer your second question.
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 8 years ago.
Improve this question
I came across this situation where List has no type specified after it in <>:
List someVar = new List();
However when I try this in Visual Studio I get an error. What is the reason for VS not letting me declare List this way?
Let me show you where I saw it:
public override IEnumerable SomeMethod()
{
List someVar= new List();
// more code
return someVar;
}
P.S After contacting the owner of the project it turned out Wordpress striped out the tags <> after List and IEnumerable, so it actually should be List<SomeClass> and IEnumerable<SomeClass>
public override IEnumerable<SomeClass> SomeMethod()
{
List<SomeClass> someVar= new List<SomeClass>();
// more code
return someVar;
}
There is not an inbuilt class called List. There is ArrayList, but: click on List and press f12. This will show you where it is declared. There are two options:
a class called List that is nothing whatsoever to do with List<T> has been declared in the local project; for example:
class List { ...} // here we go; a class called List
a using alias (at the top of the file) has been used to spoof List as a name; for example:
using List = System.Collections.Hashtable;
or
using List = System.Collections.Generic.List<int>;
You get an error because the List class does not exist in the .NET Framework. If you want to use a non-generic list that can hold any type of object, use ArrayList.
I don't recognise it (I thought it was ArrayList before List<T> arrived?). Either way it would be an older class invented before generics was implemented. I'd use List<object>.