|
Would PortAudio[^] fit the bill? (Haven't looked at it much yet)
There is a C# Wrapper[^] available. It says alpha, but I haven't found many bugs with my limited testing. There is another wrapper here[^] that may be of use as well.
I can create MSVC 9/10/11/12 builds of PortAudio and compiled versions of the wrappers if you would like me to. (The 9/10 builds might take a while as I am running those versions on a VM)
What do you get when you cross a joke with a rhetorical question?
---
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
|
|
|
|
|
Thanks for the response!
I'll have a look at them.
|
|
|
|
|
I was reading Marc Clifton's "Succinctly" book, "Imperative to Functional Programming" (published by SyncFusion, free download here: [^]), when I came across this interesting C# example on page #14:
static int EvenOrZero(int x)
{
return x % 2 == 0 ? x : 0;
}
int[] numbers = { 1, 2, 3, 4, 5 };
int sumOfEvenNumbers = numbers.Sum((Func<int, int>)EvenOrZero); Of course, Marc's code works. What intrigued me was the cast of the Function to Func<int,int>> about which Marc comments: "In this example, the cast is necessary to resolve the ambiguity between Func<int, int> and Func<int, int?>"
Since the 'Sum method has an overload which use only int, and not int?: Enumerable.Sum<TSource>> Method (IEnumerable<TSource>, Func<TSource, Int32>), I thought I could do something like this to avoid the cast:
private int EvenOrZero(int x)
{
return x % 2 == 0 ? x : 0;
}
List<int> numbers = new List<int>{ 1, 2, 3, 4, 5 };
int sumOfEvenNumbers = numbers.Sum<int>(EvenOrZero); Intellisense flags this with "Ambiguous invocation," and compiling this produces 3 errors:
Error 3 Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)
Error 4 The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal>)' and 'System.Linq.Enumerable.Sum<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal?>)'
Error 5 'int ...EvenOrZero(int)' has the wrong return type Looking up the MS Doc for this overload of 'Sum [^] leads to an example that does not use int, but uses double !
What gives here with the required cast ? I assume that somehow the direct use of a named method in this case does not satisfy the compiler, but I have used named methods as the value of functions before with no problem.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
modified 16-Oct-14 18:09pm.
|
|
|
|
|
int sumOfEvenNumbers = numbers.Sum( x => EvenOrZero( x ) );
|
|
|
|
|
Down vote countered.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Eric Lippert posted an explanation of this on Stack Overflow:
Specifically, the subtle difference is that a method group is considered to be convertible to a delegate type solely on the basis of whether the arguments match, not also on the basis of whether the return type matches.
...
The upshot of it here is that your method group is convertible to all the different delegate types that are the second arguments of every version of Sum that takes a delegate. Since no best conversion can be found, the overload resolution on method group Sum is ambiguous.
He also has a blog post[^] talking about this in C# 3.
.NET 4.0 is a lot better at overload resolution for method groups, but this is one case where it still doesn't work.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a bright student who I assigned a "challenge" of writing a calculator that uses Funcs, or Lambda expressions, and a Dictionary, to provide a central "dispatch" method for various math calculations. Said student came back puzzled by why his Dictionary definition wouldn't compile, and I could not give him a satisfactory answer as to why a cast to nullable int was required.
Perhaps it's in the nature of the compile process for a Func/Lambda inside an auto-initializing Dictionary declaration ?
The compiler has no problem with this:
int? nullInt = null;
private Dictionary<string, Func<int, int, int?>> dctOps = new Dictionary<string, Func<int, int, int?>>
{
{ "+", (i1, i2) => i1 + i2 },
{ "-", (i1, i2) => i1 - i2 },
{ "/", (i1, i2) => ((i2 == 0) ? (int?) null : i1/i2) },
{ "*", (i1, i2) => i1 * i2 }
}; fyi, usage:
private int? CalcTwoInts(string op, int i1, int i2)
{
Func<int, int, int?> func;
if (dctOps.TryGetValue(op, out func)) return func(i1, i2);
throw new ArgumentException(op + " is not a legal key");
}
int? test3 = CalcTwoInts("/", 100, 0);
if(test3 == null)
{
}
else
{
}
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
The specification states:
Quote:
The second and third operands of the ?: operator control the type of the conditional expression.
Let X and Y be the types of the second and third operands. Then,
- If X and Y are the same type, then this is the type of the conditional expression.
- Otherwise, if an implicit conversion exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
- Otherwise, if an implicit conversion exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
- Otherwise, no expression type can be determined, and a compile-time error occurs.
In the case of condition ? null : integer , the types of X and Y are null and Int32 .
- X and Y are not the same type.
- There is no implicit conversion from
null to Int32 ; - There is no implicit conversion from
Int32 to null ; - Therefore, a compile-time error occurs.
When you cast one of the operands to Nullable<Int32> , the result is:
- X and Y are not the same type.
- There is an implicit conversion from
null to Nullable<Int32> ; -or- - There is an implicit conversion from
Int32 to Nullable<Int32> ; - Therefore, the type of the expression is
Nullable<Int32> .
There are several options for the expression which will work:
(i2 == 0) ? (int?) null : i1 / i2 (i2 == 0) ? default(int?) : i1/i2 (i2 == 0) ? null : (int?)i1 / i2 (i2 == 0) ? null : i1 / (int?)i2
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks for the detailed analysis, Richard !
I am surprised that (i2 == 0) ? null : i1 / (int?)i2 works: I infer from that that the compiler interprets the result of the division as a nullable int if only the divisor is a nullable int.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
With a division, the resulting type is determined based on the type of the operands in much the same way as the ternary operator:
- If both operands are the same type, that is the type of the result;
- If there is an implicit conversion from X to Y, but not from Y to X, then Y is the type of the result;
- If there is an implicit conversion from Y to X, but not from X to Y, then X is the type of the result;
- Otherwise, no result type can be determined, and a compile-time error occurs.
In this case, there is an implicit conversion from Int32 to Nullable<Int32> , but not from Nullable<Int32> to Int32 . Therefore, the type of the result is Nullable<Int32> .
You'll see similar type inference at work with the other binary operators - * , + , - , etc.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The issue isn't the one you think it is. Basically, the problem is the part that says i1/i2. What you have here is a condition that the compiler thinks could return as an int because of the implicit type of this operation. Now, you basically need to tell the compiler that you're returning an int? out of this so that it knows what to put it as. Here's a little trick to demonstrate this:
{ "/", (i1, i2) => ((i2 == 0) ? null : ((int?)i1)/i2) },
|
|
|
|
|
Thanks, Pete !
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
Hi guys,
What I'm trying to do is to programmatically open a Excel Worksheet and then within a given range read and analyse all of the tables.
I already know how to open the file and find used range per sheet. But how do I get all the tables from it?
Someone an idea how make this possible? I would be really greatfull.
|
|
|
|
|
Are you using ExcelDataReader[^] ?
If you are, I suggest you study: [^].
What is the problem you are having now with reading/analyzing: more detail please.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
No I'm not. I'm using Open XML to enter the file and to analyse the values of the cells. But I also want to be able to look at the data structure, so if there are tables in the worksheets how do I recognise them.
|
|
|
|
|
Are you opening the XLSX using zip/XML? If yes, then I'd recommend using a library that does that part.
It's quite easy to end up with an incorrect format if one of the references is broken, and there are a lot of dependencies in there
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I'm implementing some additional features from an already working API for a SignalHound USB Spectrum Analyzer (SA124B).
My code is C#, and i have the SH_API.dll (c or c++).
SHAPI_GetSerNum() & SHAPI_Initialize() are both working.
int initResult = Dll.SHAPI_Initialize();
uint ser = Dll.SHAPI_GetSerNum(0);
However SHAPI_GetSA123CalData is not !
string fileName = "";
int CalDataResult = -1;
CalDataResult = Dll.SHAPI_GetSA124CalData(fileName, 0);
No matter what i set "FileName" to i get the response "0" to CalDataResult. It seems i have some sort of connection to the method in the dll, but there is something going on with this "LPCSTR" filename parameter, that i don't understand !
Can anyone help - how do i sent a LCPSTR from C# to C/C++
-------------------------
The documentation says:
extern "C" __declspec( dllimport ) int SHAPI_GetSA124CalData(LPCSTR filename,int deviceNum=0); //Returns -1 on error
My Wrapper code -----------------------------------
[DllImport("SH_API.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern Int SHAPI_GetSA124CalData(string FileName, int deviceNum=0);
[DllImport("SH_API.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint SHAPI_GetSerNum(int deviceNum=0);
[DllImport("SH_API.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int SHAPI_Initialize();
----------------------------------------------------------
|
|
|
|
|
|
Thanks for your support. The result is unfortunately the same (response is always "0"). It doesn't care what I set the filename to or if the file it present. Maybe i'm overlooking something, or that method doesn't really work as the document says.
[DllImport("SH_API.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int SHAPI_GetSA124CalData([MarshalAs(UnmanagedType.LPStr)] string FileName, int deviceNum = 0);
string fileName = "D30510098.bin";
Int32 CalDataResult = -1;
CalDataResult = Dll.SHAPI_GetSA124CalData(fileName, 0);
|
|
|
|
|
I guess you need to go back to the provider of the library and ask them.
|
|
|
|
|
and so i did... and they just confirmed that the API is faulty in that sense, and always reports "0". They will make a new version.
|
|
|
|
|
Hi,
I want to generate an .rtf file in which the data will be filled from the database.
I am short of time to develop my application and dont have time to write my own library for rtf generation.
Any free easy to use library or dll will be helpful.
Please suggest.
Thanks,
Mukul Sharma
|
|
|
|
|
|
mukulsharma1146 wrote: Any free easy to use library or dll will be helpful. ..there's a RichTextBox control in the framework. It is well documented and does exactly what you need.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
Thanks for the reply.
Actually I do not have a rich text control in the html. I have form data in the database from which I need to create an rtf report.
As suggested by some other blogs, I am supposed to read the data from the database (SQL Server) and get it inside an HTML form and finally generate the rtf file from the html form (I don't need to render the html form).
Kindly let me know if I am following the right approach.
Also if there are any other ways of achieving this.
Thanks,
Mukul
|
|
|
|
|