How to create dynamic incrementing variable using “for” loop in C# - c#

How to create dynamic incrementing variable using "for" loop in C#? like this:
track_1, track_2, track_3, track_4. so on.

You can't create dynamically-named variables. All you can do - it to create some collection or array, and operate with it.
I think the best class for you is generic List<>:
List<String> listWithDynamic = new List<String>();
for (int i = 1; i < limit; i +=1)
{
listWithDynamic.Add(string.Format("track_{0}", i));
...
}

Assuming you want strings:
for (int i = 1; i < limit; i +=1)
{
string track = string.Format("track_{0}", i);
...
}
But when you already have variables called track_1, track_2, track_3, track_4 you will need an array or List:
var tracks = new TrackType[] { track_1, track_2, track_3, track_4 } ;
for (int i = 0; i < tracks.length; i++)
{
var track = tracks[i]; // tracks[0] == track_1
...
}

Obvious Solution
for (var i = 0; i < 10; i++)
{
var track = string.Format("track_{0}", i);
}
Linq-Based Solution
foreach (var track in Enumerable.Range(0, 100).Select(x => string.Format("track_{0}", x)))
{
}
Operator-Based Solution This is somewhat hacky, but fun none-the-less.
for (var i = new Frob(0, "track_{0}"); i < 100; i++)
{
Console.WriteLine(i.ValueDescription);
}
struct Frob
{
public int Value { get; private set; }
public string ValueDescription { get; private set; }
private string _format;
public Frob(int value, string format)
: this()
{
Value = value;
ValueDescription = string.Format(format, value);
_format = format;
}
public static Frob operator ++(Frob value)
{
return new Frob(value.Value + 1, value._format);
}
public static Frob operator --(Frob value)
{
return new Frob(value.Value - 1, value._format);
}
public static implicit operator int(Frob value)
{
return value.Value;
}
public static implicit operator string(Frob value)
{
return value.ValueDescription;
}
public override bool Equals(object obj)
{
if (obj is Frob)
{
return ((Frob)obj).Value == Value;
}
else if (obj is string)
{
return ((string)obj) == ValueDescription;
}
else if (obj is int)
{
return ((int)obj) == Value;
}
else
{
return base.Equals(obj);
}
}
public override int GetHashCode()
{
return Value;
}
public override string ToString()
{
return ValueDescription;
}
}

don't know if I get your question, but I will try:
for(var i = 1; i < yourExclusiveUpperbound; i++)
{
var track = String.Format("$track_{0}", i);
// use track
}
or with some LINQ-Magic:
foreach(var track in Enumerate.Range(1, count)
.Select(i => String.Format("$track_{0}", i)))
{
// use track
}

Do as follow:
for (int i = 0; i < lenght; i ++)
{
any work do in loop
}

No, we can't create dynamically named variables in a loop. But, there are other elegant ways to address the problem instead of creating dynamically named variables.
One could be, create an array or list before the loop and store values in array / list items in the loop. You can access the array / list later anywhere in your code. If you know which variable you want to use (track_1, track_2, ...), you can simply access it from the array / list (tracks[1], tracks[2], ...).
List<String> tracks = new List<String>();
for (int i = 1; i < limit; i++)
{
Track track = new Track();
tracks.Add(track);
...
}

Related

Casting a DataGridViewComboBoxColumn item into another object fails

