|
I do not see what the problem with this...
value => Method1(value, someString, someDouble)
This line does not say that the function you run inside the delegate can not get any value you want (number and type), it talking about the delegate itself, so you can not write this:
(val1, val2) => Method(val1, val2)
You may see the compiled code to understand more...
private void TestCodeInDictinary()
{
this.IntKeyValueCode.Add(1, delegate(int value)
{
this.Method1(value, this.someString, this.someDouble);
});
}
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
I believe it is the Action itself that is getting the parameter passed to it, 'value => ', not the method that you're calling inside the Action. Hell, you don't even need to use the parameters passed in
To illustrate:
private Dictionary<int, Action<int, int>> IntKeyValueCode = new Dictionary<int, Action<int, int>>();
private void TestCodeInDictinary()
{
IntKeyValueCode.Add(1, (value1, value2) => Method1(thisCodeDoesnt, giveAShit, whatsPassedIn));
IntKeyValueCode[1](100, 200);
}
|
|
|
|
|
Exactly!
Action<t> declares a delegate, where the left side of the lambda (=>) must fit the parameter list of that delegate (the <T> part) and the right side is what executed when the delegate called...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
modified 31-Dec-14 12:31pm.
|
|
|
|
|
Hi,
any sample code i can fulfill the following?
creating internet explorer toolbar that extract all the elements from the active tab
So far, i can only find toolbar that just redirect / navigate to another page.
Please help.
|
|
|
|
|
You might want to define precisely what you mean by "extract all the elements".
|
|
|
|
|
Did you look at in system.net?
Following code extracts favicon from websites.
string u="web site address";
Uri url = new Uri(u);
String iconurl = "http://" + url.Host + "/favicon.ico";
WebRequest request = WebRequest.Create(iconurl);
try
{
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
return Image.FromStream(s);
}
...
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
I have the Private key (from 'pem' file) as string.
But the method which im using accepts the parameter as 'Private key'
getSignaute(java.security.PrivateKey pk, string inputxml)
how to convert string to private key (in C#.net)
|
|
|
|
|
|
Hello,
i am getting stuck to apply a feature in my project with Drag&Drop developing in WPF/C#, i can post you an example of code of what i want to do as follow :
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="box1" Grid.Row="0" >
<Grid Name="grid1" Background="Aqua" Margin="15"></Grid>
</Grid>
<Grid x:Name="box2" Grid.Row="1" >
<Grid Name="grid2" Background="blue" Margin="15"></Grid>
</Grid>
<Grid x:Name="box3" Grid.Row="2" >
<Grid Name="grid3" Background="green" Margin="15"></Grid>
</Grid>
</Grid>
each Grid(box) contains another Grid my simple purpose is to drag the "grid1" to the "box2" then automatically the "grid2" go to the "box1" and this rule is applied every time i want drag&drop ; the Grid "box" is always not movable .
From many days i check out many project and library online as GongSolutions.Wpf.DragDrop,Blacklight.ShowCase.WPF and else but don't cover my request because in each grid there are many controls binding data(in the real project should be 30 grid more all controls ) then an ItemControl don't cover my purpose.
I am easy to do a simple Drag&Drop but in this case is more complicated for me then i ask you kindly if you have any suggestion or idea to work out this case.
I give in anticipate my apologies if my question result weird as i have not much experience in WPF.
Thank you in anticipate
|
|
|
|
|
That looks more like XAML (WPF) than C#; please use the correct forum.
|
|
|
|
|
Dear Richard,
thank you so much i will move this post to the correct forum,i give you my apologies for this mistake.
|
|
|
|
|
Hi,
I've encountered a few scenarios similar to the one I'm posting about today and I'd like to know a good way to write code for this scenario rather than the hokey work arounds I've been doing.
The scenario is that I have x and y coordinates and a value.
I want to sort by the x coordinate, then the y coordinate and then plot the value. In powershell I could create an object and then its properties, can I do that easily in c#? Or is there a better way to handle that? The sorting I'm not sure how to do either? I also am receiving them out of order.
Thanks for reading.
|
|
|
|
|
Let's assume that you are storing the data in a class that stores the properties as X and Y values. It's easy enough to sort the data using LINQ, first by ORDERBY on the X and then the Y. Do this in a single query.
|
|
|
|
|
Why sort it at all? You can plot unsorted values. :shrug:
On the other hand, part of what I was playing with over the last few days involved X,Y,value objects and I keep them in a Dictionary<int,object> (like a sparse* matrix, and the int Key is essentially a hash of the X and Y -- (X<<16)|Y ). In my case, I then use nested for loops to access the content of the Dictionary in the order I want them.
for ( y = miny ; y <= maxy ; y++ )
for ( x = minx ; x <= maxx ; x++ )
if ( data.ContainsKey ( hash ( x , y ) ) ...
This requires that I update the min and max x and y as I add items to the dictionary, but that's no big deal.
* There may be only 10% or fewer active items over the two-dimensional address space.
modified 29-Dec-14 17:03pm.
|
|
|
|
|
I've written this type of Class so many times I think I could write it half-asleep
using System.Collections.Generic;
using System.Linq;
namespace YetAnother
{
public class PointyPoint
{
public int X { set; get; }
public int Y { set; get; }
public double Value { set; get; }
public PointyPoint() {}
public PointyPoint(int x, int y, double value)
{
X = x;
Y = y;
Value = value;
}
}
public enum PointySortType
{
PSortAscendingX,
PSortAscendingY,
PSortDescendingX,
PSortDescendingY,
PSortAscendingValue,
PSortDescendingValue
}
public class PointyListClass : List<PointyPoint>
{
public PointyListClass() {}
public List<PointyPoint> PointySortedBy(PointySortType pSType, PointyListClass pointyList)
{
if (pointyList == null || pointyList.Count == 1) return null;
switch (pSType)
{
case PointySortType.PSortAscendingX:
{
return pointyList.OrderBy(pointy => pointy.X).ToList();
}
case PointySortType.PSortAscendingY:
{
return pointyList.OrderBy(pointy => pointy.Y).ToList();
}
case PointySortType.PSortDescendingX:
{
return pointyList.OrderByDescending(pointy => pointy.X).ToList();
}
case PointySortType.PSortDescendingY:
{
return pointyList.OrderByDescending(pointy => pointy.Y).ToList();
}
case PointySortType.PSortAscendingValue:
{
return pointyList.OrderBy(pointy => pointy.Value).ToList();
}
case PointySortType.PSortDescendingValue:
{
return pointyList.OrderByDescending(pointy => pointy.Value).ToList();
}
}
return null;
}
}
}
private void SomeButton_Click(object sender, EventArgs e)
{
PointyListClass pointyList = new PointyListClass
{
{new PointyPoint(100,400,199.99)},
{new PointyPoint(200,300,12.43)},
{new PointyPoint(300,200,-12333)},
{new PointyPoint(400,100,345.93)},
};
var pointyXAscending = pointyList.PointySortedBy(PointySortType.PSortAscendingX, pointyList).ToList();
var pointyYAscending = pointyList.PointySortedBy(PointySortType.PSortAscendingY, pointyList).ToList();
var pointyXDescending = pointyList.PointySortedBy(PointySortType.PSortDescendingX, pointyList).ToList();
var pointyDescending = pointyList.PointySortedBy(PointySortType.PSortDescendingY, pointyList).ToList();
var pointyValueAscending = pointyList.PointySortedBy(PointySortType.PSortAscendingValue, pointyList).ToList();
var pointyValueDescending = pointyList.PointySortedBy(PointySortType.PSortDescendingValue, pointyList).ToList();
}
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
Every time you use ToList or ToArray a puppy is baked into a pie.
|
|
|
|
|
Well, at least that Pie has fur on it, rather than being bald
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
Eh... like the Pie guy said, too many ToList()'s . That makes a copy of the list and enumerates the list and makes copies, etc.
But also, this is kinda overkill since the linq syntax is well known and cleaner...
OrderBy((x) => x.X)
OrderBy((x) => x.Y)
Syntactic sugar shouldn't have less sugar then the syntactic thing its attempting to sugar.
|
|
|
|
|
"That makes a copy of the list and enumerates the list and makes copies, etc."
The result of a Linq statement is not a "concrete instance" of a generic List, rather it is an internal form of what you might call the "makings of a generic List:" only when you access the Linq result by, for example, using 'foreach on its contents, or using .ToList, etc., is an actual final form "rendered."
"Syntactic sugar shouldn't have less sugar then the syntactic thing its attempting to sugar." So, let me get this straight: you object to having a method in a Class that returns the result of one of several possible sort-orders depending on the value of an Enum ?
It's a good thing I'm not your manager, because you are the kind of sledge hammer I would set to work breaking concrete, rather than writing code
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
BillWoodruff wrote: The result of a Linq statement is not a "concrete instance" of a generic List,
rather it is an internal form of what you might call the "makings of a generic
List:" only when you access the Linq result by, for example, using 'foreach on
its contents, or using .ToList, etc., is an actual final form "rendered."
Yeah, thats why we were saying you shouldn't use .ToList(). You can return an ordered enumeration of the list without "rendering" it.
BillWoodruff wrote: So, let me get this straight: you object to having a method in a Class that
returns the result of one of several possible sort-orders depending on the value
of an Enum ?
Didn't you do that for syntactic sugar purposes? To make it easier for the caller to return the list? I don't object to encapsulating functionality, I object to doing it in an unknown / custom way when there is a well known, standard way to do it . Just my opinion though. YMMV. As I mentioned, OrderBy(x => x.Y) to me seems easier / shorter then your enum method.
Also, I (perhaps mistakenly?) assumed you implemented that sort functionality in the class itself for use in a UI? Or is it for internal application use? I haven't run across any project yet where the sort order is not either hard-coded in a specific order for internal application use or the sort order is influenced by the user through the UI. Again, just my personal experience. If you have an internal need to sort data differently in different situations, I'd love to hear the use case out of curiousity.
If you did do it for use in a UI, Microsoft already has an established pattern in .Net for filtering and/or sort collections and that ain't it . The pattern doesn't specifically have to be used for UI work, but as I said, its extremely rare to have to sort in different ways unless the user requests it. See ICollectionView.
BillWoodruff wrote: It's a good thing I'm not your manager, because you are the kind of sledge
hammer I would set to work breaking concrete, rather than writing code
All my code tends to follow well known, established and accepted patterns . I'm kind of a stickler for that.
|
|
|
|
|
Here's a challenge for you, Mighty Hammer: write a response to the OP here that demonstrates what you consider "best practices"
It's rather clear to me reading your responses that you do not understand Linq fully, and grasp that no "copy" is made of the whatever structure that is processed by a Linq query.
I think you also miss the Zen humor in Piebald's comment: to bake a puppy into a pie is, indeed, to transform it, and a Linq query result is a transformed, often filtered so one has a pointer to a subset of all possible objects; it's kind of a "map" to what will be finally rendered as a result of the transformation.
What I am surprised that neither you, nor Piebald, pointed out is that all of the Linq queries shown in this example act on all of the objects in the set: no filtering here; and it is correct that Linq queries that sort, and use of operators like Cast, will always have more potential overhead than Linq queries that use Linq operators, for example 'Where, that do filter.
I don't find your comments about "standard techniques" credible, which doesn't mean I don't believe that you have evolved a set of practices that are quite useful in the kind of code you write in the context of your work, and which you now believe are "standard." We all generalize from our experience in ways that both enable, and limit, us.
Code I write in response to questions on CP is written with the goal of educating the OP, and I try to make it as "general" as possible, to communicate its intent as clearly as possible. In writing this code I felt the example just might be more useful, in the long run, on CP, if I demonstrated using an Enum, using a Switch/Case, providing an example of a utility class with a gamut of useful options (the various sorts).
Some wise words by Jon Skeet concerning the "overhead" of Linq queries: [^].
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
Sadly, it is you who does not understand LINQ fully. Allow me to impart some wisdom on you with my mighty hammer .
List<int> lst = new List<int>();
lst.Add(1);
lst.Add(2);
lst.Add(3);
System.Diagnostics.Debug.WriteLine(lst[1]);
var v1 = lst.OrderBy(x => x);
var v2 = lst.ToList();
lst[1] = 7;
foreach (int i in lst)
System.Diagnostics.Debug.WriteLine("L: " + i);
foreach (int i in v1)
System.Diagnostics.Debug.WriteLine("V1: " + i);
foreach (int i in v2)
System.Diagnostics.Debug.WriteLine("V2: " + i);
Output:
2
L: 1
L: 7
L: 3
V1: 1
V1: 3
V1: 7
V2: 1
V2: 2
V2: 3
You'll notice that the original list has become 1,7,3 (the L). V1 has become 1,3,7 because its the same list (and sorting is lazy). Now notice V2... OMG... its still 1,2,3!!!
Crazy, huh?
ToList() and ToArray() are special in LINQ. They make a COPY of the list. However, it is a SHALLOW copy. Since ints, floats, doubles, etc. are VALUE types, you get a brand new copy of them.
With REFERENCE types, you'll still get a BRAND NEW list with references to the original objects. HOWEVER, if you add / remove from the original list, the add / delete will not be propagated because its a different list.
If you didn't do the ToList(), it would work the way you expect with reference types too.
With that being said, most other LINQ operations are lazy. However, ToList() and ToArray() are NOT.
Thank you. Please play again .
|
|
|
|
|
Gosh, of course Value types are copied: that's the definition of what a Value type is.
Please see my comments to Piebald here: [^].
In your example: when you call 'ToList on 'v2 which is not a Linq intermediary structure, but a "real live" instance of a List, you are not performing an act which is any way equivalent to what Linq does. I note that I would never, in "real world" code, expect to see a call to 'ToList on something that is already in "final form."
Yet, while you are performing reductio ad absurdum in this swing of the mighty mallet, and miss the target by a country mile, I still so cherish the way you swing that hammer
Happy New Year
cheers, Bil
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
Sorry Bill, but it seems you are indulging in the festivities of the new year a tad early.
*You* claimed ToList() does not create a new list when it in fact does. Value types will of course be copied and reference types will of course be referenced. However, it is not the instance of the list that you think it is. Thus you will run into situations like I have shown you as well as others.
While my example trivially used lst.ToList() for simplicity, if you got off your condescending high horse for a second, you'd realize that LINQ operations operate on IEnumerable sequences. Of which ListT is one. However, IEnumerableT is not a ListT.
Since I'm sure you'll retort with one of your childish condescending replys, I'll leave you with this C&P from MSDN:
The ToList<tsource>(IEnumerable<tsource>) method forces immediate query evaluation and returns a List<t> that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.
ToArray<tsource> has similar behavior but returns an array instead of a List<t>.
Drop the cocky attitude kid, you might learn something.
Bye .
|
|
|
|
|
BillWoodruff wrote: The result of a Linq statement is not a "concrete instance" of a generic List
Correct; it's an IEnumerable.
BillWoodruff wrote: by, for example, using 'foreach
No; that's not a problem.
BillWoodruff wrote: or using .ToList
Exactly. That's what you're doing. ToList is making a concrete copy -- your library code should not being using ToList; it should just be returning the IEnumerable, the same as the Linq methods do. If the final output should be a List or Array or whatever, that should be done as late as possible, by the application, not by library routines that don't know how the output will be used.
|
|
|
|
|