|
An ideal candidate for generics
bool CompareLists<T>(List<T> listA, List<T> listB)
{
if (listA.Count != listB.Count)
return false;
foreach (T item in listA)
if (!listB.Contains(item))
return false;
return true;
}
Edit: I haven't optimised your code at all - just converted it to accept generics. You should probably null check both parameters - if both are null it's up to you how you want it to return. As this is nothing really to do with an actual class it may be useful to make it static and possibly an extension method if using 3.0 or above.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
That works, thank you. I was trying to find a way to pass them as IEquatables, which wasn't working out.
|
|
|
|
|
Timothy CIAN wrote: I was trying to find a way to pass them as IEquatables, which wasn't working out.
Why that?Perhaps using generic type constraints will help.
Life is a stage and we are all actors!
|
|
|
|
|
As a general List comparator, your method is flawed in one or two ways:
1. the order of items is disregarded (you may want to describe what "matching lists" is meant to mean);
2. when List1 holds duplicates, your method may return true even when List2 is not at all equivalent (even holding items that are not present in List1, or different quantities of List1 items).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Good points. In this case, both lists contain distinct values (no duplicates).
The order is insignificant, the point of the method is to determine if all items in List A exist in List B, and no others.
|
|
|
|
|
Does it matter if the contents of those items are also identical?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Revised version...
private static bool matchList<T>(List<T> List1, List<T> List2)
{
if (List1 == null || List2 == null) return false;
if ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()))
return true;
return false;
}
|
|
|
|
|
Revision revised... I realized that I could simply return the comparison, as it is bool.
private static bool matchList<T>(List<T> List1, List<T> List2)
{
if (List1 == null || List2 == null) return false;
return ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()));
}
|
|
|
|
|
An interesting approach. thanks.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
|
Hello,
I am trying to add google maps in my C# application but i can't figure out how to do it because i am new in C#. I've checked all of the online tutorials but nothing comes up. I want to add google maps in the rectangle shown in the picture below. If any one can help me this would be very good for me.
http://yfrog.com/0kgoogleaep[^]
Thanks,
John
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:27am.
|
|
|
|
|
But i want to take information from the map. For example when i click in London i want to store it for later use!
|
|
|
|
|
You will need to register your URL with Google in order to use the maps API. They will provide a script block that must be included in your header.
You will also need a map canvas div, and use JS to create a new GMap2 object (with the div name as a parameter), create the icon objects you need, and use addOverlay methods to place icon instances at specific latitude/longitudes. Personally, I wrap all of these into a function that is called from BODY onLoad.
I would recommend reading the API documentation carefully. They have examples, also.
|
|
|
|
|
can you please link me with an example like this?
thanks for you help mate
|
|
|
|
|
Here is an example. Note that if you want to add markers using C#, you will need to create the entire "initialize" function in your code (that is, create C# code that writes the JavaScript code).
Hope this helps.
<html lang="en-us">
<head>
<title>Map Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://maps.google.com/maps?file=<Google will provide this>" type="text/javascript"></script>
<script type="text/javascript">
function initialize()
{
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(20, 0), 2);
map.enableScrollWheelZoom();
var redIcon = new GIcon(G_DEFAULT_ICON);
redIcon.image = "Images/RedCircle.png";
redIcon.shadow = "Images/transparent.png";
redIcon.iconSize = new GSize(32, 32);
redIcon.shadowSize = redIcon.iconSize;
redIcon.iconAnchor = new GPoint(16, 16);
map.addOverlay(new GMarker(new GLatLng(50, 110), { icon:redIcon } ));
map.addOverlay(new GMarker(new GLatLng(100, 200), { icon:redIcon } ));
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 1600px; height: 1000px"></div>
</body>
</html>
|
|
|
|
|
|
OK, this program is from a class I'm taking, it's an example out of our book. You have four classes, D inherits from C, C from B and B from A. I kind of understand how this works but I'm not sure.
Question. Is this considered a single object? Only one object is created but do the references make the other classes(or the methods within them) part of the same object?
I think I understand the rest.
1.) D is called and outputs 'D' becuase that is the method within D.
2.) Since C is virtual and it's child class D overries it, C also outputs 'D'.
3.) Since C is declared 'new' that hides C from B (I assume it also hides D from B) and therefore B outputs it own method and displays 'B'
4.) A is virtual and since its child class B overrides it, it also outputs 'B'.
Sorry, but I think my brain actually froze for a while when I was trying to figure this out and I'm just trying to see if anyone has any input or knows if I'm right.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
{
class A
{
public virtual void M() { Console.WriteLine("A"); }
}
class B : A
{
public override void M() { Console.WriteLine("B"); }
}
class C : B
{
new public virtual void M() { Console.WriteLine("C"); }
}
class D : C
{
public override void M() { Console.WriteLine("D"); }
}
class Program
{
static void Main(string[] args)
{
D d = new D();
C c = d;
B b = c;
A a = b;
d.M();
c.M();
b.M();
a.M();
}
}
}
|
|
|
|
|
It is indeed one object, but depending on how you type the object, you're getting different methods to execute. The type you reference an object by can definitely impact what functionality is executed.
It's kind of like explicitly implementing interfaces, check this[^] out.
|
|
|
|
|
Thank you, I thought that it was the same object but I wasn't sure.
I think you answered the 'why' question also.
Your saying that that like interfaces (I'm still trying to grasp those also) You can take the same data but manipulate it differently.
With an interface you explicitly cast the data using the interface you want.
With inheratance you use the same object but use a different reference to the object.
|
|
|
|
|
It sounds like your close to the correct understanding. Be aware though, this is NOT a normal situation in coding. The only reason you would ever use new is that the member is NOT virtual and you need to override its functionality. Because the method is virtual, there is really no need to shadow it with new . Typically shadowing can get you into a lot of confusion if you're not careful, hence why when its not necessary it is strictly avoided.
As an example of why its not needed, if you change the definition of the method in C to:
class C : B
{
public override void M() { Console.WriteLine("C"); }
}
It'll print out four "D"s.
As for interfaces, they don't represent a class you can create (eg. new IList() ), but rather just an expected contract of functionality.
I mention explicit implementation of interfaces because they represent something similar to the behavior seen in the code you posted. Basically, how you reference an object determines exactly what functionality is called.
When you down cast class D to C, that is an implicit cast (ie. .NET assumes you mean to do that). It'd be exactly the same as if you had typed:
C c = (C)d;
Ok, he comes some really confusing stuff, hopefully it won't make things worse.
If you look at the reflected IL code of the Main method, you'll see this:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] class Example.D d,
[1] class Example.C c,
[2] class Example.B b,
[3] class Example.A a)
L_0000: nop
L_0001: newobj instance void Example.D::.ctor()
L_0006: stloc.0
L_0007: ldloc.0
L_0008: stloc.1
L_0009: ldloc.1
L_000a: stloc.2
L_000b: ldloc.2
L_000c: stloc.3
L_000d: ldloc.0
L_000e: callvirt instance void Example.C::M()
L_0013: nop
L_0014: ldloc.1
L_0015: callvirt instance void Example.C::M()
L_001a: nop
L_001b: ldloc.2
L_001c: callvirt instance void Example.A::M()
L_0021: nop
L_0022: ldloc.3
L_0023: callvirt instance void Example.A::M()
L_0028: nop
L_0029: ret
}
I've underlined the calls to d.M(), c.M(), etc. You'll notice that its going back to the class that defines the original signature of the method. Because of the 'new' keyword, that inheritance starts over so to speak. So, from a code perspective, it'll burrow its way through the currently defined type you're referencing it as, until it finds a 'new' keyword or the 'virtual' keyword. From there, it'll find the furthest up the inheritance chain it can go before it reaches the real type of the class or another 'new' keyword. By using 'new', you break all inheritance hence forth.
I hope that helps you, rather than confuses you.
|
|
|
|
|
hi,
as i told in recent post, i need to a way to place generated serial number into my application. but i build my application using msbuild, bcoz of dynammic build i cant change the source code to place serial number in it, is there any way to place serial number of software without changing source code manually??? or a way to define serial of software?
note: I need this serial number to be checked in my hardlock validation routine!
|
|
|
|
|
Well, you could write a utility to patch the executable, but what about obfuscation? Are you going to obfuscate your code? If so (and believe me you should), you have to do it AFTER you patch the exe because (decent) obfuscation utilities typically encrypt strings.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
thanx for ur answer! u mean there isn't any way to add serial number to software? could u explain how obfuscate my code?
|
|
|
|
|
I figured you didn't want to spend the money on a commercial solution.
Obfuscation makes it difficult/impossible to reverse engineer your code (which is easy to do on a .net assembly). Google ".Net Obfuscation tools" to find a list of viable candidates.
BTW, Visual Studio comes with an obfuscation tool, but it's so featureless as to be completely worthless. You have to actually spend money on a 3rd party product, and this can cost from $150 to THOUSANDS of dollars. If your app written in WPF or Silverlight, there's only one obfuscator that can obfuscate it, but I don't remember what the name of it is.
As far as a serial number tool, you may be able to find one that can work on obfuscated assemblies, but I dont' know. Again, google is your friend.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|