|
Well, I've just got that thought. In my Visual Studio, the "Check for arithmetic overflow/underflow" option is not enabled. I'm pretty sure that's the reason. The compiler neglects the overflow (data loss which is fine in this case). But I’m curious; the code you provided does the same (casting from int to byte if I’m not wrong).You're right. ANDing with 1 removes possible overflow.
Eslam Afifi
modified on Monday, June 16, 2008 11:32 PM
|
|
|
|
|
Thanks for all the reply. But I should get the value as
byArrData[4] = E8;
byArrData[5] = 03;
If i do
byArrData[4] = (byte)((unUserID) >> 8);
byArrData[5] = (byte)(unUserID);
I'll get 232 & 03.
How should I get the hex value? Please help
|
|
|
|
|
Actually, everything is represented in binary. A byte is 8 bits an you're storing the values in byte[]
232 is 11101000 in binary. The way you see it (decimal, binary, octal, hexadecimal ...) doesn't matter because it's actually stored in binary.
If you want to get a string representing the value in hexadecimal, use ((byte)(unUserID >> 8)).ToString("X") // this returns a string
Eslam Afifi
|
|
|
|
|
When I tried that "Cannot convert from string to byte" error is coming. Any solution.
|
|
|
|
|
|
I would require the code/methods for detecting different firewalls installed on a system. Though I have done this for Windows Firewall, I would require help in detecting other firewalls installed.
-----Have A Nice Day-----
|
|
|
|
|
I think you can do this with WMI, You should find a reference on that to help you.
See here[^], read the question on "How does Windows Security Center detect third-party products and their status?"
Simon
|
|
|
|
|
I have to switch between forms in my application. I don't need classical MDI look and feel. So I created all my forms as controls. Created a single form with a panel and the navigation panel. When items are clicked on the navigation panel, I am adding corresponding control to the panel. It works for the first time, but when I click 2nd time on the button, it's not showing. So I called Panel.Controls.Clear() before I add controls. It is working fine now.
Is this approach correct ? Or is there any other efficient methods available ? Here is sample code
this.panel1.Controls.Clear();
form1.Dock = DockStyle.Fill;
form1.BringToFront();
form1.Visible = true;
this.panel1.Controls.Add(form1);
NOTE : form1 is a user control here and not a form.
Any ideas will be helpful.
|
|
|
|
|
You should probably just add all the usercontrols to the panel1.controls collection at the beginning, then show and hide the usercontrols (by setting the visibility) as necessary. This will save you clearing the collection and recreating each usercontrol each time you show it.
Simon
|
|
|
|
|
Hello everyone,
From the IL for the following code, seems there is default constructor for both value and reference types which accepts null argument? Are they added by CLR directly (from .locals init IL statement)?
class Program
{
struct Foo
{
int abc;
}
class Goo
{
public int abc;
}
static void Main(string[] args)
{
Foo f;
Goo g;
return;
}
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 0
.locals init (
[0] valuetype TestStruct1.Program/Foo f,
[1] class TestStruct1.Program/Goo g)
L_0000: nop
L_0001: br.s L_0003
L_0003: ret
}
thanks in advance,
George
|
|
|
|
|
Yes, according to the spec[^].
"All value types implicitly declare a public parameterless instance constructor called the default constructor."
|
|
|
|
|
Thanks Senthil,
1. For value type, even if we provide a customized constructor, there is also the default one (co-exist with customized one)?
2. For reference type, there is also a default constructor even if we provide a customized one (co-exist with default one)?
regards,
George
|
|
|
|
|
George_George wrote: For value type, even if we provide a customized constructor, there is also the default one (co-exist with customized one)?
NO
George_George wrote: For reference type, there is also a default constructor even if we provide a customized one (co-exist with default one)?
NO
|
|
|
|
|
ForumExpertOnLine wrote: George_George wrote:
For value type, even if we provide a customized constructor, there is also the default one (co-exist with customized one)?
NO
Wrong - the default constructor exists, whether you create a overloaded one or not.
struct S
{
int x;
public S(int x)
{
this.x = x;
}
public int X
{
get { return x; }
}
}
class Program
{
static void Main(string[] args)
{
S s = new S();
Console.WriteLine(s.X);
}
}
|
|
|
|
|
Ahh - I didn't noticed that. Thanks for pointing it.
|
|
|
|
|
Great Senthil!
I think the conclusions are, could you help to confirm? Correct?
1. For value type, whether or not we provide a customized constructor, there is a default one (co-exist with customized one).
2. For reference type, there is also a default constructor, whether or not we provide a customized one (co-exist with default one).
regards,
George
|
|
|
|
|
George_George wrote: 1. For value type, whether or not we provide a customized constructor, there is a default one (co-exist with customized one).
No, but you can always create value types without calling a constructor.
If you for example declare an array of value types, the array will be filled with the default value of the value type (which is all members set to zero).
George_George wrote: 2. For reference type, there is also a default constructor, whether or not we provide a customized one (co-exist with default one).
No, if you create a custom constructor, there is no default constructor automatically created for you.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
I think for value types, only the memory space is reserved, you can not access the fields if you only "create" them with default constructor and not initializing to them. Right?
regards,
George
|
|
|
|
|
George_George wrote: I think for value types, only the memory space is reserved, you can not access the fields if you only "create" them with default constructor and not initializing to them. Right?
That is only if you declare a value type as a local variable in a method. If you declare a value type as an instance member in a class or declare an array of value types, they are initialised (set to all zero).
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
I have a further question. For code like this,
struct Foo
{
public int abc;
}
Is there a default constructor generated which is parameter-less? From IL code, I can not see. Any comments?
regards,
George
|
|
|
|
|
George_George wrote: For code like this,
struct Foo
{
public int abc;
}
Is there a default constructor generated which is parameter-less?
No, the compiler doesn't create a parameterless constructor. It exists in theory, and you can use it, but the compiler translates that into simply clearing all members of the structure.
This code:
Foo x = new Foo();
compiles into this IL code:
L_0000: ldloca.s x<br />
L_0002: initobj Foo
The initobj instruction sets the bytes to zero in the memory area used by the structure.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Great Guffa!
Question answered.
regards,
George
|
|
|
|
|
Guffa wrote: George_George wrote:
1. For value type, whether or not we provide a customized constructor, there is a default one (co-exist with customized one).
No,
Actually yes, there is always the default parameterless constructor, regardless of whether you overload it or not.
|
|
|
|
|
S. Senthil Kumar wrote: Actually yes, there is always the default parameterless constructor, regardless of whether you overload it or not.
Yes, in theory the value type has a parameterless constructor. It never exists as an actual method.
This source code:
int x = new int();
actually results in this code being created by the compiler:
int x = 0;
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
George_George wrote:
1. For value type, even if we provide a customized constructor, there is also the default one (co-exist with customized one)?
Yes. Read the spec[^] again.
George_George wrote: 2. For reference type, there is also a default constructor even if we provide a customized one (co-exist with default one)?
Read the spec (http://www.jaggersoft.com/csharp_standard/8.7.8.htm[^]) again.
"If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided."
|
|
|
|