Cannot use ArrayList in Unity, C# - c#

I try to use ArrayList to store a value but I don't know why it gets error.
using UnityEngine;
public class Example : MonoBehaviour
{
ArrayList aList = new ArrayList();
// Start is called before the first frame update
void Start()
{
int i = 3;
aList.Add(i); //error here
Debug.Log(aList[0]); //and here
}
// Update is called once per frame
void Update()
{
}
}
when I create a C# script in Unity it has using System.Collections and using System.Colections.Generic but when I use Ctrl+S to save those will disappear because IDE005: Using directive is unnecessary
And here is the error statement:
Error CS1061 'ArrayList' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'ArrayList' could be found (are you missing a using directive or an assembly reference?)
I don't know I'm missing any using directive or something.

What class is that actually? Presumably it is not the System.Collections.ArrayList class, because that does have an Add method. If that's the class you meant to use then you need to make sure that you have imported the namespace or else you must qualify the type name. That said, you probably shouldn't be using that ArrayList class at all. Not sure whether it's a thing with Unity but, in .NET development generally, that class was effectively replaced by the List<T> in 2005.

Try updating your Unity and Visual Studio, the below code worked perfectly for me. (Also ensure that you downloaded the Unity libraries in Visual Studios so you get better intellisense suggestions)
One last thing, if you have problems with classes then try to implicitly state your class
System.Collections.ArrayList a = new System.Collections.ArrayList();
a.Add("test");

Related

Using directive - Find all methods and properties in use by reference [duplicate]

This question already has answers here:
How can I find all usages of a namespace and its members?
(2 answers)
Closed 2 years ago.
EDIT3: Question rewritten for future reference, thanks to BahJiy
Is there a Visual Studio (or ReSharper) command like "Find Usages" to find class usages from using directive?
Find Usages only shows the reference of the namespace itself (and thus "the only usage" as per below screenshot).
When I comment out that using System.Reactive.Linq line as per below, Observable indicates an error that it doesn't exist in the current context.
I could comment out the using directive and check errors like above, is there a better way than checking errors one by one?
Answer: Find Usages does it, cursor position matters
As mentioned below in the comment to the answer, the cursor position needs to be at using, rather than the namespace.
Old question body
Is there a Visual Studio (or ReSharper) command that tells me which methods are coming from SomeNamespace.SomeClass defined by using SomeNamespace.SomeClass within using file?
I would like to check the use case when I encounter some unexpected namespace.
Find Usages or All References on the using directive only shows other directives.
I could delete the very using directive and check for errors within the file, but I must be ignorant of a better way...
EDIT + EDIT2:
An example may help to clarify the situation better.
SomeClass.cs
namespace SomeNamespace {
public static class SomeClass {
public static string SomeExtensionMethod(this string s){
// some implementation
}
/*
More extension methods
*/
}
}
Consumer.cs
using SomeNamespace.SomeClass
namespace OtherNamespace {
public class Consumer {
public void Consume(){
string id = "someid";
string after = id.SomeExtensionMethod;
}
/*
More extension methods from SomeClass used
*/
}
}
The question is, when I find some Consumer having a using SomeNamespace.SomeClass, what's the best way to check where the underlying methods are used?
Is there a command similar to Find Usages to get all the methods coming from SomeNamespace.SomeClass under Consumer class?
Normally just check your using. Visual Studio only allow a method/class call from one Namespace. If there are two more more method of the same name in multiple of your Namespace that you are using, Visual Studio will force you to specify the Namespace explicative in your call.
i.e.
using Name1;
using Name2;
namespace Class1 {
class Testing {
public Testing {
// this method only exists in Name1
Method1 ( );
// this method exist in both Name1 and Name2
Name1.Method2 ( );
Name2.Method2 ( );
}
}
}
So if you see only
using SomeNamespace;
you will know that any classes that may come from that namespace is specifically from SomeNamespace.
If there was another class of that same name but also in a different namespace, Visual Studio will force you to type in that namespace when calling.
So if you see that you usings are normal and there are no random
RandomNamespace.Class1 class1 = new RandomNamespace.Class1 ( );
You know for sure that nothing is out of the ordinary.
you want to know if a imported namespace is neccessary to be imported, because types from that namespace are used? if its that have a look in the addon "Productivity power tools" e.g. There is an option to remove and sort usings on save

Check using directive reference in Visual Studio [duplicate]

