|
What in Bob's name is that?! That looks totally bassackward to begin with.
If Helper.DeviceType is an enumeration then:
0) Why not send DeviceCode in as a Helper.DeviceType in the first place?
or 1) Cast the DeviceCode to Helper.DeviceType ? (Helper.DeviceType) DeviceCode
2) Why are you using Convert.ToInt32 when a simple cast will do? (int) Helper.DeviceType.Tubelight
2.0) Don't ever use Convert , it's needless.
3) Then you can use a switch instead of a bunch of freakin' if s.
switch ( DeviceCode )
{
case Helper.DeviceType.Fan : ...
}
or
switch ( (Helper.DeviceType) DeviceCode )
{
case Helper.DeviceType.Fan : ...
}
At least then your code will be readable and you can start looking for a better solution.
It appears you have a lot to learn about C# and just basic programming, I really hope you don't actually get paid to write code like this.
|
|
|
|
|
Hello ,
even if u are going for switch also . if 2mrw any device added then again u have to write obne more case statement ...
So that does not solve my problem
|
|
|
|
|
jpk420 wrote: that does not solve my problem
Indeed, which is part of why the Abstract Factory design pattern was defined. So, let's step back, what exactly are you trying to do?
Where does the DeviceCode come from?
|
|
|
|
|
Hello,
new to encryption.
however i am using the following partial code to encrypt and decrypt and it works....
<br />
<br />
<br />
byte[] encrypted = encryptStringToBytes_AES(txtMessage.Text, myRijndael.Key, myRijndael.IV);<br />
<br />
<br />
<br />
string decipheredText = decryptStringFromBytes_AES(encrypted, myRijndael.Key, myRijndael.IV);<br />
<br />
so this is an MSDN example and it works!!! i get the same thing if i DONT manipulate the byte[] any.
now the fun parts....
i try to do this
<br />
<br />
byte[] encrypted = encryptStringToBytes_AES(txtMessage.Text, myRijndael.Key, myRijndael.IV);<br />
<br />
<br />
string ciphertext = Encoding.UTF8.GetString(encrypted);<br />
<br />
byte[] cipher = Convert.FromBase64String(ciphertext);<br />
<br />
<br />
<br />
string decipheredText = decryptStringFromBytes_AES(cipher, myRijndael.Key, myRijndael.IV);<br />
<br />
<br />
<br />
as you can see i added 2 extra lines, and encoded to get back to a string. then converted to base 64 to get back to a byte []. YES i transmit the data in between those two lines but i made it simple to troubleshoot. this DOES NOT work..... i have tried diff encodings like UTF8 etc.
help!!
10 million dollars to the one who gets it working hahaha
thanks!
modified on Tuesday, August 11, 2009 11:32 PM
|
|
|
|
|
This is non-sense. You have an encrypted string in a byte array.
1. There is no guarantee that such byte array represents a string, yet you call Encoding.XXX.GetString() on it.
2. You have no guarantee that the result only contains characters from a limited set (the 64 characters acceptable in a Base64 string), yet you call Convert.FromBase64String() on the string.
Of course this does not work. There are two reasons it does not have to work.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
ahhhhh i seee what you are saying... hmmmmm
i need to send the byte[] over a streamwriter.....
|
|
|
|
|
i get the 10 million dollars!!!!!!!!!!
i solved it!!!!!
this is like the most common problem in cryptography on the NET!!!
heres the code fix:
basically this fixes problems with you cant convert from base64string
<br />
<br />
<br />
byte[] encrypted = encryptStringToBytes_AES(txtMessage.Text, myRijndael.Key, myRijndael.IV);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
char[] base64data = new char[(int)(Math.Ceiling((double)encrypted.Length / 3) * 4)];<br />
<br />
Convert.ToBase64CharArray(encrypted, 0, encrypted.Length, base64data, 0);<br />
<br />
string ciphertext = new String(base64data);<br />
<br />
byte[] cipher = Convert.FromBase64String(ciphertext);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
string decipheredText = decryptStringFromBytes_AES(cipher, myRijndael.Key, myRijndael.IV);<br />
<br />
chatbox.Text = decipheredText;<br />
<br />
<br />
the good conversions are in the middle between the spaces.
it works!!!!
tnx for all the help
|
|
|
|
|
|
try something like this:
using System.Drawing.Drawing2D;
private bool clearImage = false;
protected override void OnPaint(PaintEventArgs e)
{
if (this.clearImage)
{
using (Pen clearPen = new Pen(this.BackColor))
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(clearPen, 100, 100, 2, 2);
this.clearImage = false;
}
}
else
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(Pens.Red, 100, 100, 2, 2);
}
}
then your button click event
this.clearImage = true;
this.Refresh();
Hope this helps.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
hi
what is the best way for open connection to database ?
1.open in form1 - and close and open again in form2 ?
2.open in form1 as static - and use it for all form's (stay open all the time) ?
3.after I clossing the connection is it good to do - Connection.dispose() ?
I work on C#, and connect to sqlCE, Windows mobile 2005
thank's in addvance
|
|
|
|
|
I prefer method one. SQL server has connection pooling anyway, so it's likely to get re-used behind the scenes. And it clearly separates the functionality of the two forms, so one doesn't rely on another
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Unless totally unavoidable, don't use option 2) (open all the time), considered very naughty.
Go for option 1) when you can.
As far as 3) is concerned try to do it like this:
using (SqlConnection myConnection = new SqlConnection(.....))
{
myConnection.Open();
}
by doing so you avoid having to worry about Dispose() and Close().
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I use a static class for the DAL and close the connection after each operation.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
But when do you connect?
Generally I instantiate my DAL in Main, the DAL instantiates the connection and connects.
Then I can pass a reference to the DAL to the constructor of any Form that needs it.
Each method in the DAL opens and closes the connection as necessary.
using ( MyDAL dal = new MyDal ( ... ) )
{
S.W.F.A.Run ( new Form1 ( dal ) ) ;
S.W.F.A.Run ( new Form2 ( dal ) ) ;
...
}
A form (or other application) shouldn't know about the database at all.
|
|
|
|
|
Good afternoon.
I have the following code:
foreach (DataGridViewRow row in this.dgv.Rows)
{
int intID = (int)dgv.Rows[row.Index].Cells[0].Value;
string strType = dgv.Rows[row.Index].Cells[1].Value.ToString();
string strValue = dgv.Rows[row.Index].Cells[2].Value.ToString();
string strVoidDate = dgv.Rows[row.Index].Cells[3].Value.ToString();
d.UpdateAllowedTypes(intID, strType, strValue, strVoidDate);
}
The problem is when the last row is read, it throws the unhandled error. If I put a try catch in I'll have to do something with error. What I am attempting to do is to test for a blank ID # or make sure it doesn't read the next blank reocrd.
Any suggestion? WHEELS
|
|
|
|
|
Which line does the error occur on? If it happens on the very first line of your code snippet, then just continue; if row == null. I'm not certain why this hapens though - maybe it's the 'add new record' line?
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Thank you Computafreak. I finally figured it out:
foreach (DataGridViewRow row in this.dgv.Rows)
{
int intRowCount = dgv.Rows.Count;
if (dgv.Rows[row.Index].Cells[0].Value != null)
{
int intID = (int)dgv.Rows[row.Index].Cells[0].Value;
string strType = dgv.Rows[row.Index].Cells[1].Value.ToString();
string strValue = dgv.Rows[row.Index].Cells[2].Value.ToString();
string strVoidDate = dgv.Rows[row.Index].Cells[3].Value.ToString();
d.UpdateAllowedTypes(intID, strType, strValue, strVoidDate);
}
intctr++;
}
MessageBox.Show("Completed update.");
WHEELS
|
|
|
|
|
Hello,
Im new in programing and this is the reason why i ask you for help.
I had a program and after i click on a button i would like to show up a new form where is a ZedGraph sample.
I did everything like in Quick Start Tutorial
( http://zedgraph.org/wiki/index.php?title=CSharp_Reference_Code_for_Sample_Charts ).
Than i run the Build Solution and got no error message. But when i run the application and click on the button i got an unhandled exception message:
"Could not load file or assembly 'ZedGraph, version=5.1.5.28844, Culture=neutral, PublicKeyToken=02a83cbd123fcd60' or one of its dependencies. The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'ZedGraph, Version=5.1.5.28844, Culture=neutral, PublicKeyToken=02a83cbd123fcd60' or one of its dependencies. The system cannot find the file specified.
File name: 'ZedGraph, Version=5.1.5.28844, Culture=neutral, PublicKeyToken=02a83cbd123fcd60'
at Proba_5___XML.graf.InitializeComponent()
at Proba_5___XML.graf..ctor() in D:\-= FEI =-\-= Diplomovka =-\Programozas\Program - Utlmovy Plan\Proba 21\Proba 5 - XML\graf.cs:line 17
at Proba_5___XML.Form1.button7_Click(Object sender, EventArgs e) in D:\-= FEI =-\-= Diplomovka =-\Programozas\Program - Utlmovy Plan\Proba 21\Proba 5 - XML\Form1.cs:line 641
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"
I dont know what i did wrong. Ill be happy for any help.
Regards
Tom
|
|
|
|
|
this happens when you have the wrong version DLL of zedgraph in the directory (or didnt copy it to the debug or release directory (did you look there??))
also if you didnt include the reference,
goto add reference.
easy fix.
|
|
|
|
|
Hi, everyone, I'm building an application in VC# 2008 that displays a bitmap into a pictureBox but my bitmap it's always displayed at my top-left corner, I know that I can center it, but I want to position the bitmap at the bottom-left corner. Does anyone know how can I do that? Thanks for your time.
|
|
|
|
|
There are two methods that spring to mind.
1) subclass the PictureBox and do the paint handling yoursef. Very complicated! Not my first choice.
2) create an in-memory bitnap the same size as your PictureBox's ClientRectangle with a transparent background, draw the required image in the required location and then add this new bitmap as the Image for the PictureBox. Sounds more complicated, but is much easier to do. My recommendation.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
That really sounds complicated but I understand you perfectly. Actually I made a proof project and this is the code:
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Bitmap OrigImage = new Bitmap(pictureBox2.Image);
int targetTop = pictureBox1.Height - OrigImage.Height;
int targetLeft = 0;
int targetWidth = pictureBox1.Width;
int targetHeight = pictureBox1.Height;
int sourceWidth = pictureBox1.Width;
int sourceHeight = pictureBox1.Height;
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawImage(OrigImage,new Rectangle(targetLeft, targetTop, targetWidth, targetHeight),new Rectangle(0,0, sourceWidth, sourceHeight),GraphicsUnit.Pixel);
It worked prefectly. That's exactly what I wanted.
Thanks a lot
|
|
|
|
|
Pleased to help.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I would not create a new image, instead resize the control to match the image size, and locate it appropriately.
And of course, if I were to paint the image myself, there would be no PictureBox at all.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|