|
assume u want to make them all the same size of f1 then
in f2 do that:
f1 TempForm=new f1();
f2.Size=f1.Size;
the same for f3 and f4.
|
|
|
|
|
I overload operator== in a class, say, MyClass, like this
public static bool operator==(MyClass lhs, MyClass rhs) {<br />
return lhs.someValue == rhs.someValue;<br />
}
Which will fail if either lhs or rhs is null. Fix
public static bool operator==(MyClass lhs, MyClass rhs) {<br />
try {<br />
return lhs.someValue == rhs.someValue;<br />
} catch (Exception) {<br />
return false;<br />
}<br />
}
But this will result in the following code not working
void MyFunction(MyClass anInstance) {<br />
if (anInstance == null) {<br />
}<br />
}
Fix
public static bool operator==(MyClass lhs, MyClass rhs) {<br />
if (lhs == null && rhs == null) {<br />
return true;<br />
}<br />
<br />
try {<br />
return lhs.someValue == rhs.someValue;<br />
} catch (Exception) {<br />
return false;<br />
}<br />
}
But this causes a stack overflow since the check on "lhs == null" calls operator== again ...
Help someone pls
-- modified at 15:05 Tuesday 10th January, 2006
|
|
|
|
|
I've never done anything with operator overloading, this is just a guess tho:
if(Object.Equals(lhs, null) && Object.Equals(rhs, null)) return true;
Dunno if this works, but it's worth a try.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
You're right check works!
And as I see it, you have to do this whenever you overload operator== !!!
Thx
|
|
|
|
|
Only overload operator== on value (i.e., non-reference) types such as structures and enumerations. You'll be much safer.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
FxCop says that you should not override operator== on reference types ]^].
Instead, override Object.Equals( object ) and use that in your code:
MyClass a = new MyClass( someValue );
MyClass b = new MyClass( someValue );
MyClass c = new MyClass( anotherValue );
a.Equals( b );
a.Equals( c ); Now, you can handle null s.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Ok, and that advice might be due to the exact reason i've just discovered.
Yes using Equals will do the trick, but the code is much less intuitive to read, as opposed to some syntactic sugar, like "a == b"
Thx
|
|
|
|
|
Whoa, that FxCop found 100 errors/warnings in my little 3D-engine. Is there a reason to be afraid now?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
<chuckle>No, not at all!</chuckle>
FxCop just contains the "best practices" as defined by Microsoft and some other folks. <smile /> I often hit the ignore button because I need to use an out or ref param in my code. They don't like that.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Casting to object will cause the default == operator to be called:
Use this:
public static bool operator==(MyClass lhs, MyClass rhs) {
if ((object)lhs == (object)rhs) {
return true;
}
if ((object)lhs == null || (object)rhs == null) {
return false;
}
return lhs.someValue == rhs.someValue;
}
|
|
|
|
|
Don't you mean
public static bool operator==(MyClass lhs, MyClass rhs) {<br />
if ((object)lhs == null && (object)rhs == null) {<br />
return true;<br />
}<br />
if ((object)lhs == null || (object)rhs == null) {<br />
return false;<br />
}<br />
return lhs.someValue == rhs.someValue;<br />
}
Otherwise Object.operator== will be used for all comparisons, as I see it.
Thx
|
|
|
|
|
No, the first if ("if ((object)lhs == (object)rhs) {") will only return true when the instances are the same, ie when both are null or when both point to the same instance (in that case, the values will always be the same).
When they point to different instances, the if-block will not be entered and the next statment will compare the values.
|
|
|
|
|
|
I am trying to create a mailbox from a web applicattion that it is not in a domain controller (a webservice) using CDOEXCH. The code works fine when the web service is in the domain controller but no way out of it. If we put the code in a Windows applicattion then everything work fine even in a non domain controller computer. We have try to impersonate the asp, or even the application pool. The security context was ok but there is something wrong with any permission or something like that though the credentials have domain administrator rights.
I have tried to encapsulate the code in a COM+ object and impersonating with administrator rights but I didn´t success. I copy the general idea from our code and the return error. Any idea wolud be really apreciate
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
//This creates the new user in the "users" container.
//Set the sAMAccountName and the password
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);
user = container.Children.Add("cn=" + fullName, "user");
user.Properties["sAMAccountName"].Add(alias);
user.CommitChanges();
user.Invoke("SetPassword", new object[]{password});
//This enables the new user.
user.Properties["userAccountControl"].Value = 0x200; //ADS_UF_NORMAL_ACCOUNT
user.CommitChanges();
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
RETURN ERROR:
Unable to create a mailbox for the specified user. Using homeMDB path: LDAP://PRSRVDCCAM1.redcam.local/CN=Mailbox Store (PRSRVDCCAM1),CN=First Storage Group,CN=InformationStore,CN=PRSRVDCCAM1,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=CAM,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=REDCAM,DC=local. Inner exception attached from: mscorlib. Current user context: REDCAM\AdministratorWith error
Facility: LDAP Provider
ID no: 80072020
Microsoft CDO for Exchange Management
|
|
|
|
|
I don't know if this helps, but according to the article ASP.NET Web Application Security[^] from MSDN, ASP.NET has client impersonation set to false. I don't know if this affects your ASP.NET application (I don't do that kind of thing), but I thought it might be a direction for you to look.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Hi:
Is it possible for showing when an option has been toggled on/off on a menu? If so, how do I program it?
Thanks, Smurfy
|
|
|
|
|
Well, first you need to set the MenuItem 's Checked property to the appropriate Boolean value.
From here, you have a couple of choices.
1) If the user can only change the state of the application through clicking that MenuItem , then hook up an event handler to the Click event of the MenuItem (which I'm sure you already have) and set the MenuItem 's Checked property equal to the Boolean inverse of its current value. For example: menuItem1.Checked = !menuItem1.Checked; .
2) If the state of the application can change without the use of the MenuItem , then hook up an event handler to the MenuItem 's parent MenuItem 's Popup event. In that event handler, have something like targetMenuItem.Checked = ShouldICheckThisMenuItem(); where ShouldICheckThisMenuItem() is some method that returns a Boolean value determining if the MenuItem should get checked.
Hope that helps.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
I have a window form and I have label with the text "open file" on the form. I want the text "open file" to appear in red (not the defaul black). How can i do it?
THX.
|
|
|
|
|
Set the Label 's ForeColor property to the desired color. You can do this in the form designer in the properties pane (easier), or in the code (easy).
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
I suggest to use MSDN for quick help!! as it is so powerful and quicker
|
|
|
|
|
I hava a situation where I have a class that contains objects os other classes that are to be readonly (the whole thing is initialized in the constructor, and the gris is used only to show the results).
The problem is that the TypeConverters for some of the inner objects are allowing to edit the object:
using System;
using System.ComponentModel;
namespace PropertyTest
{
public class SimpleClass
{
private NestedClass _xy;
private int _z;
public NestedClass XY
{
get { return _xy; }
}
public int Z
{
get { return _z; }
}
public SimpleClass()
{
_xy = new NestedClass(1, 2);
_z = 3;
}
}
[TypeConverter(typeof(NestedClassConverter))]
public class NestedClass
{
private int _x;
private int _y;
public int X
{
get { return _x; }
set { _x = value; }
}
public int Y
{
get { return _y; }
set { _y = value; }
}
public NestedClass(int x, int y)
{
_x = x;
_y = y;
}
}
public class NestedClassConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(NestedClass))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(System.String) && value is NestedClass)
{
NestedClass nc = (NestedClass)value;
return nc.X.ToString() + "," + nc.Y.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
If I create an object of type SimpleClass, I want the property of type NestedClass to be readonly on the propertygrid. How can I do that?
Luis Barreira
|
|
|
|
|
|
I've tried that, and I also tried declaring the variable as
private readonly NestedClass _xy;
but it still let's me edit XY's properties...
|
|
|
|
|
|
Hi,
I have a C# application by using Access database. I developed it on Windows 2000 professional, evrything is fine.
But when I create a setup project, and install it on window xp home, I can n't connect to the database (I got a message, said db connection is closed!!).
I didn't install office on the xp home.
and the followin is the connection code:
OleDbConnection con=null;
string sConnection;
string sFile=Application.StartupPath + "\\cwads.mdb;";
sConnection="Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + sFile
+ "User ID=Admin;"
+ "Jet OLEDB:Database Password=password";
try
{
con=new OleDbConnection(sConnection);
con.Open();
}
catch(Exception){};
Please help me out of this!!!!
Thanks!!
Alan Shen
MCAD for .NET Version
^~^~^~^~^~^~^~^~^~^~^
Great idea is the beginging of success!
|
|
|
|