|
Hi friends..
I am using bitmap to draw graphics object...How can i perform cut ,delete function ...
currently i done cut, Delete, zoom operation by the following way
for cut and delete
1)Delete the particular graphics object in storage and redraw the remaining graphics object
for zoom
2)Creating new bitmap based on zoom level and redraw all the graphics object..
|
|
|
|
|
Hi,
Can anyone please tell me the difference between virtual function Equals(), static function Equals() and Static function ReferenceEquals() defined in Object class. I searched a lot, but didnt get a clear idea about their differences. Am confused with these three. Can anyone give a small example with the output in these 3 cases.
Regards
modified on Wednesday, April 2, 2008 2:10 AM
|
|
|
|
|
Darmi wrote: difference between virtual function Equals(), static function Equals()
Virtual function Equals(object) checks the current instance with the supplied instance. Consider the following code
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
bool b = obj1.Equals(obj2); here obj2 will be compared with obj1 .
Static Equals(object1,object2) checks the supplied instances (object1 and object2 here).
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
bool b = MyClass.Equals(obj1,obj2);
Darmi wrote: Static function ReferenceEquals()
This checks the the supplied object instances are having same reference.
|
|
|
|
|
When you call Object.Equals(o1, o2) the static version, internally that function first checks to see if the references are not null, and then calls the virtual method o1.Equals(o2). I just checked that using Reflector so that's accurate. Now here's where it can get tricky, if you do not override the Equals method, when o1.Equals(o2) is called in the static function, it just checks object references. I'm going to repost the code that I previously posted.
namespace PersonTest {
class Program {
static void Main(string[] args) {
Person p1 = new Person { Member = "ABC", OtherMember = 1 };
Person p2 = new Person { Member = "ABC", OtherMember = 1 };
Console.WriteLine(Object.Equals(p1, p2).ToString());
Console.WriteLine(Object.ReferenceEquals(p1, p2).ToString());
Console.ReadLine();
}
}
public class Person {
public String Member { get; set; }
public Int32 OtherMember { get; set; }
public override Boolean Equals(Object obj) {
return obj is Person ? Equals(obj as Person) : false;
}
private Boolean Equals(Person obj) {
if (Object.ReferenceEquals(this, obj)) return true;
if ((obj == null) || (this == null)) return false;
return obj.Member == this.Member && obj.OtherMember == this.OtherMember;
}
public override int GetHashCode() {
return base.GetHashCode();
}
}
}
Put this into a project and the output will be True False. Then comment out both of the Equals methods in the Person class and the output will be False False. The reason is that you have 2 different references to Person classes. Regardless of the EQUALITY of the classes the IDENTITY will always be different. Object.ReferenceEquals is always IDENTITY. Object.Equals starts by checking EQUALITY, but then defaults to IDENTITY in the absence of an overridden Equals() method in your class. That's why when you comment out the Equals methods in the Person class, the Object.Equals(p1, p2) the method goes to IDENTITY and not EQUALITY. You have to supply the EQUALITY.
I hope this rather thorough explanation helps.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Working in C# ASP.NET 2.0
I am new to C# development, looking to get the unique key for a user to use as a foreign key in other tables.
I must be missing something, but is there a way to get this without a query to the asp_net Profile table?
Thanks.
|
|
|
|
|
Alright now I have a really strange bug thats popping up in my code.
Some Before hand details.
Language: C#
Framework Version: 2.0
IDE: Visual Studio 2008 Team Edition
Now to the real problem
I'm working on a project and because it is based on a plugin engine I have to pass the current context of the system to the plugin. So
I use the Singleton Pattern alot. Basic snippet below.
public class SomeClass
{
private static SomeClass _current = null;
public static SomeClass Current
{
get
{
if(_current == null)
{
_current = new SomeClass();
}
return _current;
}
}
public SomeClass()
{
}
}
Now that seems pretty normal and correct to me but this is how it executes. Just watch the little arrow.
public class SomeClass
{
private static SomeClass _current = null;
--> public static SomeClass Current
{
get
{
if(_current == null)
{
_current = new SomeClass();
}
return _current;
}
}
public SomeClass()
{
}
}
Ok good so far.
public class SomeClass
{
private static SomeClass _current = null;
public static SomeClass Current
{
get
--> {
if(_current == null)
{
_current = new SomeClass();
}
return _current;
}
}
public SomeClass()
{
}
}
Still ok
public class SomeClass
{
private static SomeClass _current = null;
public static SomeClass Current
{
get
{
--> if(_current == null)
{
_current = new SomeClass();
}
return _current;
}
}
public SomeClass()
{
}
}
Alright we are there..
public class SomeClass
{
private static SomeClass _current = null;
public static SomeClass Current
{
get
{
if(_current == null)
{
_current = new SomeClass();
}
--> return _current;
}
}
public SomeClass()
{
}
}
Thats right.. It evaluates _current == null as false but clearly I have it assigned to null.. and it never executes my constructor.
So I have no idea whats going on.
By the way this happens in a .DLL thats being referenced by a web project and I first noticed this issue when testing out a class with one
of my unit tests.
So basicly what I'm asking is if someone with more knowledge than me can help me figure out whats going on cause no matter what I do it always seems to skip that _current = new SomeClass() statement.
But whats even more confusing is I have another class thats exactly the same (except the named of course) and it executes and works fine. So I dont know.<
|
|
|
|
|
I was interested by this one so I thought I'd try it out. My full code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
SomeClass theClass = SomeClass.Current;
theClass.RunMethod();
Console.ReadLine();
}
}
public class SomeClass
{
private static SomeClass _current = null;
public static SomeClass Current
{
get
{
if (_current == null)
{
_current = new SomeClass();
}
return _current;
}
}
public SomeClass()
{
Console.WriteLine("Constructor");
}
public void RunMethod()
{
Console.WriteLine("Method");
}
}
}
As you'd expect, when I run this, it prints out both strings. So then I started to debug it to see what was happening. As I debugged, I hovered my mouse over the various variables as I always do to check the state of things, and lo and behold, it skipped straight over the if statement just like you described, and I realised what might be happening.
SomeClass.Current is a static member that creates the instance the first time it is called. That can include being called by any attached debugger trying to look at the static member, so when I hovered my mouse over the SomeClass.Current bit, that action kicked off the constructor, so when I stepped into the routine it had already run.
The really interesting thing about this is that if you stick a break point in the constructor it never gets hit! The Visual studio debugger must disable break points when it runs code for property evaluation.
I suggest you have a look in your watch window and see if you've got a watch on the static member that will be having the effect of calling the constructor. You could also try sticking a Debug.WriteLine in the constructor and check that it does indeed output something.
(Incidentally, you should bear in mind that your singleton isn't thread safe. It might not matter for your use, but you should take a look at this article for a discussion of the singleton pattern in c# http://www.yoda.arachsys.com/csharp/singleton.html[^])
Simon
|
|
|
|
|
I think Simon has probably nailed this, but why bother checking for null in the first place. Static constructors only get called once so you can infer it will always be null when you get there.
It's a good pattern for Singletons. Threadsafe to boot as well.
Regards,
Rob Philpott.
|
|
|
|
|
Right Scrubb that. I was being thick and thinking the construction was done in the constructor. Which I presume it isn't to make it lazier.
Regards,
Rob Philpott.
|
|
|
|
|
The issue is the static member variable. You should probably declare it private readonly and not static. What you are trying to do with the singleton pattern is force one instance in your application. static classes do that, and if your SomeClass has no internal state, then you should move it to a static class instead. If you insist on keeping the member static, then put the initialization into a static constructor and don't check ==null in the Current property.
public class SomeClass {
private readonly SomeClass _current;
public static SomeClass Current {
get {
if(_current == null) { _current = new SomeClass();}
return _current;
}
}
public SomeClass() {
}
}
public class SomeClass {
private static SomeClass _current;
static SomeClass(){
_current = new SomeClass();
}
public static SomeClass Current {
get {
return _current;
}
}
public SomeClass() {
}
}
Hope that helps.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Thank you all for helping cause I was going insane trying to figure out why this was happening. It was so very annoying.
By the way Simon thank for the thread safety tip.
|
|
|
|
|
Hi.. i'm making a program and this is how it goes..
I have actually two buttons and having the same event handler... how will i know which button triggered the event?.. what i was planning to do is just to pass a parameter to the method but it seems like the only parameters that is only allowed is the (object sender, EventArgs e)... am not that really proficient in C# so i guess you could give me solutions and workarounds on how to do this
|
|
|
|
|
The object 'sender' is the button that triggered the event.
just cast it to button type and check if it is the one you require.
e.g.
Button b = sender as Button;
if(b == button1)
.....;
else ...;
or you could assign a value in the "Tag" property of the button and check it in the event handler.
regards
|
|
|
|
|
Thanks a lot.. can i also cast like using the java syntax like
"Button b = (Button) sender" <--- is that possible?
|
|
|
|
|
Silvyster wrote: is that possible?
Did you try it? What happened?
|
|
|
|
|
hy everyone!
i would like to start a project in programming a webservice which does some tasks.
but because i have done none before i do not really know where or what to start with. does anyone of you have links, helpful infos or a "best practice" to help me start with? something like a cookbook, which describes how or where to start, if you want to do it the first time?
because i guess it's better to fetch many useful hints instead of just trying things and to get frustrated because it doesn't work as expected
thanks for any useful hint!
stephan.
|
|
|
|
|
Start with small projects and keep with the basics.
Just because we can; does not mean we should.
|
|
|
|
|
Start here: http://support.microsoft.com/kb/308359[^]
It's pretty simple. Create an .asmx page in a webproject, go to the code behind and add a method. Decorate the method with the attribute [WebMethod] and make sure you're only using simple types as parameters. Set you asmx as the start page of your project and hit F5. You should be able to test your method from Internet Explorer.
Regards,
Rob Philpott.
|
|
|
|
|
Hello,
I currently have a form that reads through an xml file and displays information based on the object selected in the combobox.
I can read the information from the xml just fine, but editing and saving the information is where I'm having trouble at.
xml file:
<Objects>
<Object name="Square">
<Height>15</Height>
<Width>15</Width>
<Color>red</Color>
<Object name="Circle">
<Radius>10</Radius>
<Hollow>True</Hollow>
<HollowRadius>5</HollowRadius>
<Color>blue</Color>
</Objects>
How would I change the "Sqaure" objects height value?
or
How would I change the "Circle" objects hollow value to false?
Thanks
|
|
|
|
|
<br />
using System.Xml;<br />
<br />
public void ChangeObjectHeight(string Name, int Height)<br />
{<br />
XmlDocument Doc = new XmlDocument();<br />
Doc.Load("yourfilenamehere.xml");<br />
<br />
XmlNode Node = Doc.SelectSingleNode("/Objects/Object[@name='" + Name + "']");<br />
XmlNode Node2 = Node.SelectSingleNode("Height");<br />
Node2.InnerText = Height;<br />
<br />
Doc.Save("someplacetosavetheshit.xml");<br />
}<br />
or something to that extent.
|
|
|
|
|
Thanks very much, you rock!
|
|
|
|
|
Really? String concatenation? And multiple queries? That's how it's done?
string xpath = string.Format("/Objects/Object[@name='{0}']/Height", Name);<br />
Doc.SelectSingleNode(xpath).InnerText = Height.ToString();
led mike
|
|
|
|
|
Stupefy wrote: that reads through an xml file
How?
Also your XML is not valid so until you fix that it will be difficult to help you.
led mike
|
|
|
|
|
Hello all.
Now that I have discovered extension methods, I am revisiting my libraries of handy-dandy routines to see where it makes sense to convert them. In doing so I am also revisiting a question about how to handle methods that work on different data types.
I am wondering if anyone out there has any opinions or knows any arguments for/against writing such methods in a particular style. I know there probably isn't any one "right" answer, but I thought it would be informative to hear what other programmers think.
For instance, I have a lot of methods that work on strings. I also need the same operations to work on character arrays. So I might end up with something like
public int foo ( char[] SourceData ) { ... }
public int foo ( string SourceData ) { ... }
I have also handled this using
public int foo ( char[] SourceData ) { return foo(new string(SourceData)); }
public int foo ( string SourceData ) { ... }
and even
public int foo ( object SourceData )
{
string source;
if ( SourceData.GetType() == typeof(string) ) source = SourceData;
else if ( SourceData.GetType() == typeof(char[]) ) source = new string(SourceData);
else error_condition;
...
}
I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.
Opinions? Critisisms? Other ideas? All are welcome.
Clive Pottinger
Victoria, BC
|
|
|
|
|
cpotting wrote: I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.
Why would generics be overkill. Just because you are limiting to only 2 or 3 data types, generics are still very useful. Let's face it, generics (in most cases) are often only used against a couple of types only. Don't limit yourself just because your code doesn't use every possible type.
|
|
|
|