Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Gorg

Member Since 24 Jul 2000
Offline Last Active May 08 2012 07:49 AM

Posts I've Made

In Topic: Opinions on C# dynamic

01 September 2011 - 08:00 AM

And the compiler will wistfully allow my stupidity until runtime then BOOM.



I never found that to be a problem in practice. If it is used with object created by programmers while writing code, unit/integration tests will usually catch the mistake.

It only gets problematic if the object type is selected from values from an external source, like say an external file or your JSON example. Then extra validation has to be done before calling the function, but since the type is selected externally, it has to be validated anyway before being used.

However, if you do have a large project with a lot of programmers, I certainly would recommend to stick with static typing and only allow a few leads to use dynamic if it would significantly improve the code. My example fits that because the VM I wrote does not deal with types directly, so a switch statement with a type check would have needed to be done for each math operations if I did not use dynamic.


result = VMMath.ADD(src1, src2);

instead of say
if ( src1 is int )
{
      result = VMMath.ADDInt(src1,src2)
}

else if ( src1 is float )
{
      result = VMMath.ADDFloat(src1,src2)
}

else if ( src1 is string)
{
      result = VMMath.ADDString(src1,src2)
}
.....

A different design can work around that problem and not require dynamic and still be a one liner, but it's all that was required for the VM I built.

In Topic: Opinions on C# dynamic

01 September 2011 - 05:57 AM

I have used it once so far in a little VM. I used it for Mathematical operations.



public object Add(dynamic lhs, dynamic rhs)
        {
            return lhs + rhs;
        }

That was a lot faster to code that then having all the overloads for all the existing types. Of course, this might not perform in tight loops. The .NET runtime does perform polymorphic caching, but I never bother to do any benchmark it because the VM was fast enough for its purpose.

In Topic: [C#] List for generic undo redo and Mementos

31 August 2011 - 06:06 AM

Can you just make your history list of type "List<Object>"? You can store anything there. Then when someone hits Undo or Redo you just look at the most recent item's type:
...


Don't do this. The scenario presented by the OP has a clear solution that follows the open/closed principle. With the solution, you can add new commands without modifying the UndoRedo class. In your example, you cannot.

In Topic: [C#] List for generic undo redo and Mementos

29 August 2011 - 11:22 AM

Your interface should not be generic and each memento target bundled in the memento.

With that change the UndoRedo class also does not require to be a generic and can handle any memento from anywhere for any instance.

public interface IMemento
{
	IMemento Restore();
}



class AddTileMemento : IMemento
{
	private int index;
	Tile[] target;
	public AddTileMemento(Tile[] target, int index)
	{
		this.target = target;
		this.index = index;
	}

	public override IMemento Restore()
	{
		Tile removed = target[index];

		IMemento inverse = new RemoveTileMemento(target, index, removed);

		target[index] = null;

		return inverse;
	}
}


class RemoveTileMemento : IMemento
{
	private int index;
	private Tile removed;
	Tile[] target;
	public RemoveTileMemento(Tile[] target, int index, Tile removed)
	{
		this.target = target;
		this.index = index;
		this.removed = removed;
	}

	public override IMemento Restore()
	{
		IMemento inverse = new AddTileMemento(target, index);

		target[index] = removed;

		return inverse;
	}
}

In Topic: Jitters in opengl textures

26 August 2011 - 06:40 AM

i'm not find GL_TEXTURE_MAX_FILTER may be this is a problem?
thanks


Sorry, I meant GL_TEXTURE_MAG_FILTER.

As AndyEsser said, a video would be good or a bunch of quick successive snapshots would work too.

PARTNERS