|
Hi,
inside class B you have a delegateMethodA which is null, and never changes, hence MethodB() will never succeed. the delegateMethodA inside class Program is unrelated.
You really should use an event to "link" both classes.
I suggest:
- you read up on delegates and events;
- you study an introductory book to C#;
- you look at how Visual Studio and Visual Designer generate code when connecting a Button to its click handler.
PS: please use PRE tags (e.g. through the "code block" widget when showing code), as it results in better readability.
|
|
|
|
|
Hello,
i'll try to be concret on my problem.
I want to convert a string: "alex" to bits (by bits I mean 0 and 1) and then write them into a binary file.
There are many ways to do this, but i've selected this:
private void MyWriteMethod()
{
byte[] bytes = UTF8Encoding.Default.GetBytes("alex");
Stream str = File.Create("test.bin");
BinaryWriter bw = new BinaryWriter(str);
BitArray bits = new BitArray(bytes);
for (int i = 0; i < bits.Length; i++)
{
bw.Write(bits.Get(i));
}
}
My problem is:
All goes well, but when I look for my file it occurs 32 bytes on my HDD.
Now to do a comparison, I create a new file and write by hand the text "alex". When I look for this new created file, I see that it occurs 4 bytes!!!!
My questions are:
1. Why is that ?? Can anyone explain to me this phenomenon ??
2. Can I occure the same amount of bytes using the bits ?
3. Afther that may I recive a code sample on how this can be done? I mean: how can I write bits to a binary file?
Thanks you for you're patience!
Alex Manolescu.
modified on Tuesday, December 29, 2009 3:58 AM
|
|
|
|
|
oldsellerros wrote: 1. Why is that ?? Can anyone explain to me this phenomenon ??
It's lazy and writing a whole byte. It could save up bools in batches of 8 and flush them when there are 8 or when something else is written, but they just didn't make it like that.
oldsellerros wrote: 2. Can I occure the same amount of bytes using the bits ?
Could you try to translate that again, maybe using a different translator?
oldsellerros wrote: 3. Afther that may I recive a code sample on how this can be done? I mean: how can I write bits to a binary file?
It has been done countless times before, just search codeproject/google for "bitstream C#" or something like that. Or you could make one yourself, just collect bits until you have 8 of them and then write them all at once in a byte. The exact details will depend on whether you want bytes/ints/etc to be byte-aligned or not
|
|
|
|
|
Thanks you verry much for answer! It really helps me.
Sorry for my mistake at question 2, it's about 3:15 AM in my country now and today is my birthday! i'm 22
At question 2 I mean:
Supose I have a file, 1.dat, with "alex" text in it. Can I write the bit array "alex" to a binary file that occurs the same amount of size like the fist file? something like that was in my mind.. hope you'll understand me!
Have a wonderfull day and Happy New Year!
I've got the answer now!
|
|
|
|
|
Happy birthday/new year
oldsellerros wrote: Supose I have a file, 1.dat, with "alex" text in it. Can I write the bit array "alex" to a binary file that occurs the same amount of size like the fist file?
It's still a bit unclear to me, but the answer to what I think you mean would be yes
On the other hand, the file 1.dat is probably 4 bytes, right? In that case writing the string "alex" as 4 ASCII chars (or even as UTF8 since it's all below 127 anyway) should make the same file (at least in size, the bit-order could be different, depending on how you're writing the bits)
|
|
|
|
|
You're a genius ! Thanks for understanding. Now it's clear to me.
I'm verry grateful to you.
May I add you to my friends list? or something like that?
Could I have you're e-mail adress, I fell that I need to make you a gift for you're help!
Good luck!
Alex Manolescu.
|
|
|
|
|
You're welcome
Help here is free of course, but you can vote 5 for my posts if you really feel they helped you
|
|
|
|
|
I've voted 5 for you.
Thanks you for help!
|
|
|
|
|
Hi,
in a file system such as FAT or NTFS, files get allocated in multiples of sectors (512B) or clusters (several sectors). So you can't create a non-empty file of less than 512B of disk space.
the logical file length is expressed in bytes, so you can't create a non-empty file of less than 8 bits.
BitArray.Get() returns a bool; BinaryWriter.Write(bool) writes a byte, the doc says "Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true." That could be lazyness, or a defense against unaligned data. If it were to write a single bit, a consequent Write(int) would be misaligned, and the total logical length would not necessarily be a multiple of 8.
|
|
|
|
|
Thanks you verry much for you're quick answer!
I am verry grateful!
Happy New Year!
|
|
|
|
|
Hey,
I was wondering what this is called:
Class MyClass<MyType>
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
Hi,
class MyClass<MyType> is "generics", introduced with .NET 2.0
|
|
|
|
|
Oh ok, thanks
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
No problem.
BTW: did you notice how the C# forum turns VB code into C# automatically?
|
|
|
|
|
By applying random mutations to my code, I found for myself how it was done in C#.
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
|
Woha, that's neat.
* adds bookmark *
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
Here goes, I'm trying to read Unicode text in from a ini/uni file.
I've made the call to GetPrivateProfileStringW
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileStringW(
string lpAppName,
string lpKeyName,
string lpDefault,
string lpReturnString,
int nSize,
string lpFilename);
And this is what I use at the point of actual calling.
string returnString = new string(' ', 32768);
GetPrivateProfileStringW(fetchUniSection[n], fetchUniInfo[i], "FAIL", returnString, 32768, containingFolder + @"\BiblioSelfCheckDefault.uni");
I get the text, but rather than be in proper Chinese, Japaneses, Arabic, etc.. it is just garbage characters.
Can anyone tell me what I am doing wrong? As far as I can tell, it is taking my input and forcing it onto ASCII.
Thanks!
-Elmernite
|
|
|
|
|
Hi,
AFAIK you need to pass a StringBuilder, not a string, for native strings that must be writable.
I have an article[^] in progress that shows an example.
|
|
|
|
|
After switching to StringBuild I now get
"Warning: A StringBuilder buffer has been overflowed by unmanaged code. The process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."
Forgive my lack of understand... But what does that mean?
---
I solved that by increasing the stringbuilder size.
Here is my new code, but I am still getting mangled results like below.
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern int GetPrivateProfileStringW(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnString,
int nSize,
string lpFilename);
StringBuilder returnString = new StringBuilder(5000);
GetPrivateProfileStringW(fetchUniSection[n], fetchUniInfo[i], "FAIL", returnString, returnString.Capacity, containingFolder + @"\BiblioSelfCheckDefault.uni");
Then I just use returnString.ToString(), store it in a string variable, and later I place it in a text box.
But in the text box, instead of the other language, I get
:رسوم الاÙتتاØ
-Elmernite
modified on Tuesday, December 29, 2009 10:40 AM
|
|
|
|
|
Hi,
I don't see anything that is fundamentally wrong.
I never used GetPrivateProfileString myself.
However:
- I would drop the "SetLastError = true" part, as I don't see anything about GetLastError in the MSDN documentation here[^]
- I trust your filename is ...BiblioSelfCheckDefault.ini not .uni
Hope this helps.
|
|
|
|
|
I dropped the "SetLastError" part, still the same results.
Actually the file is .uni not .ini, which as far as I can tell, .uni is simply an .ini file the denotes it can handle Unicode.
I guess I can manually sort through the uni/ini file and access the text that way and maybe it will be in proper unicode.
Thanks for the assistance!
-Elmernite
|
|
|
|
|
hi guz.
i wonder how can i use open gl with C# 08 express edition...
Plz rply me as soon as possible as i hav to submit a project related to these befor 8th jan,2010.
Awais
|
|
|
|
|
Message Closed
modified 23-Nov-14 7:14am.
|
|
|
|
|
That's really really old and quite buggy, better use the Tao Framework[^] these days (though it also seems quite dead now)
|
|
|
|
|