This question already has answers here:
How can I find all usages of a namespace and its members?
(2 answers)
Closed 2 years ago.
EDIT3: Question rewritten for future reference, thanks to BahJiy
Is there a Visual Studio (or ReSharper) command like "Find Usages" to find class usages from using directive?
Find Usages only shows the reference of the namespace itself (and thus "the only usage" as per below screenshot).
When I comment out that using System.Reactive.Linq line as per below, Observable indicates an error that it doesn't exist in the current context.
I could comment out the using directive and check errors like above, is there a better way than checking errors one by one?
Answer: Find Usages does it, cursor position matters
As mentioned below in the comment to the answer, the cursor position needs to be at using, rather than the namespace.
Old question body
Is there a Visual Studio (or ReSharper) command that tells me which methods are coming from SomeNamespace.SomeClass defined by using SomeNamespace.SomeClass within using file?
I would like to check the use case when I encounter some unexpected namespace.
Find Usages or All References on the using directive only shows other directives.
I could delete the very using directive and check for errors within the file, but I must be ignorant of a better way...
EDIT + EDIT2:
An example may help to clarify the situation better.
SomeClass.cs
namespace SomeNamespace {
public static class SomeClass {
public static string SomeExtensionMethod(this string s){
// some implementation
}
/*
More extension methods
*/
}
}
Consumer.cs
using SomeNamespace.SomeClass
namespace OtherNamespace {
public class Consumer {
public void Consume(){
string id = "someid";
string after = id.SomeExtensionMethod;
}
/*
More extension methods from SomeClass used
*/
}
}
The question is, when I find some Consumer having a using SomeNamespace.SomeClass, what's the best way to check where the underlying methods are used?
Is there a command similar to Find Usages to get all the methods coming from SomeNamespace.SomeClass under Consumer class?
Normally just check your using. Visual Studio only allow a method/class call from one Namespace. If there are two more more method of the same name in multiple of your Namespace that you are using, Visual Studio will force you to specify the Namespace explicative in your call.
i.e.
using Name1;
using Name2;
namespace Class1 {
class Testing {
public Testing {
// this method only exists in Name1
Method1 ( );
// this method exist in both Name1 and Name2
Name1.Method2 ( );
Name2.Method2 ( );
}
}
}
So if you see only
using SomeNamespace;
you will know that any classes that may come from that namespace is specifically from SomeNamespace.
If there was another class of that same name but also in a different namespace, Visual Studio will force you to type in that namespace when calling.
So if you see that you usings are normal and there are no random
RandomNamespace.Class1 class1 = new RandomNamespace.Class1 ( );
You know for sure that nothing is out of the ordinary.
you want to know if a imported namespace is neccessary to be imported, because types from that namespace are used? if its that have a look in the addon "Productivity power tools" e.g. There is an option to remove and sort usings on save

Enforcing full namespace with using statement in C#

I have been looking around for a while now to see how can I enforce my C# projects to have full namespace path.
For example actual if namespace for class X is Foo.Bar.Car.Dealer when doing Ctrl+. in visual studio it sometimes puts statement like using Car.Dealer; this specially becomes a problem with multiple projects solution. I have been looking around for StyleCop rule or something that might help me get this done.
Any help or ideas?
EDIT
The above statement holds true only if the using class falls under same namespace prefix. Here is complete code example:
File: X.cs
namespace Foo.Bar.Car.Dealer {
class X {}
}
File: UsingClass.cs
namespace Foo.Bar.Another.ClassPath {
using Car.Dealer;
class UsingClass {
private X _x;
}
}
The VS picked using Car.Dealer but I want to enforce using Foo.Bar.Car.Dealer
I do not know about versions prior to 2012, but from then on the icon that pops up for action upon coming across an unknown type offers both adding the namespace via using directive or to simply prefix the type being referenced by the full namespace.
If you do not want to add the namespace via using directive (which would look like using Foo.Bar.Car.Dealer;),
then in your example you simply need to reference your type X as Foo.Bar.Car.Dealer.X.
Example:
//assuming your X type is instantiable
Foo.Bar.Car.Dealer.X myX = new Foo.Bar.Car.Dealer.X();

SmartTag for adding a "using" statement for extension method

Lets say I have the following code that needs to be moved to another file/class:
namespace MyNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Class1
{
public void SomeMethod()
{
IEnumerable<int> l = new List<int>();
if (l.Count() == 0)
{
//...
}
}
}
}
If I move SomeMethod to another class (in another file) that does not have (say) a using System.Linq declaration, the l.Count() statement would not compile giving the very obvious message:
'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Collections.Generic.IEnumerable<int>' could be found (are you missing a using directive or an assembly reference?)
If the new class/file does not have a using System.Collections.Generic declaration and you put your cursor on the IEnumerable<int> you will get a SmartTag that would suggest possible using-declarations. If you put your cursor on the Count() extension call you get no SmartTag to suggest the using System.Linq declaration.
Is there maybe an IDE extension out there that would help add a using statement for extension methods as explained above? This would help allot with refactoring code.
I have found a solution that would be free:
Download and install DevExpress CodeRush Express and then download and install the CR_ExtensionMethodsHelper community plugin.
You could also try the following commercial products (not free):
DevExpress CodeRush (with CR_ExtensionMethodsHelper community plugin; although there is a request pending to have this plugin supported natively)
ReSharper (I'm not using this one my self, but others have suggested it)

are you missing a using directive or an assembly reference

I can't figure out this error in visual c#.
Error 1
'Engine.VerticalMenu' does not contain a definition for '_buttons' and no extension method '_buttons' accepting a first argument of type 'Engine.VerticalMenu' could be found
(are you missing a using directive or an assembly reference?)
For this line:
System.Diagnostics.Debug.Assert(false, _menu._buttons.Count.ToString());
I have two projects, first one is Engine with same namespace Engine and of type class library, and the other one is windows form app that uses this Engine library. I have both using directives and references to project, what could possibly be causing this? Thank you.
Looks like _buttons is private class member, so you can't access it from the outside.
Either make it public, or even better add public getter to the class of _menu:
public TypeOfButtonCollectionHere Buttons { get { return _buttons; } }
And change the calling code to:
System.Diagnostics.Debug.Assert(false, _menu.Buttons.Count.ToString());
Is _buttons possibly private? Then it is not visible outside of the menu class and you can't access it. Wrap it to a public property and you can access it.
It sounds like a naming conflict (namespace or class). Have you tried using the fully qualified name for the class? Without more information this is just a shot in the dark.
No, its not the public thing, vs has other errormessages for that. It looks like _menu doesn't have a member _buttons at all. So this means either the class or the interface _menu is of doesnt have _buttons.
Check that all the assemblies referenced by the project Engine is also referenced by your Win form.
It would usually give another error if that's the case but not always.
If they are all referenced. Try a rebuild of just the Engine project. VS might throw the mentioned error if there's a compilation error in a referenced project. Those errors should show up in the error log, so you could also check the error log to see if there are other errors some of which is in engine.
(Even if that's not the case I would personally still build Engine alone, to completely rule it out)

Categories

Resources