I have to take this to a program that can handle a Double linked list, but I am very new to C# and windows forms. I have the following code so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace doublelinkedtest
{
public class nodetoDBList
{
public object elements;
public nodetoDBList prev;
public nodetoDBList next;
public nodetoDBList (int temp)
{
elements = temp;
next = null;
}
public void inserToList(object elements)
{
nodetoDBList newLink;
newLink = new nodetoDBList (elements);
}
}
}
But now I get the following error:
Argument 1: Cannot convert from 'object' to 'int'.
Why do I get this error?. I am only referencing the variable, I am not converting it.
I am very new to C#. And as you can see I am taking this project step by step in order to achieve a double linked list project. Please Help!.
You're calling the nodeToDBList constructor (which takes an int) with an object instead:
public nodetoDBList (int temp) <- Constructor takes an int
{
elements = temp;
next = null;
}
public void inserToList(object elements)
{
nodetoDBList newLink;
newLink = new nodetoDBList (elements); <- passing in an object instead
}
Since elements is declared as an object, and it's an object in the insertToList method, odds are you should modify the constructor so that temp is an object instead of an int.
Related
I have a List A filled with double variables from equations. Now I want to create an n amount of lists equal to the equations i have with different names each.
For Example take the first 6 elements from A, put them to List1, then the next 6 of A, put them to List2 and so on.
Already wrote a method which returns back the amount of equations as int. I know how to create lists, but doing that manually makes my code too big confusing. I want to know how to create Lists in a loop. (1st List is List1, 2nd List is List2...)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
enum Equation
{
A,
B,
C
}
class Result
{
public readonly Equation Equation;
public readonly double Value;
public Result(Equation equation, double value)
{
Equation = equation;
Value = value;
}
}
class Program
{
static void Main(string[] args)
{
var results = GetGroupedResults();
}
static ILookup<Equation, Result> GetGroupedResults()
{
return GetResults().ToLookup(x => x.Equation);
}
static List<Result> GetResults()
{
return new List<Result>();
}
}
}
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?
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]);
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()
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.