|
I'm trying to wrap my head around using LINQ and lambda expressions in c# 3.0, and as such I threw together a short (presumably) simple exercise- A general function that will take an IEnumerable and return a concatonation of the string representations of each of the elements contained therein.
public String concat(ICollection values )
{
return values.Cast<object>().Aggregate((Object x, Object y) => x.ToString() + y.ToString());
}
However, when I try to run this little gem, I get the following:
"The type arguments for method 'System.Query.Sequence.Aggregate<t>, System.Query.Func<t,t,t>)' cannot be inferred from the usage. Try specifying the type arguments explicitly"
Googling revealed that this error is usually the result of saying (x,y) => ... instead of explicitly typing X and Y the way you would when defining the parameters in a method. Makes sense, except that I'm explicitly saying "(Object x, Object y)". Further, when I change all instances of "Object" to "String" (which I don't want to do for the final version, as I want this to work for any type of object), it works without a hitch. I don't understand why that makes a difference, since I'm calling ToString() on the object to get the string out, and "ToString()" works for any object. Is this a failing of the type inference system? Or am I just completely glossing over something?
Much appreciated.
|
|
|
|
|
public string concat(ICollection values)
{
return (string)values.Cast<object>().Aggregate((x, y) => x.ToString() + y.ToString());
}</object>
|
|
|
|
|
I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y).
Thanks for the attempt, though.
|
|
|
|
|
Member 2148527 wrote: I'm still getting the "type arguments for method ... cannot be inferred from the usage" error. Even tried throwing gratuitious use of "Object" back in (Object x, Object y).
Thanks for the attempt, though.
Erm, the code as I pasted I compiled, so either you are missing something, or you are compiling in VS2005 maybe (or using the C# 2.0 compiler?)!
|
|
|
|
|
My mistake- I was using a "linq playground" project I'd initially created in VS2005. Moving to 2008 apparently didn't update all the references, so it was still trying to compile down to C# 2.0. Changed the LINQ references to point to 3.5 (instead of the preview release that you could download for 2.0), and changed the "target" to .NET framework 3.5, and it works perfectly now.
Sorry for the confusion, and thanks for the help!
|
|
|
|
|
Hello All,
I'm having real memory issues trying to marshal and unpack an unmanaged structure in C#. Any help would be appreciated.
The structure is defined as:
typedef struct
{
WORD BaudGen;
WORD Timer0Reload;
BYTE Prescaler;
DWORD BaudRate;
} BAUD_CONFIG;
I tried a few ways to marshal and had the most success with:
[StructLayout(LayoutKind.Sequential)]
public struct BAUD_CONFIG
{
public System.UInt16 BaudGen;
public System.UInt16 Timer0Reload;
public Byte Prescaler;
public System.UInt32 BaudRate;
}
[DllImport("cp210xmanufacturing.dll")]
public static extern int CP210x_GetBaudRateConfig(
IntPtr cyHandle,
IntPtr baudConfigData);
I tried to access it with the following C# code:
int myStatus = 0;
UsbToCom.BAUD_CONFIG myBaudConfig = new UsbToCom.BAUD_CONFIG();
IntPtr SlHandle = new IntPtr();
IntPtr myBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(myBaudConfig));
Marshal.StructureToPtr(myBaudConfig, myBuffer, true);
myStatus = UsbToCom.CP210x_Open(0, ref SlHandle);
myStatus = UsbToCom.CP210x_GetBaudRateConfig(SlHandle, myBuffer);
myBaudConfig = (UsbToCom.BAUD_CONFIG)Marshal.PtrToStructure(myBuffer,
typeof(UsbToCom.BAUD_CONFIG));
Marshal.FreeHGlobal(myBuffer);
UsbToCom.CP210x_Close(SlHandle);
The problem is that the results are good but NUnit reports an MDA memory problem probably due to the way I'm marshalling.
Thanks in advance for any help!
|
|
|
|
|
ahfong wrote: The problem is that the results are good but NUnit reports an MDA memory problem probably due to the way I'm marshalling.
Never heard of that!
|
|
|
|
|
I have a Generic Queue<privateType> object, myGenericQueue.
My application receives events from another application. In my event handler, I asynchronously (and therefore on a different thread) enqueue data to myGenericQueue, using a delegate (myDelegate.beginInvoke).
I use a callback method to determine if the Enqueue operation succeeded. Both the asynch method and the callback produce debug output.
In my app's main(), a Form Timer periodically pings, at which time I LOCK myGenericQueue, enumerate and dequeue it. Again, more debug output.
To test for synchronization problems, I put a long sleep in my dequeue loop, so that myGenericQueue will be locked when my event handlers try to Enqueue data.
Here's what I'm seeing, that I don't understand: while myGenericQueue is theoretically LOCKed, my event handlers are still having no problems Enqueuing data. The callbacks fire right away, suggesting no delay, and all of the debug output matches order/timing expectations. All data is making it to myGenericQueue.
Is this expected? Why is there no collision with the lock?
The documentation for the Generic Queue object says:
A Queue<T> can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Doesn't this suggest I should be seeing collisions?
Here's my dequeuing code:
lock (myGenericQueue)
{
while (myGenericQueue.Count > 0)
{
myList.Add((privateType)myGenericQueue.Dequeue());
System.Diagnostics.Debugger.Log(0, "", "Starting 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
System.Threading.Thread.Sleep(5000);
System.Diagnostics.Debugger.Log(0, "", "Coming out of 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
}
}
|
|
|
|
|
You only mention locking in one place. You have to lock it in both places. Basically, the lock is asking "can I aquire a lock?" and if it can it gets one. If it can't it blocks until it can get one.
|
|
|
|
|
How... odd. The documentation of "lock" at http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^] makes no mention at all about having to use lock on both sides. In fact, the example quite clearly shows the lock being applied in only one place, and the lock still preventing another thread from accessing that code...
|
|
|
|
|
JoeRip wrote: In fact, the example quite clearly shows the lock being applied in only one place,
That is because the one place is being used by multiple threads. It is being used on ALL (10) sides, in that example.
|
|
|
|
|
Yeah, I figured out my error in thinking. I was so used to using lock (myQueue.SyncRoot) that I had come to think of it as locking the Queue itself, not setting a lock flag on one of the queue's members.
Turns out all I needed to do for my Generic Queues - which I thought didn't have a SyncRoot member, since it didn't show up in VS 2008's Intellisense , was to use
lock (((ICollection)myGenericQueue).SyncRoot)
{
}
It was good to be reminded of what the lock statement was actually doing, though. Thanks!
|
|
|
|
|
No, that doesn't protect the queue. You still have to use a lock around every piece of code that uses the queue.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Agreed, I should have made that clear. I was just explaining why I thought the lock (object) call was actually protecting the object.
And, I was just noting that the lock (((ICollection)myGenericQueue).SyncRoot) statement gives me a nice shared object to lock that can be used by my library code and its callers. I was happy to find I could still do this with Generic Queues, the way I had with... uh, non-generic queues. Specific queues?
|
|
|
|
|
JoeRip wrote: Is this expected?
Yes.
JoeRip wrote: Why is there no collision with the lock?
Because you are not using the lock correctly. The lock doesn't protect the object that you are referring to in any way, the object is only used as an identifier. What the lock does is to prevent another code block locking with the same identifier to be entered (or the same code block to be reentered by another thread).
So, to protect your queue, you need a lock around every piece of code that uses the queue.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
JoeRip wrote: (I can't figure out how to show the angle braces).
Try reading the page you use to post the code into. There is a check box that says something about ignoring HTML or something.
led mike
|
|
|
|
|
I'm following the code here http://www.developerfusion.co.uk/show/4071/1/ to check Verizon email.
On this page the code is started http://www.developerfusion.co.uk/show/4071/7/. I set the server to 'incoming.verizon.net', and my username and password.
When I run it, I get the error "An existing connection was forcibly closed by the remote host".
I'm not sure what this means, or what I'm doing wrong. I checked the code, and it's all entered as exactly as in the article.
Anyone know what this message means?
Everything Makes Sense In Someones Mind
|
|
|
|
|
Just a tip...if you get an error, copy the error message and Google it. There are many chances that you will find the answer there itself without waiting for someone's comment here.
Like this[^]
KMAROIS wrote: I'm not sure what this means, or what I'm doing wrong. I checked the code, and it's all entered as exactly as in the article.
Well, in your case I believe either your username or password is incorrect.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
I am not sure what I am missing, pls point me out
public class X{
private MyPage mypage;
public MyPage MainPage { get { return mypage;} set { mypage;= value; } }
ArrayList listedApplication = new ArrayList();
listedApplication.Add("A");
listedApplication.Add("B");
listedApplication.Add("C");
}
I need to assign this Array list to a property
class MyPage
{
private ArrayList applicationList = new ArrayList();
public ArrayList ApplicationList { get { return applicationList; } set { applicationList = value; } }
}
I tried assigning using
MyPage.ApplicationList = listedApplication ;
I get a NullReferenceException, asking me to use new. i tried using
public class X{
private MyPage mypage;
public MyPage MainPage { get { return mypage;} set { mypage;= value; } }
ArrayList listedApplication = new ArrayList();
listedApplication.Add("A");
listedApplication.Add("B");
listedApplication.Add("C");
MyPage.ApplicationList.Add(new ArrayList(listedApplication)) ;
}
but still get the same error, pls help
|
|
|
|
|
There is an incorrect semicolon in your setters. I do not know if that is your problem however.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
maybe you need
public class X{
private MyPage mypage = new MyPage();
... ?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
AnilUnni wrote: I tried assigning using
MyPage.ApplicationList = listedApplication ;
I get a NullReferenceException, asking me to use new.
You can't possibly have gotten a NullReferenceException with that code. It's a runtime error, and you can't run the code as it doesn't even compile.
MyPage is a class, and ApplicationList is not a static member of the class, so you can't access it that way.
AnilUnni wrote: MyPage.ApplicationList.Add(new ArrayList(listedApplication)) ;
Using "new ArrayList(listedApplication)" would only create a new ArrayList that is a copy of the first ArrayList, so that doesn't serve any purpose in your case. Adding the ArrayList to the ApplicationList property would create an ArrayList containing an ArrayList, which would work so far, but is surely not what you intended.
The problem is still that you are accessing the ApplicationList property as if it was a static member of the MyPage class.
Unless you are stuck with framework 1.x you should not use the ArrayList class at all. Use a type safe generic list instead, like List<string>.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
hi
i am working on a project that neede to use multi threading
in a method i use Monitor.Enter(Object) for define a critical area.
now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state.
now my problem is may be user wants to cancell the execution of a thread that is on wait state
how can i kill the thread.???
if i call Thread.CurrentThread..Interrupt() below error appears:
"cannot kill a thread that was in wait state" this is not complete error, this is just shortest of error.
thank you
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|
|
The thread that currently owns the lock should use some mechanism
to indicate a "cancelled" state and then release the lock.
After acquiring a lock, waiting threads should first look at the
cancelled state before proceeding.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|