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>();
}
}
}
Related
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.
I want to write a program that simulates a waiting list based on priority and arrival time. Priority takes higher precedent over arrival time. The list will contain priority, arrival time and name.I can manage one key priority, name or arrival time, name. What is the best way to combine 2 keys ?
Try something like code below. CompareTo() method will allow you to use standard sort methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication61
{
class Program
{
static void Main(string[] args)
{
SortedList<PriorityTime, string> sList = new SortedList<PriorityTime, string>();
}
}
public class PriorityTime : IComparable<PriorityTime>
{
public int priority { get; set; }
public DateTime time { get; set; }
public int CompareTo(PriorityTime other)
{
if (other.priority != this.priority)
{
return this.priority.CompareTo(other.priority);
}
else
{
return this.time.CompareTo(other.time);
}
}
}
}
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);
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.
Say we have a UI and in this UI we have a dropdown. This dropdown is filled with the translated values of an enum.
Bow, we have the possibility to sort by the int-value of the enum, by the name of the enum, and by the translated name of the enum.
But what if we want a different sorting than the 3 mentioned above. how to handle such a requirement?
Implement your own IComparer:
using System;
using System.Collections.Generic;
namespace test {
class Program {
enum X {
one,
two,
three,
four
}
class XCompare : IComparer<X> {
public int Compare(X x, X y) {
// TBA: your criteria here
return x.ToString().Length - y.ToString().Length;
}
}
static void Main(string[] args) {
List<X> xs = new List<X>((X[])Enum.GetValues(typeof(X)));
xs.Sort(new XCompare());
foreach (X x in xs) {
Console.WriteLine(x);
}
}
}
}
You can use the Linq extension OrderBy, and perform whatever comparison magic you want:
// order by the length of the value
SomeEnum[] values = (SomeEnum[])Enum.GetValues(typeof(SomeEnum));
IEnumerable<SomeEnum> sorted = values.OrderBy(v => v.ToString().Length);
Then wrap the different sorting alternatives into methods, and invoke the right one based on user preferences/input.
IEnumerable<T>.OrderBy(Func<T, TKey>, IComparer<TKey>)
Sort FileSystemRights enum using Linq and bind to WinForms comboBox:
comboBox1.DataSource = ((FileSystemRights[])Enum.GetValues(typeof(FileSystemRights))).
OrderBy(p => p.ToString()).ToArray();
Perhapse you could create an extension method for the Enum class, like this:
... first the declaration...
public enum MyValues { adam, bertil, caesar };
...then in a method...
MyValues values = MyValues.adam;
string[] forDatabinding = values.SpecialSort("someOptions");
...The extension method...
public static class Utils
{
public static string[] SpecialSort(this MyValues theEnum, string someOptions)
{
//sorting code here;
}
}
And you could add different parameters to the extension metod for different sort options etc.