|
Hi !
I'm new to this whole .NET framework stuff. Till now I was working with a team by some CAE projects. Our programs have been using binary files to store results and datas. So now I decided to write an implementation of reading these binary files in C#. In Delphi it look something like that:
- define a packed record
- write it to the stream with TStream.Write() method.
The TStream.Write() method took two arguments: one was a reference to a buffer and second was the buffer size.
As far as I can see there is no such possibility to read a whole bunch of data at-once like this. Can you please tell me how do I do that (write a bunch of data directly to the stream) using the .NET framework architecture ?
The goal is to create a next version of these apps that are taking advantages of the .NET framework and the C# programming language (which is btw really more readable than the Pascal syntax for me) but leaving the possibility to read old projects by new users.
Best regards - Matthias Hryniszak
|
|
|
|
|
Doing so isn't very wise because it can easily break by the addition, removal, or changing of position of an element. Every C book I've read has also pointed out that doing so is a bad idea.
Because it is a bad idea (and generally mis-used) the framework doesn't support it in the form you are looking for. The alternative is to use the Serialization classes, but since you have an existing format to follow those won't work for you.
The next best thing is to just read the data in and interpret it as you go. I'm not aware of how Delphi/Pascal format binary data so I can't comment on any differences. But if memory serves Pascal uses length-prefixed strings instead of null-terminated strings. This could potentially be one problem you'll encounter.
I hope that gives you some direction on where you should go from here.
James
At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven"
|
|
|
|
|
I forgot to mention that if you didn't know, the Stream object has support for reading/writing an array of bytes to the stream (getting the bytes to be in the correct format for reading/writing is the issue).
You can use the BinaryWriter/Reader class to read/write the basic data types in a binary format. You can use a class extending from TextWriter/Reader to do the same with a file in a text format.
James
At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven"
|
|
|
|
|
I got that - the reason I didn't wanted to use it is that it supports only reading of a single data at once instead of a whole record. As for now I see that I'll have to create some methods that read/write a bunch of fields decomposing it to silgle fields.
Anyway - thnx a lot
Matthias.
|
|
|
|
|
Matthias,
If you want to do it the old way, you can define a structure that mirrors what you want, and then use unsafe to copy the data from a byte array to the struct:
MyStruct myStruct;
byte[] data = readdata(sizeof(myStruct));
fixed (byte* pData = data)
{
myStruct = *((MyStruct*) pData);
}
This will require unsafe code and full trust when you execute, and works only if you don't have any reference types (like strings or arrays) within the struct.
|
|
|
|
|
I know the name of the form but I have to create it usig strings how to do in reflection?
nick
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
You can use the static method, Type.GetType , passing in the full class name of your form. Assuming that assembly has been loaded into the AppDomain a Type object for your form will be returned.
You can then use one of the Activator.CreateInstance overloads to create a new instance of the class represented by that Type object. Cast the returned object back to a Form and you can use it as normal.
public Form GetForm(string className)
{
Form form = null;
Type formType = Type.GetType(className);
if( formType == null )
return null;
form = (Form) Activator.CreateInstance(formType);
return form;
} Its untested, but that should work for you.
James
At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven"
|
|
|
|
|
James T. Johnson wrote:
Type formType = Type.GetType(className);
Now how many times has that worked for you? MIne doesnt 99% of the time.
leppie::AllocCPArticle("Zee blog");
|
|
|
|
|
leppie wrote:
Now how many times has that worked for you?
Personally, I've never used it. Usually I use reflection to load the assembly and get all of the Type objects that match certain criteria so I don't need to use Type.GetType. Type.GetType is just much easier to type out than to go through all the reflection code
James
At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".
|
|
|
|
|
|
I have a class with 10 properties. Within one of the class methods, I'd like to iterate through the 10 properties rather than referencing each one manually. So instead of this:
this.property1=
this.property2=
this.property3=
I'd like to do something like this:
foreach( property p in this.properties )
{
p.value =
...
}
Is there a way to do this?
Using reflection, I can get the properties and iterate through them, but it's a blank object. I want to be able to do that with an existing object where the properties contain valid data values.
Thanks in advance.
|
|
|
|
|
what do you mean its a blank object
get the Type()
then get the propertyInfo for each one
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
One idea - since you are accessing from inside the class, have the property values in an ArrayList:
private enum MyProperties {Prop1, Prop2, Prop3, ...};
private ArrayList m_PropVals = new ArrayList ();
public object property1
{
get
{ return m_PropVals[MyProperties.Prop1]; }
}
...
private void foo()
{
foreach (object p in m_PropVals)
{
p = bar;
}
}
Otherwise, its System.Reflection for you:
private void foo()
{
foreach (PropertyInfo propInfo in this.GetType ().GetProperties ())
{
propInfo.SetValue (this, bar, null);
}
}
α.γεεκ Fortune passes everywhere. Duke Leto Atreides
|
|
|
|
|
The second way was what I was looking for. Thanks!
The first way is also an interesting idea, but won't work for what I'm trying to do.
|
|
|
|
|
Hi there, I think the subject simply describes the point of my problem (I want to get the decimal ASCII codes of these characters like "k" or "S" and so on...) Thanx...
|
|
|
|
|
|
Thanx This is what I need...
|
|
|
|
|
How to communicate a aspx page with a thread ( one thread per session )?
I try to build a thread that be able to communicate with a aspx (C#).
Basically, this thread launches a special procedure which can take several minutes. Also, the aspx page refreshes automatically every 5s(meta http-equiv=refresh content="1,qsdf.aspx”) and it shows the status like a progress bar. Currently, I can launch and execute a separate thread in my Web Sessio n. But I could not communicate by event and collection class.
For example. In my main aspx,I create a new instance of my Collection(ArrayList) (private list { set{ Session[“List”] } get{return (LIST)Seesion[“ref”] }}). In my constructor thread, I take this List(ref) to pass like parameter. But apparently my collection has not been modified by my thread when I try to read that in my aspx page.
Could you help me?
Where can I find some documentation about that?
Thank in advance …..
Bye bye
Ps=the thread is saved in the session variable
-=zoltx=-
|
|
|
|
|
Hi All,
I m new to C#, and i want to know how could i detect the double click Event on a Row in DataGrid Control.
Any Response will be appreciated.
Thanx in advance
cheers
|
|
|
|
|
hi !
that's a realy great link for datagrid stuff:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
andi
--
still not having a meaningful aphorism to use as a signature
|
|
|
|
|
In a winform, before saving, I call the function this.Validate() to make sure all controls contents are good (verifications are made in the Validating event of each control).
In some cases, the call to the validate() function returns false. How can I know which control causes the validation to fail ?
Thanks for help,
Nico
|
|
|
|
|
I wonder how fast is .NET and especially some of its components.
As everyone can read pinvoke is slow. Does it make sense to uses it in real applications (applications you want to sell and to be sure that there aren't any problems for you and your customers) for cases where there is no real need to to it like creating fancy custom GUI?
How fast is GDI+? Does it depend on CPU power or it just requires powerful 2D (or 3D) accelerator?
For me SharpDevelop is an example for a very slow application. I wonder if all .NET applications with complex GUI will be as slow as SharpDevelop. (I think that WebMatrix is also completely written for .NET and it is totally fast!!! Does this mean that SharpDevelop is not written well enough?)
Should one be careful when using GDI+ for creating new custom windows forms controls or for stuff like CAD drawing, bitmap and vector manipulation programs? Eventually if these programs are executed on latest CPUs and graphics accelerators they will run fast but what about old and slow systems like: P90 or even P400 with old slow TNT or worse graphic cards?
What is your oppinion? Could you, please, enlighten me? I'm talking here about commercial use and not about free time development ;_)
Thank you!
|
|
|
|
|
I certainly hope I formulated my questions clear enough for you to understand and that they make sense.
|
|
|
|
|
|
Excellent article, BTW, Nish! Didn't get a chance to say it before.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|