|
|
Prior to .NET 4 there was a performance penalty (caused by boxing/unboxing) in using Enums as Keys in a Dictionary [^]; and there were some work-arounds, for example, this CP article: [^].
Comments on .NET 4 indicate the boxing is now not performed when Enum values are of the "simplest Type." I think that means when the values are 'Int rather than the other legal Types, but I am not sure about that.
I have been searching for a definitive statement on Enums and boxing in .NET 4.5 and above, but no luck so far. I wonder if an Enum using Flags would be handled in any different way (I doubt that).
thanks, Bill
«The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you.» Soren Kierkegaard
|
|
|
|
|
Looking at the source of the EqualityComparer<T> class[^], there's special-case code to handle enums where the underlying type is Byte , SByte , Int16 , UInt16 , Int32 , UInt32 , Int64 or UInt64 :
if (t.IsEnum) {
TypeCode underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(t));
switch (underlyingTypeCode) {
case TypeCode.Int16:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer<short>), t);
case TypeCode.SByte:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer<sbyte>), t);
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Byte:
case TypeCode.UInt16:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer<int>), t);
case TypeCode.Int64:
case TypeCode.UInt64:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer<long>), t);
}
}
The presence of the [Flags] attribute won't make any difference.
Obviously, this is the latest version of the BCL. I'm not sure how far back this was added.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Message Removed
modified 31-Mar-16 9:18am.
|
|
|
|
|
Hello,
i'am using the sync Framework to sync a local sdf with different Server databases (Oracle, Postgre).
Now i have a look for a newer versions or alternative solution.
I am going to use a local sqlite instead of the SQL Compact but i do not find current solutions to sync an sqlite against Oracle or Postgre.
Many Thanks
|
|
|
|
|
You can treat them as ODBC data sources and should be able to sync against them that way.
I know this works for Oracle at any rate, providing you have the Oracle client installed on the machine that will provide the data source.
|
|
|
|
|
I'm very sorry if I didn't post my code correctly. I am having trouble with this. The code is working to the directions specifications, but, I am not sure about one thing. How do avoid getting....lets say....6 "O's" and 3 "X's" when I run the program. That is not simulation. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tic_Tac_Toe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
const int ROWS = 3;
const int COLS = 3;
int[,] game = new int[ROWS, COLS];
Random rand = new Random();
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
game[row, col] = rand.Next(2);
}
if (game[0, 0] == 0)
{
label2.Text = "O";
}
else
{
label2.Text = "X";
}
if (game[0, 1] == 0)
{
label3.Text = "O";
}
else
{
label3.Text = "X";
}
if (game[0, 2] == 0)
{
label4.Text = "O";
}
else
{
label4.Text = "X";
}
if (game[1, 0] == 0)
{
label5.Text = "O";
}
else
{
label5.Text = "X";
}
if (game[1, 1] == 0)
{
label6.Text = "O";
}
else
{
label6.Text = "X";
}
if (game[1, 2] == 0)
{
label7.Text = "O";
}
else
{
label7.Text = "X";
}
if (game[2, 0] == 0)
{
label8.Text = "O";
}
else
{
label8.Text = "X";
}
if (game[2, 1] == 0)
{
label9.Text = "O";
}
else
{
label9.Text = "X";
}
if (game[2, 2] == 0)
{
label10.Text = "O";
}
else
{
label10.Text = "X";
}
if (game[0, 0] == 0 && game[0, 1] == 0 && game[0, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[0, 1] == 1 && game[0, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[1, 0] == 0 && game[1, 1] == 0 && game[1, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[1, 0] == 1 && game[1, 1] == 1 && game[1, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[2, 0] == 0 && game[2, 1] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[2, 0] == 1 && game[2, 1] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 0] == 0 && game[1, 0] == 0 && game[2, 0] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[1, 0] == 1 && game[2, 0] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 1] == 0 && game[1, 1] == 0 && game[2, 1] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 1] == 1 && game[1, 1] == 1 && game[2, 1] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 2] == 0 && game[1, 2] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 2] == 1 && game[1, 2] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 0] == 0 && game[1, 1] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[1, 1] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 2] == 0 && game[1, 1] == 0 && game[2, 0] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 2] == 1 && game[1, 1] == 1 && game[2, 0] == 1)
{
label11.Text = "X Wins";
}
else
{
label11.Text = "DRAW";
}
}
}
}
}
|
|
|
|
|
The problem is that there's no context for the random values generation. You generate each 0 or 1 irrespective of how many there already are. So you need a method that ensures that there are either four 0's and five 1's or the other way around. There are several ways to do that. Here are two:
a) Create a one-dimensional array with a length of nine and fill it either with four or five 1's (the other indices are 0 by default and to decide whether it will be four or five 1's you can use Random.Next(4, 6) ). Then randomly swap the values of two indices multiple times. Then fill the values from that one-dimensional array into your two-dimensional game[3,3]-array.
b) Create an array of nine random numbers with no explicit upper bound (so the maximum value will be Int32.MaxValue). For the first four or five of those numbers, set the least significant bit (LSB) to 0 and for the remaining values, set it to 1. Then sort the array by values. Then fill the two-dimensional game-array based on the values from the sorted array but take only the value of the LSB.
There's something more to say about your code but, as I have to finish, I'll do that later if nobody else did until then.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Quite interesting code, I can see a fair bit wrong with it.
But, to your question of getting an equal balance of Xs and Os, why don't you just do it the way it's done, but taking it in turns?
That means choosing a random place to put you X or O (flip-flopped), rather than choosing a random X or O to put in an ordered place.
Regards,
Rob Philpott.
|
|
|
|
|
You can greatly simplify your code by storing "X" and "O" directly in game after changing it to a table of chars .
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
|
Hi All,
I have created console application to execute DB2 commands using C#. my commands executed fine and DB2 CLP command window opened i can see output as well. but once my command executed I am not able to close DB2 CLP window using C# code.
I tried to kill Process.GetProcessByName but not succeeded.
I also used Application.Exit() method.
Note: i am connecting to server DB2 using local machine.
Below is my code.
try
{
ProcessStartInfo objProcStartInfo;
Process objProcess;
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", "db2 connect to " + strMasterdatabaseName + " user " + strserviceAccountUserName + " using " + strserviceAccountPassword + "&db2 -f " + strexportFilePath + " > " + strexportLogFilePath + "");
objProcStartInfo.RedirectStandardOutput = false;
objProcStartInfo.UseShellExecute = false;
objProcStartInfo.CreateNoWindow = false;
objProcess = new Process();
objProcess.StartInfo = objProcStartInfo;
objProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
can someone please provide any direction on this?
Thanks
modified 30-Mar-16 12:49pm.
|
|
|
|
|
Message Closed
modified 30-Mar-16 15:05pm.
|
|
|
|
|
tried below code still DB2 CLP window remains open
using (objProcess = new Process())
{
try
{
//Connect To IBM DB2 database using CLP Process Object
//<tbd> Need to work on autoclose DB2 CLP
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", "db2 connect to " + strMasterdatabaseName + " user " + strserviceAccountUserName + " using " + strserviceAccountPassword + "&db2 -f " + strexportFilePath + " > " + strexportLogFilePath + "");
objProcStartInfo.RedirectStandardOutput = false;
objProcStartInfo.UseShellExecute = false;
objProcStartInfo.CreateNoWindow = false;
objProcess.StartInfo = objProcStartInfo;
objProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
objProcStartInfo = null;
objProcess.Dispose();
}
}
}
|
|
|
|
|
Calling Dispose on a Process only cleans up the unmanaged resources; it doesn't terminate the process.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Message Closed
modified 30-Mar-16 15:05pm.
|
|
|
|
|
The window the the OP is seeing belongs to the external db2cmd.exe process. Your answer implied that disposing of the Process instance would close that window.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Try adding -c at the start of the arguments:
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", string.Format("-c db2 connect to {0} user {1} using {2}&db2 -f {3} > {4}", strMasterdatabaseName, strserviceAccountUserName, strserviceAccountPassword, strexportFilePath, strexportLogFilePath));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
excellent , I added -c and its working fine. thanks Richard
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", "-c db2 connect to " + strMasterdatabaseName + " user " + strserviceAccountUserName + " using " + strserviceAccountPassword + "&db2 -f " + strexportFilePath + " > " + strexportLogFilePath + "");
|
|
|
|
|
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.
public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = null;
using (var memory = new System.IO.MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.EndInit();
return bitmapImage;
}
}
public void ShowPicture(Bitmap bmp)
{
pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
return;
}
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine.
What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
I don't know the answer to this, but Googling suggests others might:
c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^]
Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something.
I'd so some Googling!
Regards,
Rob Philpott.
|
|
|
|
|
I did google. I know it is some small mistake that I am not seeing. But I did google alot. Thanks again.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Here's the converter I wrote years ago. It converts a Bitmap to ImageSource (specifically a BitmapFrame ):
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ResourcesLibrary
{
[ValueConversion(typeof(Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Bitmap bmp = value as Bitmap;
if (bmp == null)
return null;
using (Stream str = new MemoryStream())
{
bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
str.Seek(0, SeekOrigin.Begin);
BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return bdc.Frames[0];
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Compared to my working code:
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
and
window.Content = imageControl;
window.UpdateLayout();
window.ShowDialog();
Don't know enough about your app ... you may be able to apply .UpdateLayout() somewhere else in the visual tree.
|
|
|
|
|
this should work:
private BitmapImage SetModifiedImage(System.Drawing.Bitmap bmp){
bmp = CreateNonIndexedImage(bmp);
BitmapImage bmpi = new BitmapImage();
bmpi.BeginInit();
bmpi.StreamSource = new System.IO.MemoryStream((byte[])(new System.Drawing.ImageConverter()).ConvertTo(bmp, typeof(byte[])));
bmpi.EndInit();
return bmpi;
}
private System.Drawing.Bitmap CreateNonIndexedImage(System.Drawing.Bitmap bmp){
System.Drawing.Bitmap newbmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newbmp);
g.DrawImage(bmp, 0, 0);
return newbmp;
}
You don't need the "CreateNonIndexedImage" normally. I had different sets of input and the difference in the PixelFormat gave me problems when working on the bytes of the bitmap.
Hope this helps.
|
|
|
|
|