Search Results

Search found 17 results on 1 pages for 'danvil'.

Page 1/1 | 1 

  • How to compute fmod in C#?

    - by Danvil
    For given floating point numbers x and a, I would like to compute r (and n) such that x = a*n + r . In C/C++ this function is called fmod. However I do not see a convenient function in .NET. Math.DivRem is only for integers ...

    Read the article

  • How to use linq to find the minimum

    - by Danvil
    I have a class A { public float Score; ... } and a List<A> items and would like to find the A which has minimal score. Using items.Min(x => x.Score) gives the minimal score and not the instance with minimal score. How can I get the instance with only iterating once through my Collection?

    Read the article

  • How to handle an "infinite" IEnumerable?

    - by Danvil
    A trivial example of an "infinite" IEnumerable would be IEnumerable<int> Numbers() { int i=0; while(true) { yield return i++; } } I know, that foreach(int i in Numbers().Take(10)) { Console.WriteLine(i); } and var q = Numbers(); foreach(int i in q.Take(10)) { Console.WriteLine(i); } both work fine (and print out the number 0-9). But are there any pitfalls when copying or handling expressions like q? Can I rely on the fact, that they are always evaluated "lazy"? Is there any danger to produce an infinite loop?

    Read the article

  • How to set up a Bitmap with unmanaged data?

    - by Danvil
    I have int width, height; and IntPtr data; which comes from a unmanaged unsigned char* pointer and I would like to create a Bitmap to show the image data in a GUI. Please consider, that width must not be a multiple of 4, i do not have a "stride" and my image data is aligned as BGRA. The following code works: byte[] pixels = new byte[4*width*height]; System.Runtime.InteropServices.Marshal.Copy(data, pixels, 0, pixels.Length); var bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for(int i=0; i<height; i++) { for(int j=0; j<width; j++) { int p = 4*(width*i + j); bmp.SetPixel(j, i, Color.FromArgb(pixels[p+3], pixels[p+2], pixels[p+1], pixels[p+0])); } } Is there a more direct way to copy the data?

    Read the article

  • How can I copy unmanaged data in C# and how fast is it?

    - by Danvil
    I have two unmanaged pointers in the form of IntPtr and want to copy data between them. How can I do this? I know the method Marshal.Copy, but it can only copy between unmanaged and managed. And the second part: Is copying unmanaged data from C# slower than doing it in unmanaged C/C++ using memcopy?

    Read the article

  • How to link a property setter to a delegate?

    - by Danvil
    I would like to give a property setter to a delegate. How is this done? class A { private int count; public int Count { get { return count; } set { count = value; } } } A a = new A(); delegate void ChangeCountDelegate(int x); ChangeCountDelegate dlg = ... ? // should call a.Count = x

    Read the article

  • Why is the compiler not complaining about an additional ',' in Array or Object Initializers?

    - by Danvil
    Using simple type like class A { public int X, Y; } with object intializers, one can write var a = new A { X=0, Y=0 }; But the following is also accepted by the compiler: var a = new A { X=0, Y=0, }; // notice the additional ',' Same for int[] v = new int[] { 1, 2, }; This looks a bit strange ... Did they forgot to reject the additional ',' in the compiler or is there a deeper meaning behind this?

    Read the article

  • What is the purpose of CS0161 when switching over an enum and how to work around it?

    - by Danvil
    Consider the following example of a simple enum and a switch over all possible enum values. enum State { Alpha, Beta, Gamma } State state = ...; switch(state) { case Alpha: ... break; case Beta: ... break; case Gamma: ... break; } Now I get error CS0161: not all code paths return a value. This seems rather pointless to me, because I would expect that other cases are not possible. So what is the purpose of this error, and how would one work around this in the most elegant way?

    Read the article

  • How to force a deep copy when copying structs with arrays?

    - by Danvil
    If have a struct A { public double[] Data; public int X; } How can I force a deep copy when using operator= or adding instances of A to a container? The problem is for example: A a = new A(); var list = new List<A>(); list.Add(a); // does not make a deep copy of Data A b = a; // does not make a deep copy of Data Do I really have to implement my own DeepClone method and call it every time? This would be extremly error-prone ...

    Read the article

  • Typedef in C# across several source files

    - by Danvil
    I am writing a C wrapper an would like to use a typedef aquivalent to define some types which should be valid in quite a lot of source files. Those "types" are just different aliases to [u]int16/32/64_t, but are useful to distinguish function parameters. One could use using MyId=System.Int32;, but this needs to be redeclared in every file as far as I see... Is there a better way?

    Read the article

  • How to build a LINQ query from text at runtime?

    - by Danvil
    I have a class A { public int X; public double Y; public string Z; // and more fields/properties ... }; and a List<A> data and can build a linq query like e.g. var q = from a in data where a.X > 20 select new {a.Y, a.Z}; Then dataGridView1.DataSource = q.ToList(); displays the selection in my DataGridView. Now the question, is it possible to build the query from a text the user has entered at runtime? Like var q = QueryFromText("from a in data where a.X > 20 select new {a.Y, a.Z}"); The point being, that the user (having programming skills) can dynamically and freely select the displayed data.

    Read the article

  • Constructor for an immutable struct

    - by Danvil
    Consider the following simple immutable struct: struct Stash { public int X { get; private set; } public Stash(int _x) { X = _x; } } This is not working, because the compiler wants me to initialize the "backing field" before I can access the property. How can I solve this?

    Read the article

1