Solved

Help Me

Posted on 2016-11-08
6
63 Views
Last Modified: 2016-11-27
object AppWorkspace = null;
            MainForm.DefaultBackColor.Equals(AppWorkspace);
MainForm mf = new MainForm();
            mf.Show();
this is supposed to change the BackColor in MainForm

Getting ready for sulotions
0
Comment
Question by:Brian Smith
6 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points (awarded by participants)
ID: 41879551
you aren't setting the background color in this. equals is a boolean check
1
 
LVL 22

Accepted Solution

by:
p_davis earned 250 total points (awarded by participants)
ID: 41879554
if this is winforms use the properties of the form to change the color
0
 
LVL 2

Assisted Solution

by:weifai
weifai earned 125 total points (awarded by participants)
ID: 41879562
The Equals() methods are usually not for setting the property, but to compare it to the parameter and return a boolean value.

Also, you should involve your form, not the form's class.

It should be something like
mf.BackColor = new Color(...);

Open in new window

right after creating mf and before showing it.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 125 total points (awarded by participants)
ID: 41879595
What p_davis is saying is that .Equals() is a comparison method.  It checks to see if two objects are equal to each other and returns a boolean value that indicates if they are Equal to each other.  Think of .Equals() as being directly related to '==' but with subtle differences:
The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour.

For reference types where == has not been overloaded, it compares whether two references refer to the same object - which is exactly what the implementation of Equals does in System.Object.

Value types do not provide an overload for == by default. However, most of the value types provided by the framework provide their own overload. The default implementation of Equals for a value type is provided by ValueType, and uses reflection to make the comparison, which makes it significantly slower than a type-specific implementation normally would be. This implementation also calls Equals on pairs of references within the two values being compared.

- Source

Regardless, in C#, Equals and '==' are not the same as '=' which is the assignment operator.

Consider the following:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace EE_Q28981881
{
	class Program
	{
		static void Main(string[] args)
		{
			Person bob = new Person() { ID = 1, FirstName = "Bob", LastName = "Smith" };
			Console.WriteLine("The original bob");
			Console.WriteLine(bob);
			Person bob2 = bob.Clone();
			Console.WriteLine("The cloned bob");
			Console.WriteLine(bob2);
			Console.WriteLine();
			Console.WriteLine("Are the orginal bob and the cloned bob equal? {0}", bob == bob2);
			Console.WriteLine();
			Console.WriteLine("Let's change the cloned bob's last name.");
			bob2.LastName = "Johnson";
			Console.WriteLine("The cloned bob");
			Console.WriteLine(bob2);
			Console.WriteLine("Are the orginal bob and the cloned bob equal? {0}", bob == bob2);
			Console.ReadLine();
		}
	}

	[Serializable]
	class Person
	{
		public int ID { get; set; }
		public string FirstName { get; set; }
		public string LastName { get; set; }

		public override string ToString()
		{
			return string.Format("{0} {1} [{2}]", FirstName, LastName, ID);
		}

		public override bool Equals(object obj)
		{
			if (obj == null || (obj.GetType() != GetType()))
				return false;

			return this == (obj as Person);
		}

		public bool Equals(Person person)
		{
			if (object.ReferenceEquals(person, null))
				return false;

			return this == person;
		}

		public override int GetHashCode()
		{
			return (ID != null ? ID.GetHashCode() : 0) ^ (FirstName != null ? FirstName.GetHashCode() : 0) ^ (LastName != null ? LastName.GetHashCode() : 0);
		}

		public static bool operator ==(Person lhs, Person rhs)
		{
			if (object.ReferenceEquals(lhs, rhs))
				return true;

			if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null))
				return false;

			return (lhs.ID != null && rhs.ID != null ? lhs.ID.Equals(rhs.ID) : false) &&
				(lhs.FirstName != null && rhs.FirstName != null ? lhs.FirstName.Equals(rhs.FirstName) : false) &&
				(lhs.LastName != null && rhs.LastName != null ? lhs.LastName.Equals(rhs.LastName) : false);
		}

		public static bool operator !=(Person lhs, Person rhs)
		{
			return !(lhs == rhs);
		}
	}

	static class Extensions
	{
		public static T Clone<T>(this T source)
		{
			if (!typeof(T).IsSerializable)
				throw new ArgumentException("The type to be cloned must be serializable.", "source");

			if (object.ReferenceEquals(source, null))
				return default(T);

			IFormatter formatter = new BinaryFormatter();
			using (Stream stream = new MemoryStream())
			{
				formatter.Serialize(stream, source);
				stream.Seek(0, SeekOrigin.Begin);
				return (T)formatter.Deserialize(stream);
			}
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG
-saige-
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41879755
Could you please explain clearly what you want to achieve?

1. Guys above explained already that ".Equals" is just a check if something equals to something, therefore it won't change the color.

2. Setting color 2 null won't probably give any effect. How you imagine "null color"?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41902972
Solution is provided
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question