|
The two loops set off my code style alarms. But having reviewed it, and worked out the domain here, it really is two separate operations, and there's not much there that is common enough to pull out. When it comes down to it you're duplicating four lines:
for(int i = 0; i < symbolCount; i++) {
staff.AddVisual(something);
staff.AddVisual(PlaceHolderVisual.ThinPlaceholder);
}
So I think I would leave it, but with a note (mental or explicit) to watch this space because if you add more key signature related stuff it could get messy and need refactoring.
Now you could take the duplication out like this:
void AddSharpsAndFlats(StaffVisual staff, Clef clef)
{
int symbolCount = _context.GetSharpCount();
if(symbolCount != 0){
var Positions = symbolCount > 0 ? clef.SharpPositions : clef.FlatPositions;
var Order = symbol.Count > 0 ? MusicContext.SharpOrder : MusicContext.FlatOrder;
VisualCreator creator = symbolCount > 0 ? (p => new SharpVisual(p)) : (p => new FlatVisual(p));
symbolCount = symbolCount > 0 ? symbolCount : -symbolCount;
for (int i = 0; i < symbolCount; i++)
{
staff.AddVisual(creator( {
Position = Positions[(int)Order[i]]
});
staff.AddVisual(PlaceHolderVisual.ThinPlaceholder);
}
}
}
... where
delegate VisualCreator(Position position);
(You could also have a VisualFactory that hides that selection away from you.)
I'm not convinced that for such a small duplication this is worthwhile.
|
|
|
|
|
Prompt to write a program to work with web camera to as I understand it to use OpenCV? Tell a escho library (preferably C #) to work with web camera and computer vision? and in what language it is better to write C # or C + + Open CV is a library of C + + swoop I know
|
|
|
|
|
Some good C# hits here[^].
The best things in life are not things.
|
|
|
|
|
Check out this article Motion Detection Algorithms[^]
It shows you how to integrate a webcam with motion detection.
It was the basis for this TeboCam[^]
One small tip - your question is very brief however computer vision is a hugely complex area - even Microsoft have a long way to go with the Kinect...
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
Hi everyBody;
I want to browse my string like this:
int k = 0;
char car = ' ';
string successZero = string.Empty;
char[] arr;
arr = chaine.ToCharArray(0, chaine.Length);
if (arr [0] != '0')
{
while (k < chaine.Length)
{
car = arr [k];
if (car == '0')
{
successZero += car;
car = ' ';
k ++;
}
else
break;
}
}
else
successZero = "";
return successZero;
but if I enter "001" the arr [0]= 48 '0' and NOT '0' only
I don't know it appears to me that 48 (I know it is the ASCII of 0)
but I want to discutate sur arr[0] == '0' or not
How to remidiate to that?
ty
|
|
|
|
|
I'm not sure what you are trying to do but your code does not seem to correspond to your description. You are looking for a zero in the arry (maybe), or trying to count them, but the line:
if (arr [0] != '0')
means that you never enter the search loop if the first character of your string is '0', as is the case you describe.
The best things in life are not things.
|
|
|
|
|
Hi,
yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001" it appears to me arr [0] = 48 '0'
what can I do?
ty
|
|
|
|
|
What are you expecting arr[0] to be?
With you input of "001" you can expect your array of characters to be:#
arr[0] = '0'
arr[1] = '0'
arr[2] = '1'
So in this case your code is going to me the condition of (arr[k] != '0') on the 3rd iteration of your loop, not on the first.
Is this what you’re looking for?
|
|
|
|
|
Pierre besquent wrote: yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001"
No it doesn't; your coding logic is wrong as I stated in my previous answer.
I have a feeling something is still getting lost in translation here, but I cannot figure it out.
The best things in life are not things.
|
|
|
|
|
This is simply a viewing thing in your IDE. arr[0] will be '0' in this case. The problem is that if arr[0] is '0', the outer if fails, and if it is not '0', the if inside the while will fail on the first iteration and break out of the loop. You have a simple logic problem.
I think the outer if is entirely unnecessary (even if it is corrected).
|
|
|
|
|
Hi,
viewing thing in your IDE?what u mean. I debug and I found that arr[0] = 48 '0' even if chaine = "001". I know what i write so this it is my probleme: WHY it considerate arr [0] = 48 '0'??????
ty
|
|
|
|
|
Your debugger is showing to you that arr[0] contains char '0' which is the same as short 48.
short number = 48;
char letter = '0';
Console.WriteLine(number == letter);
Hope, this makes is clear.
|
|
|
|
|
Hi,
thanks for the clarification but still not clear HOW to test if arr [0] == '0' or not??
still my debugger returns to me arr [0] = 48 '0'.
ty
|
|
|
|
|
Pierre besquent wrote: HOW to test if arr [0] == '0' or not??
Well you could use a simple expression like:
if (arr[0] == '0')
if (arr[0] == 0x30)
if (arr[0] == 48)
You need to try and clarify your question as it is still not obvious to us what your problem is.
The best things in life are not things.
|
|
|
|
|
You are debugging in an IDE. It is showing you the integer value as well as the character, in case it is an unprintable character. arr[0] is still equal to '0' and the problem is that the two tests in your code contradict each other.
(edit: oops, not a BBcode forum.)
|
|
|
|
|
hi
how can I perform a group by on a DataTable's rows using dynamically determined group-by-columns?
For example, below, I group by "Symbol" and "Measure" - however, what if group by column is passed into the function as argument IList<string> GroupByColumnNames
<br />
static void TestLINQGroupBy()<br />
{<br />
#region Construct dummy data<br />
Random rnd;<br />
IList<string> Measures = new List<string>() { "eps", "bps", "close" };<br />
IList<string> Symbols = new List<string>() { "0001HK", "0002HK", "0003HK" };<br />
DataTable TestPrice = new DataTable("TestPrice");<br />
DataColumn c = null;<br />
DataRow newRw = null;<br />
<br />
DataColumn[] PKs = new DataColumn[3];<br />
c = new DataColumn("Symbol", typeof(string));<br />
TestPrice.Columns.Add(c);<br />
PKs[0] = c;<br />
<br />
c = new DataColumn("Measure", typeof(string));<br />
TestPrice.Columns.Add(c);<br />
PKs[1] = c;<br />
<br />
c = new DataColumn("RecordDate", typeof(DateTime));<br />
TestPrice.Columns.Add(c);<br />
PKs[2] = c;<br />
<br />
TestPrice.Columns.Add("Value", typeof(double));<br />
TestPrice.PrimaryKey = PKs;<br />
<br />
foreach (string Symbol in Symbols)<br />
{<br />
foreach (string Measure in Measures)<br />
{<br />
for (int iter = 1; iter < 12; iter++)<br />
{<br />
newRw = TestPrice.NewRow();<br />
newRw["Symbol"] = Symbol;<br />
newRw["Measure"] = Measure;<br />
rnd = new Random((Symbol + Measure + iter).GetHashCode());<br />
newRw["Value"] = rnd.NextDouble();<br />
newRw["RecordDate"] = new DateTime(DateTime.Now.Year, iter, 1);<br />
TestPrice.Rows.Add(newRw);<br />
}<br />
}<br />
}<br />
#endregion<br />
<br />
var q = from rw in TestPrice.Select()<br />
group rw by new {Key1=rw["Symbol"], Key2=rw["Measure"]} into key<br />
select key;<br />
foreach (var k in q)<br />
{<br />
Console.WriteLine(k);<br />
}<br />
return;<br />
}<br />
REF: 101 LINQ Examples[^]
dev
|
|
|
|
|
Have a look at this neat article[^], especially the GroupData function. Once you dynamically build the data, you can use
Func<DataRow, String> groupingFunction = GroupData; as shown in the article.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
Hi,
I am calling a .dll from C#. There is no problem loading the dll and call some functions from C#, but one function call gives
"System.AccessViolationExceptionMessage: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
There is no problem to call this function from c++.
I suppose something is wrong with the structures I use in C# but I'm not sure what and how to solve this issue.
Using Visual Studio 2008.
********************************** C++ **********************************
typedef enum _MAPI_APN_TYPE
{
MAPI_APN_PRIMARY = 0,
MAPI_APN_ACTIVATION,
}MAPI_APN_TYPE;
typedef struct _MAPI_APN_DATA
{
wchar_t wcApn[80];
wchar_t wcUsername[80];
wchar_t wcPassword[80];
wchar_t wcDialNumber[80];
}MAPI_APN_DATA, * LPMAPI_APN_DATA;
typedef struct _MAPI_CORE_PATH
{
wchar_t strPath [ 260 ];
}MAPI_CORE_PATH, *LPMAPI_CORE_PATH;
***** c++ function that works: *******
MAPI_APN_DATA data;
MAPI_CORE_PATH path;
memset( &path, 0, sizeof( MAPI_CORE_PATH ) );
result = M_API_GetApnData( &path, MAPI_APN_ACTIVATION, &data );
********************************** C# **********************************
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MAPI_APN_DATA
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string apn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string username;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string password;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string dialNumber;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MAPI_CORE_PATH
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string strPath;
}
public enum MAPI_APN_TYPE
{
MAPI_APN_PRIMARY = 0,
MAPI_APN_ACTIVATION,
};
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate MAPI_RESULT M_API_GetApnData(ref MAPI_CORE_PATH path, MAPI_APN_TYPE apn_type, ref MAPI_APN_DATA data);
private M_API_GetApnData fnMGetApnData;
pFunction = MbmFramework.Native.GetProcAddress(pMClientDll, "M_API_GetApnData");
fnMGetApnData = (M_API_GetApnData)Marshal.GetDelegateForFunctionPointer(pFunction, typeof(M_API_GetApnData));
MAPI_APN_DATA data = new MAPI_APN_DATA();
MAPI_CORE_PATH path = new MAPI_CORE_PATH();
MAPI_RESULT apiRes = fnMGetApnData(ref path, MAPI_APN_TYPE.MAPI_APN_ACTIVATION, ref data);
Best regards
Olof
modified on Monday, July 4, 2011 4:54 AM
|
|
|
|
|
Does the exception occur inside the fnMGetApnData() function? If so you need to track exactly what is going on in there, and without seeing the code it is impossible for anyone here to make a guess.
The best things in life are not things.
|
|
|
|
|
Yes, fnMGetApnData() is the unmanaged call to the dll. I don't have the source code to this dll. I tried to debug this unmanaged call in the assembler code but have not enought assembler experience. The only thing I can see is that a assembler "call" is causing the exception.
|
|
|
|
|
marca292 wrote: I don't have the source code to this dll
It's unlikely that anyone here will be able to guess what's going on. Is there any way you can get hold of the people who created the dll?
The best things in life are not things.
|
|
|
|
|
I was just wondering if anyone could see something wrong with my C# struct declaration that could cause this error.
Maybe, I shall see if I can get hold of the people who created this dll.
|
|
|
|
|
Make sure wchar_t is what you think it is. I don't see anything obviously wrong with the structure definitions.
Alternatively, does the function rely on the path being set to zeros? You are doing that in C++ and not C#. You might need to assign "\0" to path.strPath if the C function expects it to be set to zero length to start with. (I'm not sure what the marshalling does with an unassigned string variable in a struct. You might need to assign a value to all the string properties; a constructor or method on the struct to do that might be a good idea.)
|
|
|
|
|
Try changing the CharSet to CharSet.Ansi.
|
|
|
|
|
I'm confused as to why you're using the UnmanagedFunctionPointerAttribute to get the dll function and then go through all that marshalling mess. Wasn't a simple DllImport enough?
|
|
|
|