C#: Instantiate a Class with String Variable as Name - c#

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);

Related

Methods, arguments, and member variables, not accessible in Windows to Mono port

I can't find this addressed on SO. I am porting a Windows .NET application, that compiles and runs perfectly, to Mono on Linux. I have missed something small. Here is a part of the code:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace DAPTimeClock
{
public static class DatabaseOps
{
private static string _dbFile;
public static string Get_dbFile()
{
return _dbFile;
}
public void Set_dbFile(string value)
{
this._dbFile = value;
}
public static bool foundFile = false;
public static string SetFile(String dblocation = #"Data" +
"Source=../db/TimeClock.db; Version=3;")
{
Set_dbFile( dblocation );
int pathStart = Get_dbFile().IndexOf ('=') + 1;
int pathEnd = Get_dbFile().IndexOf (";") - 1;
string filePath = Get_dbFile().Substring (pathStart,
pathEnd - pathStart + 1);
if (File.Exists (filePath)) {
foundFile = true;
return string.Format ("The db file {0} has been " +
"located", filePath);
} else
{
foundFile = false;
return string.Format("Unable to find db file {0}.",
filePath);
}
}
public static bool AddPerson(Person p)
{
bool result = false;
Class Continues .....
Here are the issues:
Other classes can't find any methods from this class. ex. DatabaseOp.Set_dbFile() Yields no such method.
The methods in this class can't find methods from this class.
The methods in this class can't see the outside classes.
If I pass an argument to the methods in the class, the complier says it can't find a parameter by that name.
The methods in the class can't see its own member variables.
I have done the following:
I removed the static, now making regular objects. (no change)
I double checked the namespaces. (all the same, no typos)
I tried making the member variable public and adding a get; and set;
I am stumped. Can anyone see what might be hanging this up? Is there something about mono I missed? All other classes are working fine.
Set_dbFile should be declared as
public static void Set_dbFile(string value)
compiler should complain: "cannot declare instance members in static class". Have you miss this?

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()

Why does constructor turn to method when brackets added?

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] = ... }

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]);

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