|
I have a collection that inherits the NameObjectCollectionBase and also has two internal state booleans. I need to serialize this collection and include the booleans. I implemented ISerializable but I cannot find very much step-by-step info. MSDN just shows using the GetObject() method to pass on to the base serializer which provides not help to me. The few articles here never touch on doing your own data serialization.
What I would like to do is serialize not only the objects in the collection, but also these two booleans. The SetType() seems to provide a means of defining the type to serialize. But everything else is Get methods to retrieve data from the SerializationInfo but I don't see how to integrate the booleans to the output. Do I just write out via the Context??? Do I set the info in the GetObject SerializationInfo object?
Any help in how to deploy this would be greatly appreciated.
|
|
|
|
|
You just need to call the base class's implementation after you serialize your own private members (or whatever else you want):
[Serializable]
public class MyNameObjectCollection : NameObjectCollectionBase
{
private int field1;
private string field2;
public MyNameObjectCollection() : base()
{
}
protected MyNameObjectCollection(SerializationInfo info,
StreamingContext context) : base(info, context)
{
field1 = info.GetInt32("field1");
field2 = info.GetString("field2");
}
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("field1", field1);
info.AddValue("field2", field2);
}
}
<div style="width: 100%; font-size: 2px; margin-bottom: -10px; background-color: rgba(255, 153, 0, 1)"> </div>
Microsoft MVP, Visual C#
<a href="http://www.codeproject.com/script/articles/list_articles.asp?userid=46969">My Articles</a>
|
|
|
|
|
Hi everybody,
I'm asking for 2 things:
1-I want to know the equivelant Int value for some messages like WM_SETFOCUS , WM_ACTIVATEAPP ,..etc
From where can I get information about messages like that?
2-After catching a message sent to my application by overriding WndProc(),
Am I allowed to cancel or erase the effect of the received message? and how?
for Example: (When the user presses on Maximize button, I don'twant the window to be maximized).
thanks for Help
|
|
|
|
|
1. If you have Microsoft SDK look in the "include" directory for WinUser.h, it has ALL Windows messages defined there. If you don't have the SDK, either get it or Google for the message codes. If you have VB6 I think the API Viewer has the Windows messages as well.
2. You need to implement the IMessageFilter in some class and it's only method
bool PreFilterMessage(ref Message msg)
Check the message code in msg.Msg and return either 'true' if you want to cancel processing of this message and 'false' if you want to let it through to WndProc. If you want this filter application wide you can add this message filter like so:
MyMessageFilter mf = new MyMessageFilter();
Application.AddMessageFilter(mf);
If you want to process this on just one window, just override the WndProc method. And do not call the base.WndProc after you process a message.
|
|
|
|
|
I wonder if there is an equivalent of C# to the #define prepropcessor directive of C++. The one of C# is not very useful since you can not define macros. Can i use something else, which would perform the same functions. This will really clean my code.
|
|
|
|
|
Simple answer: no, there isn't equivalant (but you can define symbols for compiler)
Complex answer:
Some solutions:
1) Develope own preprocessor that handles macros (shouldn't been too hard to do - but will make your code uncompatible)
2) Use #if (compiler symbols) to "select implementation". For example:
public abstract class DumpableObject : IDumpableObject
{
#if DEBUG
private DumpContext m_contextDump;
private int m_iDumpLevel = DumpContext.DEFAULT_DUMP_LEVEL;
protected int DumpLevel
{
get { return m_iDumpLevel; }
set { m_iDumpLevel = value; }
}
public void DumpAll()
{
DumpContext context = new DumpContext( this, m_iDumpLevel, DumpContext.DumpDetails.ALL );
}
public void Dump( DumpContext context )
{
m_contextDump = context;
OnDump();
m_contextDump = null;
}
protected virtual void OnDump()
{
}
protected void Dump( string strName, object obj )
{
m_contextDump.Dump( strName, obj );
}
#else
protected int DumpLevel
{
get { return 0; }
set { }
}
protected void DumpAll()
{
}
public void Dump( DumpContext context )
{
}
protected virtual void OnDump()
{
}
protected void Dump( string strName, object obj )
{
}
#endif
}
(Yes I know - it doesn't implement all macro tricks - but at least some). Basicly you really should be able to do almost anything without macros (just use sub methods).
|
|
|
|
|
The problem with this method is that my code size actually Increases very rapidely. I wonder how come there is no cheap tricks. Maybe I can Use attributes for that ?
But i will probably have to create a preprocessor directive myself
|
|
|
|
|
HAHAHA_NEXT wrote:
But i will probably have to create a preprocessor directive myself
I itch for that too
I use it (#if) alot. To simulate the enviroment, eg:
#define HELLO
bool Blah(string foo)
{
#if HELLO
return true;
#elseif
return false;
#endif
}
|
|
|
|
|
Hi guys!
My question is a big one as I spent all the day to find out HOW to redraw the scroll bars of a TreeView (or any other control) without finding anything!!!
So my question is how to make the TreeView's scrollbars owner drawn ??
Thanks!
Jean Bédard
|
|
|
|
|
I am using ThreadPool class and I cannot find a way to return data from the callback procedure. Does anybody have any idea how to do that.
Thank for your time
Spiros Prantalos
Miami the place to be!!
|
|
|
|
|
That's what the state parameter is for. Since it's an object - and therefore a reference type - it can be used to pass both input and output arguments. If you want better handling of parameters and return values, use asynchronous methods instead (such as BeginWhatever and EndWhatever ). If you use the proper signature for methods in C#, the compiler can actually create asynchronous methods for your methods. See Including Asynchronous Calls[^] in the .NET Framework SDK for more information.
Also, worker threads are common associated with an object that contains the data with which the callback method operates, so if it also stores state in such properties, you can always retrieve them later when the worker thread finishes, if it finishes at all (some are designed to run continuously - it's all up to your design).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
Try this sites for ideas and information.
http://www.codeproject.com/csharp/XYNetSocket.asp?target=socket%7Cprogramming
http://www.codeproject.com/csharp/socketsincs.asp
http://www.developerfusion.com/show/3918/4/
I hope this will help you.
/dabuskol
|
|
|
|
|
if I ask 'GetPrivateProfileString()' to read file
const string fileName = "c:\\temp\\myfile.ini";
instead of reading this file it reads file
const string fileName = "c:\\temp\\myfile";
In other words it drops the extention '.ini'. I have checked the facts and this is what is really going on.
Thanks for your comments.
Sample source follows.
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
string key = "Key";
string defaultValue = "dftValue";
int size = 255;
StringBuilder result = new StringBuilder(size, size);
const string fileName = "c:\\temp\\myfile.ini";
for(int i=0; i < 5; i++)
{
string section = "Section" + i.ToString();
string outvalue = "Value" + i.ToString();
//WritePrivateProfileString(section, key, outvalue, fileName);
GetPrivateProfileString(section, key, defaultValue, result, size, fileName);
Debug.WriteLine(i + " result " + result);
}
|
|
|
|
|
hi,
can please tell me how do I add function to Explorer context menu (right mouse click on a file)? thanks
Paolo Ponzano
|
|
|
|
|
See Michael Dunn's article at http://www.codeproject.com/shell/ShellExtGuide1.asp and related articles for how this is done using C/C++. If you want to do this using C#, you have to create CCWs (COM-Callable Wrappers) for all the necessary COM interfaces you need to implement, as well as use the GuidAttribute and other attributes from System.Runtime.InteropServices as necessary.
If you know C/C++, you'd be wise to use it to avoid large development overheads, as well as for performance since a managed shell extension would require the CLR to be loaded which has quit a bit of overhead compared to a native DLL.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
|
I was just wondering if there is a way to do a control array for ease of setting up some controls. Basically I am trying to do this (syntax is more psuedo code):
<br />
controlA_View1.Top = controlA_View2.Top;<br />
controlA_View1.Left = controlA_View2.Left;<br />
controlA_View1.Visible = false;<br />
<br />
controlB_View1.Top = controlB_View2.Top;<br />
controlB_View1.Left = controlB_View2.Left;<br />
controlB_View1.Visible = false;<br />
<br />
...<br />
<br />
controlN_View1.Top = controlN_View2.Top;<br />
controlN_View1.Left = controlN_View2.Left;<br />
controlN_View1.Visible = false;<br />
<br />
I was wondering if I could do this:
<br />
Array arrView1;<br />
arrView1.Add(controlA_View1);<br />
arrView1.Add(controlB_View1);<br />
...<br />
arrView1.Add(controlN_View1);<br />
<br />
Array arrView2;<br />
arrView2.Add(controlA_View2);<br />
arrView2.Add(controlB_View2);<br />
...<br />
arrView2.Add(controlN_View2);<br />
<br />
for(int i = 0; i < arrView1.Count() && i < arrView2.Count(); i ++)<br />
{<br />
arrView1[i].Top = arrView2[i].Top;<br />
arrView1[i].Left = arrView2[i].Left;<br />
arrView2[i].Visible = false;<br />
}<br />
I am adding the controls via the form desinger because it is obviously easier to lay them out. I am using my current home project to explore deeper in to C# however I know I could do something like this with pointers or maybe with unsafe code. I remember the ref keyword can cause a reference to be passed to a function but can a ref be passed to an array?
Thanks for any help...
Brian
If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.
|
|
|
|
|
You don't actually declare the variable as Array , but as Control[] , which inherits from Array implicitly:
Control[] controls = new Control[]
{
myControl1,
myControl2,
};
for (int i=0; i<controls.Length; i++)
{
Control c = controls[i];
if (i == 0) c.Location = new Point(8, 8);
else c.Location = new Point(8, 8 + controls[i - 1].Location.Y;
} You could also use a collection or list, but then you either have to cast or use a typed collection or list like Control.ControlCollection .
Also, since you need to add your controls to their container's Controls collection property anyway, you can always enumerate them all after adding them and initialize their properties.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I use a openfiledialog but i want it to return the filename with the extension and nothing more (eg: filename.doc). if i do it like this it returns the complete path:
openFileDialog1.ShowDialog();<br />
txtRunprogram.Text = openFileDialog1.FileName.ToString();
thx in advance
|
|
|
|
|
Try doing this:
string filename = String.Substring(openFileDialog1.FileName.ToString().LastIndexOf("\\"));
That should strip it out for you.
|
|
|
|
|
Or just Path.GetFileName(openFileDialog1.FileName) , which takes platform-dependent directory separators into account making your code more portable.
ToString is also completely unnecessary since FileName already returns a String .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
First of all, you do not need to call ToString on a string! It's already a string.
Second, use Path.GetFileName instead of the previous recommendation. Path.GetFileName takes platform-dependent directory separators into account. If you want to use String.LastIndexOf , then at least use the Path.PathSeparator field to get the platform-dependent directory separator.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
dear all,
i am using crystal reports for vs.net 2002, infact i want to set my report datasource as dataset. i am using AddCommand to reterive required fields. problem is that when i set report data source as dataset it shows all those reocrds on report that are retrived by AddCommand not by my
here is my code
private DataSet myDs()
{
SqlConnection nwindConn = new SqlConnection("DataSource=localhost;Integrated Security=SSPI;Initial Catalog=testDB");
SqlCommand selectCMD = new SqlCommand("testPorcedure",nwindConn);
selectCMD.CommandType = CommandType.StoredProcedure;
selectCMD.CommandTimeout = 30;
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
nwindConn.Open();
custDA.Fill(custDS);
nwindConn.Close();
return custDS;
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
this.ConnectionString();
CrystalReport1 oReport = new CrystalReport1();
oReport.SetDataSource(this.myDs());//dataset that is filled by my sp
crTableLogonInfos = new TableLogOnInfos();
crTableLogonInfo = new TableLogOnInfo();
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = server;
crConnectionInfo.DatabaseName = database;
crConnectionInfo.UserID = user_id;
crConnectionInfo.Password = password;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTableLogonInfo.TableName = "Command";
crTableLogonInfos.Add(crTableLogonInfo);
CrystalReportViewer1.LogOnInfo = crTableLogonInfos;
CrystalReportViewer1.ReportSource = oReport;
CrystalReportViewer1.DataBind();
}
any body kindly help me i realy stuck
thanks in advance
|
|
|
|
|
Why are you providing log into to another source when you're already providing the data in a DataSet ? Using ADO.NET to fill a DataSet (along with a connection string stored in your .config file) is much more robust than hard-coding connection information in your report source. If you ever need to re-deploy the solution elsewhere, you have to change your source and recompile. Instead, make it as abstract as possible. Just configurable connection strings and DataSet s, as you seem to do above, except you're still using connection information on the viewer, for which I really don't understand the point.
Microsoft MVP, Visual C#
My Articles
|
|
|
|