|
I thought NonSerialized is intended for this purpose, or what whould you expect?
|
|
|
|
|
Hi,
of course you are right, but I need this as the parent class saves information in this field which the child classes do not need anymore because they compute it dynamically. And of course we are not talking about an int field, but about an array of a more complex data type which would consume up to 20 or 30 KB per instance which would be serialized and "thrown away" after deserialization as it has to be recomputed anyways. I hope this helps to better understand the purpose of what I want to achieve.
Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
|
|
|
|
|
If you don't want to store it, but on deserialization want to get default values. Then maybe the OnDeserializedAttribute can be of use...
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx
|
|
|
|
|
Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there.
Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
|
|
|
|
|
maybe something like this could be usefull?!
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
this.nonserializedmember = reInitialise();
foreach (Child c in this.childs) {
child.newNonserializedValue = this.nonserializedmember;
}
}
|
|
|
|
|
I just answered this question myself . I just had to use OnSerializing and OnSerialized to set the field to null and reset it to its old value respectively. Thank you very much for your help.
Regards,
Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
|
|
|
|
|
No prob
|
|
|
|
|
I just don't get it why is it so complex to make a simple update...
I want trough a procedure to modify all the rows in a table like so: suppose I have a table with colums a, b, and c. c is identified by a and b. The procedure has the parameters a,b and c.
a,b,c
Example: UpdateazaCantCont(1,3,5) must add 5 to the old value of c. So if one row in the database table values are 1,3,8 the new values must be 1,3,13.
Please help me, because I'm in an death stop in my project!
The procedure i've wrote gives me no error, but it doesnt do anything.
Here's the code I have written:
private void UpdateazaCantCont(string aVal, string bVal, int cVal)
{
DataSet mds = new DataSet();
OleDbDataAdapter mda = new OleDbDataAdapter("select * from table where a='" + aVal + "'and b='" + bVal + "'", sirConex);
mda.Fill(mds,"continut_locatie");
DataTable t = mds.Tables[0];
DataRow[] r = t.Select("a='"+aVal+"b='"+ bVal + "'");
for (int i=0; i<r.length;>
string sirUpdate = "Update table set c=c+'"+cVal+"'where a='" + aVal + " and b='" + bVal + "'";
OleDbConnection myConex = new OleDbConnection(sirConex);
OleDbCommand cmd = new OleDbCommand(sirUpdate, myConex);
cmd.CommandText = sirUpdate;
mda.UpdateCommand = cmd;
try
{
mda.Update(mds, "table");
}
catch (Exception ex)
{
MessageBox.Show("Eroare: " + ex);
}
}
|
|
|
|
|
First of all, because aVal and bVal are strings, it's possible I can erase your database. Do you know about SQL Injection ?
Second, why use a dataset to get back a single value ?
Third, NEVER assume that an array has any values in it, without checking.
Fourth, what's the point of doing a select when you already have only those values in the table ?
you seem to be some of the way along, although you could do all of this in a stored proc and with 3 lines of code on the C# side.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks for your reply. Can you tell me those 3 lines?
|
|
|
|
|
http://www.codeproject.com/KB/cs/simplecodeasp.aspx
[^]
Probably more than 3 lines, given you need one line for each of your three params. but still, a lot more readable. Of course, you also need to write the proc...
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi,
Currently i am doing a project related to file encryption using c#. I new to the c# programming actually. I used microsoft visual c# express 2008. I had combine some of the code from internet reference, but i am facing a problem where program highlight "cs.Close();" and show
IndexOutOfRangeException was unhandled when decrypt button is clicked. The file decryption is unsuccessful due to this exception i think. Is there anybody know how to solve for this problem? Any suggestion on how to change the code? The code of my project is as below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox2.Text = filename;
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
}
private void button3_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox3.Text = filename;
}
else
{
return;
}
}
private void button4_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize /8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.Close();~IndexOutOfRangeException was unhandled
fsIn.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
fsCrypt = null;
}
}
}
The source code can be download from the link below:
http://cid-1caedb18b83acfa1.skydrive...Encryption.rar
If I am not disturbing,please guide. Thanks a lot!
Regards,
sf
|
|
|
|
|
I recommend you learn some basics before trying to copy and paste code you don't understand. Assuming someone wants to trawl through this and find the error, how does that help you learn ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Yaya..Thanks for your advice. I am trying hard to find out what problem with my code but the result is still the same. Is there any idea on which part do i need to focus with?
Regards,
cat ang
|
|
|
|
|
cat ang wrote: I am trying hard to find out what problem with my code
There is no obvious logical structure for a start. I don't know where to start reading to determine your problem.
|
|
|
|
|
Decrypt button? Which one is that? All the buttons are named Button1, Button2, etc. That isn't descriptive and while you might know which is the "Decrypt button", I do not. Please, for the love of all things, learn how to name things properly.
|
|
|
|
|
Sorry about that. This is my first time to post a thread, it is my careless.
i had rename my code and it is as below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void browseForFileToEncrypt_Click_1(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox2.Text = filename;
}
else
{
return;
}
}
private void btnFileEncryption_Click_1(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
}
private void browseForFileToDecrypt_Click_1(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox3.Text = filename;
}
else
{
return;
}
}
private void btnFileDecryption_Click_1(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.Close();
fsIn.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
fsCrypt = null;
}
}
}
The link for this program is:
http://cid-1caedb18b83acfa1.skydrive.live.com/self.aspx/Public/File%20Encryption.rar[^]
Please give me some advice on it. Thanks ya
Regards,
cat ang
|
|
|
|
|
how to display X axis values on top of graph while the y axis displayed top to bottom values?
|
|
|
|
|
|
in my form i have several buttons(say 5)that call the same method, in the method i want to identify wich button is clicked and depending on that i decide what to do.how can i identify the button that is clicked
|
|
|
|
|
All those 5 buttons share same event handler? If yes, you can use parameter sender in that event handler. If you have a separate method, you need to add a parameter to it and pass the sender to there.
|
|
|
|
|
To expand on Navaneeth's answer/
void MyClick(object sender, eventargs ea)
{
Button button = sender as Button;
if ( button != null) // should always be the case
{
// here you can use if (button == button1, or you can use switch(button.Name), or check the Tag property, or whatever.
}
}
The way I tend to do this is to use an enum to identify the different actions, and then stuff that into the tag property, pull that out and switch on it. This means that it's typesafe instead of switching on strings.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
how can i split particular part from the image based on the pixel coordinates?
|
|
|
|
|
By using a DrawImage overload to draw just that section of the image onto a new image that's the right size to receive it.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi,
I am developing an web application, in which an windows application dll is run when the user click a button. From that application user communicate with other clients using socket.
In the server path there is an image folder.
My issue is that, how can i access the folder from the windows application dll.
Regards
YPKI
|
|
|
|
|