Why does constructor turn to method when brackets added? - c#

I am writing a class and whenever I start to write my constructor the text turns blue indicating it is relating the class, however, as soon as I add my () brackets for arguments, it turns black as if it were a normal method and the compiler complains that it needs a return type.
Here is the main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Kat
{
public partial class Form1 : Form
{
Cat[] catArray = new Cat[7];
catArray[0] = new Cat("Tom", "Tiger", "India", 'm', 10);
Cat tiger2 = new Cat("Kyle", "Tiger", "India", 'm', 4);
public Form1()
{
InitializeComponent();
}
}
}
And here is the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kat
{
class Cat
{
private string _name, _species, _country;
private char _gender;
private int _age;
public Cat(string name, string species, string country, char gender, int age)
{
_name = name;
_species = species;
_country = country;
_gender = gender;
_age = age;
}
}
}
And it definitely doesn't work because I can't use it to construct an object. This is an exact copy from my IDE and it is in a namespace with the main class.
The error in the main class is:
"Error 3 Method must have a return type"
And
"Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)"
And
"Error 8 'Kat.Form1.catArray' is a 'field' but is used like a 'type'"
And
"Error 10 The type or namespace name 'tiger1' could not be found (are you missing a using directive or an assembly reference?)"
There are more errors but they are more or less repeats.
It seems that the object will instantiate if you create it the traditional "Cat tiger1 = ...", but when you try to put it into the Array the errors really appear.

You have non-initialization code outside of a method:
public partial class Form1 : Form
{
Cat[] catArray = new Cat[7];
catArray[0] = new Cat("Tom", "Tiger", "India", 'm', 10); // not legal
Cat tiger2 = new Cat("Kyle", "Tiger", "India", 'm', 4);
public Form1()
{
InitializeComponent();
}
}
put it inside the form's constructor:
public partial class Form1 : Form
{
Cat[] catArray = new Cat[7];
Cat tiger2 = new Cat("Kyle", "Tiger", "India", 'm', 4);
public Form1()
{
catArray[0] = new Cat("Tom", "Tiger", "India", 'm', 10);
InitializeComponent();
}
}
The algorithms that the compiler uses to parse code are getting tripped up my the invalid syntax and can't accurately describe what the real problem is.