This method is part of my derived class from DataGridViewComboBoxColumn:
public ComboboxColourItem InsertColour(ushort iColourIndex)
{
ComboboxColourItem ocbItem = ComboboxColourItem.Create(iColourIndex);
bool bAppend = true;
if (Items.Count > 15)
{
// There are other colours, need to find right index
for(int i = 15; i < Items.Count; i++)
{
//if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
//{
//}
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
bAppend = false;
Items.Insert(i, ocbItem);
break;
}
}
}
if (bAppend)
Items.Add(ocbItem);
return ocbItem;
}
The Items contain ComboboxColourItem objects. Here is the definition of those items:
public class ComboboxColourItem
{
public string Name { get; set; }
public ushort Index { get; set; }
public Color Value { get; set; }
public ComboboxColourItem(string Name, ushort Index, Color Value)
{
this.Name = Name;
this.Index = Index;
this.Value = Value;
}
public override string ToString()
{
return Name;
}
static public ComboboxColourItem Create(ushort iColourIndex)
{
OdCmColor oColour = new OdCmColor();
oColour.setColorIndex(iColourIndex);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
String strColour = textInfo.ToTitleCase(oColour.colorNameForDisplay());
if (iColourIndex < 8)
strColour = String.Format("{0} ({1})", strColour, iColourIndex);
else if (iColourIndex == 8 || iColourIndex == 9 || iColourIndex >= 250)
strColour = String.Format("Grey Shade ({0})", iColourIndex);
else
strColour = String.Format("Other ({0})", iColourIndex);
ComboboxColourItem oColourItem = new ComboboxColourItem(
strColour,
iColourIndex,
Color.FromArgb(oColour.red(), oColour.green(), oColour.blue()));
oColour.Dispose();
return oColourItem;
}
}
I know that I can use foreach(ComboboxColourItem ocbItem2 in Items) but I need to start from a certain index. So I decided to use a regular for loop.
I thought that I could do this to cast the item from object:
if(ocbItem.Index < (ComboboxColourItem)Items[i].Index)
{
}
Does not like the cast. Yet, if I do this:
ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i];
if (ocbItem.Index < ocbItem2.Index)
{
}
That works perfectly. So why could I not cast? Did I do it wrong? I don't think I can use foreach in this situation.
Since the member access has higher precedence than the cast (C# operator precedence), the following
(ComboboxColourItem)Items[i].Index
is equivalent to
(ComboboxColourItem)(Items[i].Index)
which of course is invalid.
Use this instead
((ComboboxColourItem)Items[i]).Index

Deserialization causes copies of List-Entries

I'd like to create a very general Model-Layer, which can also be passed arround as JSON. One Model should show a LED-Panel of a RaspberryPi2. Since I'd like to model the Class to be as near as possible to the reality, I force a List to always have 8 * 8 Leds. The Class is looking like this:
public class VisualLedPanel
{
private readonly Lazy<List<VisualLed>> _lazyVisualLeds = new Lazy<List<VisualLed>>(CreateVisualLeds);
public VisualLed this[int x, int y]
{
get
{
var result = VisualLeds.FirstOrDefault(f => f.X == x && f.Y == y);
return result;
}
}
public IEnumerable<VisualLed> VisualLeds
{
get
{
return _lazyVisualLeds.Value;
}
set
{
var tt = value;
}
}
private static List<VisualLed> CreateVisualLeds()
{
var result = new List<VisualLed>();
for (var x = 0; x <= 7; x++)
{
for (var y = 0; y <= 7; y++)
{
result.Add(new VisualLed(x, y));
}
}
return result;
}
}
The problem arises with the Serialization: I'm using the NewtonSoft. Json.Net Serializer, and as far as I've seen, it first accesses the Getter, which causes the Logic to create the Leds, and then sets them afterwards.
The only solution I could think of would either be a Custom-Deserializer or some sort of Constructor shennenigans.
It also seems like the Deserializer doesn't use the Set-Property of the VisualLeds-Value, since my Debugger-Stop never was hitted.
Is there an easy possibility arround to have the best of both worlds? I'd like to have the Model as general as possible without the need of Custom-Deserializer.
The easiest way for you to do this without having to write your own custom JsonConverter will be to serialize your collection of VisualLed objects as a proxy array property, marking the original property as ignored:
public class VisualLedPanel
{
private readonly Lazy<List<VisualLed>> _lazyVisualLeds = new Lazy<List<VisualLed>>(CreateVisualLeds);
public VisualLed this[int x, int y]
{
get
{
var result = VisualLeds.FirstOrDefault(f => f.X == x && f.Y == y);
return result;
}
}
[JsonIgnore]
public IEnumerable<VisualLed> VisualLeds
{
get
{
return _lazyVisualLeds.Value;
}
}
[JsonProperty("VisualLeds")]
VisualLed [] SerializableVisualLeds
{
get
{
return VisualLeds.ToArray();
}
set
{
if (value == null || value.Length == 0)
{
if (_lazyVisualLeds.IsValueCreated)
_lazyVisualLeds.Value.Clear();
}
else
{
_lazyVisualLeds.Value.Clear();
_lazyVisualLeds.Value.AddRange(value);
}
}
}
private static List<VisualLed> CreateVisualLeds()
{
var result = new List<VisualLed>();
for (var x = 0; x <= 7; x++)
{
for (var y = 0; y <= 7; y++)
{
result.Add(new VisualLed(x, y));
}
}
return result;
}
}
Prototype fiddle
For a further discussion, see Why are all the collections in my POCO are null when deserializing some valid json with the .NET Newtonsoft.Json component. Using ObjectCreationHandling.Replace would not be appropriate in this case since you want your Lazy<List<VisualLed>> _lazyVisualLeds to be read-only.

How to keep one of duplicate entries in list<T>

I have a list and in the list there are multiple entries. If the list contains an entry that is duplicated then I want to only keep one of the duplicates.
I've tried many things, the list.Distinct().ToList() and this does not remove the duplicate entry, I do not want to override the classes Equals method, so is there a way outside of that.
I've also done this method which seems to again, not remove the duplicate entry as it does not consider object a == object b.
private void removeDupes(List<Bookings> list)
{
int duplicates = 0;
int previousIndex = 0;
for (int i = 0; i < list.Count; i++)
{
bool duplicateFound = false;
for (int x = 0; x < i; x++)
{
if (list[i] == list[x])
{
duplicateFound = true;
duplicates++;
break;
}
}
if (duplicateFound == false)
{
list[previousIndex] = list[i];
previousIndex++;
}
}
}
There is another overload of the Distinct LINQ extension method that also takes an IEqualityComparer as an argument (see this link). So you'd need to create a class that implements IEqualityComparer<Bookings> and supply an instance of it to the Distinct-method. This way, you do not need to override the Equals method of the type.
The rules on whether two objects are equal to one another are implemented in the EqualityComparer.
As an alternative, you can use a HashSet and supply the EqualityComparer in the constructor.
A possible solution for your problem in order of Markus answer might look like this:
public class Booking
{
public Booking(int id, float amount)
{
BookingId = id;
BookingAmount = amount;
}
public int BookingId { get; }
public float BookingAmount { get; }
}
public class BookingComparer : IEqualityComparer<Booking>
{
public bool Equals(Booking x, Booking y)
{
return (x.BookingAmount == y.BookingAmount) && (x.BookingId == y.BookingId);
}
public int GetHashCode(Booking obj)
{
return obj.BookingId.GetHashCode()*17 + obj.BookingAmount.GetHashCode()*17;
}
}
internal class Program
{
private static void Main(string[] args)
{
var booking1 = new Booking(1, 12);
var booking2 = new Booking(1, 12);
var bookings = new List<Booking>();
bookings.Add(booking1);
bookings.Add(booking2);
var result = bookings.Distinct(new BookingComparer()).ToList();
}
}

What is the java Vector.element() C# equivalent

Here is the java code i incompletely tried to translate
static Enumeration enumerate()
{
Vector list = new Vector();
Enumeration e = cache.keys();
while (e.hasMoreElements())
{
Vector v = (Vector) cache.get(e.nextElement());
for (int i = 0; i < v.size(); i++)
{
list.addElement(v.elementAt(i));
}
}
return list.elements();
}
This is the C# translation but not complete
public static IEnumerable<Http> enumurate()
{
List<Http> list = new List<Http>();
IEnumerator e = cache.Keys.GetEnumerator();
while (e.MoveNext())/*While e has more element*/
{
var vector = (List<Http>)cache[e.Current];
for (int i = 0; i < vector.Count; i++)
{
list.Add(vector.ElementAt<Http>(i));
}
}
return //Something missing!!
}
Any help please !
In C# List<Http> implements IEnumerable<Http> so you can simply return your list:
return list;
To convert the code to C# even more, you could just skip the adding of elements to the list and yield results directly:
public static IEnumerable<Http> enumerate()
{
IEnumerator e = cache.Keys.GetEnumerator();
while (e.MoveNext())/*While e has more element*/
{
var vector = (List<Http>)cache[e.Current];
for (int i = 0; i < vector.Count; i++)
{
yield return vector.ElementAt<Http>(i);
}
}
}
Also, you can avoid using enumerators directly and make the code even more readable:
public static IEnumerable<Http> enumerate()
{
foreach (var key in cache.Keys)
{
foreach (var http in (List<Http>)cache[key])
{
yield return http;
}
}
}
Just return the local list. List<T> implements IEnumerable<T>.
In C# you can use LINQ to simplify your code:
public static IEnumerable<Http> enumerate()
{ return cache.Keys.SelectMany(key => (List<Http>)cache[key]); }
Assuming cache is declared as Dictionary<..., List<Http>> you can avoid key lookup by using the Values property:
public static IEnumerable<Http> enumerate()
{ return cache.Values.SelectMany(list => list); }
One important difference between the above and the code that you have now is that the above is not evaluated until you actually try to iterate through the returned collection. If you want to evaluate results immediately, you can add a call to .ToList(). This is equivalent to your original code:
public static IEnumerable<Http> enumerate()
{ return cache.Keys.SelectMany(key => (List<Http>)cache[key]).ToList(); }

C# Extend array type to overload operators

I'd like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by "==", so it could (hopefully) be used as a key in dictionary.
The thing is I don't want to implement whole IList interface to my new class, but only add those two operators to existing array class.
I'm trying to do something like this:
class MyArray : Array<int>
But it's not working that way obviously ;).
Sorry if I'm unclear but I'm searching solution for hours now...
UPDATE:
I tried something like this:
class Zmienne : IEquatable<Zmienne>
{
public int[] x;
public Zmienne(int ilosc)
{
x = new int[ilosc];
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return base.Equals((Zmienne)obj);
}
public bool Equals(Zmienne drugie)
{
if (x.Length != drugie.x.Length)
return false;
else
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] != drugie.x[i])
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hash = x[0].GetHashCode();
for (int i = 1; i < x.Length; i++)
hash = hash ^ x[i].GetHashCode();
return hash;
}
}
Then use it like this:
Zmienne tab1 = new Zmienne(2);
Zmienne tab2 = new Zmienne(2);
tab1.x[0] = 1;
tab1.x[1] = 1;
tab2.x[0] = 1;
tab2.x[1] = 1;
if (tab1 == tab2)
Console.WriteLine("Works!");
And no effect. I'm not good with interfaces and overriding methods unfortunately :(. As for reason I'm trying to do it. I have some equations like:
x1 + x2 = 0.45
x1 + x4 = 0.2
x2 + x4 = 0.11
There are a lot more of them, and I need to for example add first equation to second and search all others to find out if there is any that matches the combination of x'es resulting in that adding.
Maybe I'm going in totally wrong direction?
For a single type, it is pretty easy to encapsulate, as below. Note that as a key you want to make it immutable too. If you want to use generics, it gets harder (ask for more info):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
static class Program {
static void Main() {
MyVector x = new MyVector(1, 2, 3), y = new MyVector(1, 2, 3),
z = new MyVector(4,5,6);
Console.WriteLine(x == y); // true
Console.WriteLine(x == z); // false
Console.WriteLine(object.Equals(x, y)); // true
Console.WriteLine(object.Equals(x, z)); // false
var comparer = EqualityComparer<MyVector>.Default;
Console.WriteLine(comparer.GetHashCode(x)); // should match y
Console.WriteLine(comparer.GetHashCode(y)); // should match x
Console.WriteLine(comparer.GetHashCode(z)); // *probably* different
Console.WriteLine(comparer.Equals(x,y)); // true
Console.WriteLine(comparer.Equals(x,z)); // false
MyVector sum = x + z;
Console.WriteLine(sum);
}
}
public sealed class MyVector : IEquatable<MyVector>, IEnumerable<int> {
private readonly int[] data;
public int this[int index] {
get { return data[index]; }
}
public MyVector(params int[] data) {
if (data == null) throw new ArgumentNullException("data");
this.data = (int[])data.Clone();
}
private int? hash;
public override int GetHashCode() {
if (hash == null) {
int result = 13;
for (int i = 0; i < data.Length; i++) {
result = (result * 7) + data[i];
}
hash = result;
}
return hash.GetValueOrDefault();
}
public int Length { get { return data.Length; } }
public IEnumerator<int> GetEnumerator() {
for (int i = 0; i < data.Length; i++) {
yield return data[i];
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public override bool Equals(object obj)
{
return this == (obj as MyVector);
}
public bool Equals(MyVector obj) {
return this == obj;
}
public override string ToString() {
StringBuilder sb = new StringBuilder("[");
if (data.Length > 0) sb.Append(data[0]);
for (int i = 1; i < data.Length; i++) {
sb.Append(',').Append(data[i]);
}
sb.Append(']');
return sb.ToString();
}
public static bool operator ==(MyVector x, MyVector y) {
if(ReferenceEquals(x,y)) return true;
if(ReferenceEquals(x,null) || ReferenceEquals(y,null)) return false;
if (x.hash.HasValue && y.hash.HasValue && // exploit known different hash
x.hash.GetValueOrDefault() != y.hash.GetValueOrDefault()) return false;
int[] xdata = x.data, ydata = y.data;
if(xdata.Length != ydata.Length) return false;
for(int i = 0 ; i < xdata.Length ; i++) {
if(xdata[i] != ydata[i]) return false;
}
return true;
}
public static bool operator != (MyVector x, MyVector y) {
return !(x==y);
}
public static MyVector operator +(MyVector x, MyVector y) {
if(x==null || y == null) throw new ArgumentNullException();
int[] xdata = x.data, ydata = y.data;
if(xdata.Length != ydata.Length) throw new InvalidOperationException("Length mismatch");
int[] result = new int[xdata.Length];
for(int i = 0 ; i < xdata.Length ; i++) {
result[i] = xdata[i] + ydata[i];
}
return new MyVector(result);
}
}
Its not permitted to extend the array class, see the reference: http://msdn.microsoft.com/en-us/library/system.array.aspx
You could either implement IList (which has the basic methods), or encapsulate an Array in your class and provide conversion operators.
Please let me know if you need more detail.
Can you not just use the List class? This already does what you want via the AddRange method.
implement the ienumerable interface

Categories

Resources