|
suprsnipes wrote: For your information the code DIDN'T work. If it did I wouldn't be asking for help still...
Perhaps it was this that raised the ire. The is a whole ettiqute involved with bulletin boards, and you've broken a few of them (e.g. reposting). Read the guidance at the top of the page. You also need to remeber that the regulars here frequently deal with students whose attitude is "I can't do my homework so here is the the question I've been set" normally finished with "This is urgent snd codez plz". I don't think your OP was like that, but we're all a bit jaded in here, and some people reuse to help students at all as they should be working this stuff out for themselves.
suprsnipes wrote: surely you guys remember what it was like starting out?
Yep. But the problem is you posted code that wasn't even close to to achieving what is needed (for examplethere is no comparison at all of the values ) and you haven't properly explained what you want, and (because the code isn't close) what is going wrong.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
suprsnipes wrote: perhaps I should have said "the code didn't do what I required".
This is where I am confused. You said you wanted to select the smallest integer greater than a certain floating point value which was rounded up. When I ran the code it worked fine and produced the correct result. You did not show the results you had received so there was no way of knowing what was different with your test.
suprsnipes wrote: My intention was not to offend anyone, surely you guys remember what it was like starting out?
Well CodeProject did not exist for one thing; I had a pencil, a coding pad, and a reference manual to help me - and hours of testing.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Ok, thanks for your reply Richard, let me convey my apologies.
This is my input, which is being read
4843
4811
4792
The floating value is 4820.
The result that I am getting is 4811.
The result that I am after is 4843, which is the next value up from 4820.
I have posted the code below;
int roundedValue;
int temp, result = 0;
int? diff = int.MaxValue;
readText = File.ReadAllText("C:\\PIn.txt");
string [] split = readText.Split(new Char [] {' '});
foreach (string s in split)
{
count++;
temp = int.Parse(s);
levels[count] = temp;
int roundedValue = (int)Close[0];
if (temp > roundedValue)
{
if (diff == 0)
diff = roundedValue;
if ((temp - roundedValue) < diff)
{
result = temp;
diff = temp - roundedValue;
}
}
}
Print("Result: " + result);
|
|
|
|
|
I just copied and pasted the above code into my C# compiler (twice to be sure) and it gives the correct result 4843! I cannot see why you should not get the same result, unless there is something external to this piece of code that is the problem. Check your input data, and try also stepping through your program with the debugger. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
You could cheat a little and use a list and it's sort method to help. Create the base list, add your value, sort it, get the index of your value and increment to get the next number up. Not very mathmatical or elegant (and probably inefficient) but it works.
class Program
{
static void Main(string[] args)
{
List<string> inputFromFile = new List<string>(3) {
"4843", "4811", "4792" };
int value = (int)4820f;
List<int> list = inputFromFile.ConvertAll(
new Converter<string, int>(StringToInt));
list.Add(value);
list.Sort();
int index = list.IndexOf(value);
if (index < (list.Count - 1))
Console.WriteLine(list[index + 1]);
else
Console.WriteLine(value);
Console.ReadKey();
}
private static int StringToInt(string text)
{
int result;
int.TryParse(text, out result);
return result;
}
} Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hello
I'm a newbie with WebService creation
For now, everyhing works fine, my Service run on a server and my Client can use it
Now I need to add autentification to the service ...
How can I do that simply on the server site ?
It does'nt need to be very secure, I just want to understand how to do it
I have an example of another service that I'm using as a client
I just have to assign a NetworkCredential to the service's client instance to be able to use it
How can I achieve that on the server side
Thanks a lot for any help
|
|
|
|
|
Consider this example that works:
List<String> list = new List<string>(){"1","2","a","b"};
list = list.FindAll(delegate(String s)
{
if (s != "a")
{ return false; }
return true;
});
I can't understand why we can't do this :
List<T> list = (from c in oq select c).ToList();
list = list.FindAll(delegate(T o)
{
if (o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() != company)
{ return false; }
return true;
});
The 'propertyName' is a property that exists in the objects naturally.
This compiles fine but throws an object null reference exception...
Thank you for time...
|
|
|
|
|
The second code is very convoluted, I don't know why you are using reflection to get the property type.
List<T> list = (from c in oq select c).ToList();
list = list.FindAll(o => o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() == company));
is cleaner, but you can do the find all operation as where clause in the select. The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
Keith Barrow wrote: The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.
That seems impossible because T has always got 'propertName' property and the value is never null because it's part of a primary key.
Keith Barrow wrote: you can do the find all operation as where clause in the select.
Maybe I didn't show enough code to make clear why I can't do the 'FindAll' in the 'Where' clause ...
var oq = (ObjectQuery<T>)(p.GetValue(EntityObject, null));
List<T> list = (from c in oq select c).ToList();
The type of the ObjectQuery is only known in runtime because I get a specific ObjectQuery thru reflection in runtime.
so
from c in oq select c where c.'no properties here' select c
...
|
|
|
|
|
Ok so it was the 'propertyName' that was incorrect, my mistake, sorry !
All is well and thank you for your input.
|
|
|
|
|
i make a program that use four for loops, and there is no error but! the program is too slow, i tried to make it in library, it make it a little more faster but not that faster, is there a way to make it works more faster?
|
|
|
|
|
How slow is it? How many loops do you use? Considered reducing loops?
sanforjackass wrote: there a way to make it works more faster?
by first posting a code here, witch we do not know how to help you
|
|
|
|
|
my program to search for an object in photo with ROI algorithm, that means i have to make two loops for the photo and two for the mask.
|
|
|
|
|
|
|
Then that's the reason it's slow
|
|
|
|
|
|
Use lockbits (only once, outside the loops) and unsafe code
|
|
|
|
|
Good catch! Regards,
Rob Philpott.
|
|
|
|
|
Thanks, the first time I used pointers in C# was for this purpose, so I remember it well
|
|
|
|
|
This is going to be a slow process, due to the size of the photo files and the complexity of what is being done. I suggest Parallelising the top level loop (the one that processes each photo in turn) so that you process multiple photos at the same time. If you can, consider parallelise the inner loops as well. Naturally this will make the code harder to understand.
There is a pdf knocking around introducing parallel extensions in .net 4, written by Daniel Moth. It is actually a good introduction to parallelism, and will help you design your code with the extensions in mind.
[Edit]
-- Or --
Do what Harold Suggests first!Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
modified on Thursday, March 11, 2010 5:46 AM
|
|
|
|
|
sanforjackass wrote: it make it a little more faster but not that faster, is there a way to make it works more faster?
Withous seeing what the code does, IMO you've three real options:
- Refactor to run in parallel. This is only an option if the order things are processed in does (or can be made to) not matter
- Re-implement using pointers. This will work best if one of the lists/collections you are itering over is long and the processing task is short-lived
- Profile the code and optimize the task that is taking the most time.
It might be helpful if you post code, of give psuedo-code about what you application is actually doing.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
IMO do the second tip first (MAJOR speed increase, between 5 and 100 times), then do the first one (speed increase depends on the processor, best-case scenario is 1x extra increase for each processor core [such as 4x for a 4-core processor]). After those two major optimizations (Use .NET 4.0 Beta's Parallel.For method for the first tip), profile the code (NProf is a good sampling profiler, also Visual Studio 2010 Beta 2 [free to use until June] comes with an excellent profiler. If you have the money, use ANTS, as I have heard only good things about it) and fix the bottlenecks (those areas that take the most time).
|
|
|
|
|
|
Hi All
I have a project which is using Microsoft.Jet.OLEDB.4.0, to connect to an access database.
Windows 7 x64 does not allow my project to connect to the database.
Is there another way in which I can connect to the database in Windows 7 x64
Thanks
|
|
|
|
|