Note the following:
A constructor must have the exact same name as the enclosing class (struct), and it must not have a return type.
A method must have a name that is distinct from the name of the class (struct), and it must include a return type (which could be void or a normal type).
If you try to make a hybrid between 1. and 2. above, you get the compile-time error.
When you start typing:
public Cat
the development environment (Visual Studio) will think Cat is the type of a method, property or field. That happens when you continue with a space and the name of that member. When instead you continue with an opening parenthesis:
public Cat(
the development environment realizes that this must be a constructor (no type, just the member name), and it therefore changes to the color used for members, instead of the color used for types of members.
I see more details in your question now. You have:
public partial class Form1 : Form
{
Cat[] catArray = new Cat[7];
catArray[0] = new Cat("Tom", "Tiger", "India", 'm', 10);
The last of those two lines is illegal. You cannot have an array entry assignment directly inside a class like that. Consider moving that to the constructor body: public Form1() { catArray[0] = ... }

Related

Cannot call a method from a class in C# ASP.NET

I have developed the following class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ESROWCFData
{
public class ESROWFCData
{
public string GetNCAMSData()
{
//int iOffset = 0, iTake = 0;
string strOffset = "iOffset", strTake = "iTake";
string strNCAMS = "SELECT * FROM[HRO_REPORTS].[dbo].NCAMS WHERE DATEPART(YEAR, VisitStartDate) = DATEPART(YEAR, GetDate()) ORDER BY VisitorID OFFSET " + strOffset + " rows fetch next " + strTake + " rows only";
return strNCAMS;
}
}
}
and have included the using ESROWCFData; statement in my main program but when I try to call the GetNCAMSData() function it does not show up and I get the error that it does not exist. I am obviously doing something wrong but I cannot see what it is.
You have two options here. You can either make the method static so you can call it without creating an instance of your class by changing the signature to be:
public static string GetNCAMSData()
Then call it like this:
var result = ESROWFCData.GetNCAMSData()
Or you could create a new instance of the class and then call it off that like follows
var instance = new ESROWFCData();
var result = instance.GetNCAMSData()
You first have to create an instance of the ESROWFCData class:
ESROWFCData data = new ESROWFCData();
and then use it:
string s = data.GetNCAMSData();
First of all your namespace and class name are the same :
namespace ESROWCFData
{
public class ESROWFCData
{
...
So even if you want to use this class outside you have to use full type name :
ESROWCFData.ESROWCFData data = new ESROWCFData.ESROWCFData();
In case you're trying to use it like such :
ESROWCFData data = new ESROWCFData();
Compiler then is confused because of that namespace name.
Consider name changes on the namespace or class to resolve that issue.
More about that issue
Another issue is ( the one we do not know much about ) how are you trying to call that object's method. If you want to just use them statically, mark it as static :
namespace ESROWCFData
{
public class ESROWCFData
{
public static string GetNCAMSData()
{
...
And you can call them like so : ESWROWCFData.ESWROWCFData.GetNCAMSData()

Address an int[] array from a class through a different class and is also constructed from a string?

The important bit is the construction of the reference using a string. ie, I need to get access to that int[] from the construction of a string.
For example using "myClass ["int"+myString]" to access myClass.intArray
What am I doing wrong? How can I do this?
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class MyClass : MonoBehaviour {
public int[] intArray = new int[3]{1,2,3};
}
//---------------------------------------------------------------------------
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public class MyOtherClass : MonoBehaviour {
MyClass myClass;
void theMethod(string myString){
myClass = GetComponent<MyClass> ();
//-->Error is here://
int[] theArray = myClass.GetType ().GetFields (myClass ["int"+myString]);
//--//
theArray[0] = 4;
}
void Awake(){ theMethod("Array"); }
}
The method GetFields returns multiple fieldinfo's. GetField returns one informational class for one field.
Using this FieldInfo you can retrieve the actual value (as you call it: the address) from your instance. Once retrieved you can use its value:
FieldInfo fi = myClass.GetType().GetField("int"+myString); // GetField instead of GetFields.
int[] theArray = (int[])fi.GetValue(myClass);
theArray[0] = 4;
myClass.GetType ().GetFields ();
returns an Array of FieldInfo-objects.
so you could go like that:
var fieldInfo = myClass.GetType().GetFields().Where(f=>f.Name == "int" + myString).First();
and then access its value like that:
var theArray = fieldInfo.GetValue(myClass) as int[];
theArray[0] = 4;
To Omit the Linq-Part, you can also use the GetField-method (which is probably what you tried in the first place)
var fieldInfo = myClass.GetType().GetField("int" + myString); // returns single FieldInfo for your field
Also note that, since this is not JavaScript, you cannot access your field using the index-Operator like you seem to try in GetFields(myClass["int" + myString]);

C#: Instantiate a Class with String Variable as Name

I was wondering if it is possible to instantiate a class with a string variable as its name in C#. I don't know how else to explain it other than this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Product
{
string name;
decimal cost;
Product(string _name, decimal _cost)
{
name = _name;
cost = _cost;
}
}
class Program
{
static void Main(string[] args)
{
string nameForInstantiatedClass = "DellComputer";
Product nameForInstantiatedClass = new Product("Inspiron", 399.99m);
}
}
}
Is it possible to do something like this or to the same effect, using a string to declare the name of an instantiated class or is it just impossible to do? Any help is appreciated.
One thing that comes to my mind is to use a
var products = new Dictionary<string,Product>();
and then you can save / retrieve items like this
products.Add(nameForInstantiatedClass, ProductObject);
Product dellComp = products[nameForInstantiatedClass]
I don't know why you want to do that.
Is there an other logic for you?
You could put the instance in a List or Dictionary like :
Dictionary<String, Product> dict = new Dictionary()
dict.add("DellComputer", new Product("",399.99m);

Cant call my method on object from arraylist

I'm calling getName() method from Achiv class on an ArrayList in printAchiv() method located in Achievements script and it throws an error.
Here is the error message that I get on the line Debug.Log("Achiv "+i+": "+ achivList[i].getName());:
Type object' does not contain a definition for getName' and no
extension method getName' of type object' could be found (are you
missing a using directive or an assembly reference?)
I'm just trying to access value of var "name" from obejct in collection.
Achiv class :
using UnityEngine;
using System.Collections;
public class Achiv: MonoBehaviour
{
public string name;
public Achiv(string name)
{
this.name = name;
}
public string getName()
{
return name;
}
}
Achievements script :
using UnityEngine;
using System.Collections;
public class Achievements: MonoBehaviour
{
public ArrayList achivList = new ArrayList();
void Start()
{
achivList.Add(
new Achiv("First name", "Descirptionn", false));
achivList.Add(
new Achiv("Second name", "Descirptionnn", false));
printAchiv();
}
void printAchiv()
{
for (int i = 0; i <= achivList.Count - 1; i++)
Debug.Log("Achiv " + i + ": " + achivList[i].getName());
}
}
Use List<Achiv> instead of ArrayList. ArrayList is archaic, not type safe shouldn't be used anymore.
Indexer of ArrayList returns object that's why you get the error. Try the following.
public List<Achiv> achivList = new List<Achiv>();
Apart from this,
Don't expose List publicly, prefer ReadOnlyCollection or IEnumerable.
Prefer foreach over for unless there is a good reason.
printAchiv doesn't follow proper naming convention, In c# we use "CamelCase", Rename it to PrintAchiv.
get/set methods are for java style languages which doesn't supports properties. In c# we use properties instead. Create a property namely Name.
The problem is that the elements inside the ArrayList are stored as object. Thus achivList[i] returns an object, which does not provide the getName() method.
Either you can add a cast:
Debug.Log("Achiv "+i+": "+ (Achiv)achivList[i].getName());
or you can switch to a generic List:
using UnityEngine;
using System.Collections.Generic;
public class Achievements: MonoBehaviour {
public List<Achiv> achivList = new List<Achiv>();
void Start () {
achivList.Add (new Achiv("First name", "Descirptionn", false));
achivList.Add (new Achiv("Second name", "Descirptionnn", false));
printAchiv();
}
void printAchiv(){
for (int i = 0; i <= achivList.Count - 1; i++)
{
Debug.Log("Achiv "+i+": "+ achivList[i].getName());
}
}
}
ArrayList operates with Objects. You need to cast result of array indexing to Achiv:
(achivList[i] as Achiv).getName()

Cannot Add class in Enum.Extensions namespace, because Enum type (System) is then inavailable

I have two projects.
1) One (library) that contains Enum extension methods in namespace:
namespace Enum.Extensions
{
public static class EnumerationExtensions
{
public static bool Has<T>(this System.Enum type, T value)
{
try
{
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
catch
{
return false;
}
}
}
}
2) Second, Console Applications which has a reference to the library above and tries to use
its new methods:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enum.Extensions;
namespace XMLExtensionsTest
{
public enum ProcesInfo
{
ifCreate = 1,
ifRun1 = 2,
IfRun2 = 4,
IFRun3 = 8
}
class Program
{
static void Main(string[] args)
{
ProcesInfo enumInfo = ProcesInfo.ifCreate;
enumInfo = enumInfo.Add(ProcesInfo.IfRun2);
bool value = enumInfo.Has(ProcesInfo.ifCreate);
bool value2 = enumInfo.Has(ProcesInfo.ifRun1);
bool value3 = enumInfo.Has(ProcesInfo.IfRun2);
bool value4 = enumInfo.Has(ProcesInfo.IFRun3);
}
}
}
Now, because of that Extensions class all standard Enum types are not accessible.
i cannot write:
public void Test(Enum test)
{
}
but need:
public void Test(System.Enum test)
{
}
There are thousands of places where Enum is used without "System".
How to add Extensions class without touching existing Enum class calls?
Thanks!
You can do any one of the following three options, #3 is your best bet if you do not/cannot change the namespace
Rename your Enum.Extensions name space
Prefix it with something like MyStuff.Enum.Extensions
Alias it like using MyAlias = Enum.Extensions
You will need to change the namespace of the Enum Library. Which is your first library project having Extensions.
Enum is the Type name and you are using it as a namespace and hence there is ambiguity.

Categories