|
I Wrote The Following Code But It has An Error
Function Fun(Of T)(X as T,Y AS T) AS T
Return X+Y
End Function
The Error Is :
That Operator + Is not Defined In Type T
''''
Ok But How Can I Make A generic Method To Make A + Math Operation For All Numerical Types
I love Peace And Programming
|
|
|
|
|
This isn't going to work, and it's completely useless.
You can't do what you want to do using a generic method. You're not constraining your Types to only value types. The addition operator is not defined for all possible types that you're allowing (which is basically everything), so you'll get this error. The problem is that you can't use the Int32, Int64, Byte, yada, yada, yada, value types as contraints because they are all structures, not classes. You can only use classes and interfaces as Type contraints, and only classes that expose a parameterless constructor. Structures do not following this rule, so they can't be used.
|
|
|
|
|
I thought there was a constraint you could use that specified that the object offered mathematical operations ? Will where T: struct do it ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: I thought there was a constraint you could use that specified that the object offered mathematical operations ?
All the base value type inherit from ValueType (obviously), but I couldn't use ValueType in the constraint at all. It's a MustInherit class with no parameterless constructor. I couldn't find anything that limited the possible types to "math only" implementors, or anything that would describe a type as being one.
|
|
|
|
|
Hi,
I have an app that retrieves and calculates a lot of data. The whole process may take about 20 minutes. To prevent the user from thinking that the app has crashed, I added a progress bar, and to give the user a better idea of the progress, I make the form's title bar display a percentage.
The problem is that at pretty much any percentage the form no longer updates itself. The application keeps running, no problem, but the progress bar and percentage in the title bar get stuck, which effectively defeats the purpose.
Can anyone tell me why this may happen ?
Cheers,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
Johan Hakkesteegt wrote: I have an app that retrieves and calculates a lot of data. The whole process may take about 20 minutes. To prevent the user from thinking that the app has crashed, I added a progress bar, and to give the user a better idea of the progress, I make the form's title bar display a percentage.
The problem is that at pretty much any percentage the form no longer updates itself. The application keeps running, no problem, but the progress bar and percentage in the title bar get stuck, which effectively defeats the purpose.
Can anyone tell me why this may happen ?
The UI will not respond so long as there are other things to do... looks like all this is in the same thread, maybe you can do your process in a separate thread?
|
|
|
|
|
Thanks for the response.
The funny thing is though, that it does work for a while. At the moment the UI freezes, the app is retrieving data from a database with a succession of datareaders. In other words it is repeating the same task over and over (with different parameters each time ofcourse).
If we consider your theory, why does the UI not freeze right away?
My advice is free, and you may get what you paid for.
|
|
|
|
|
Johan Hakkesteegt wrote: The funny thing is though, that it does work for a while. At the moment the UI freezes, the app is retrieving data from a database with a succession of datareaders. In other words it is repeating the same task over and over (with different parameters each time ofcourse).
If we consider your theory, why does the UI not freeze right away?
Your description of the problem reinforces my theory You usually find that the UI becomes unresponsive when there are repetitive tasks...
|
|
|
|
|
The UI thread is busy doing all of your operations. It would appear that every once in a while, the thread is freed to process the Window's message pump, thereby processing all the WM_PAINT messages for the window, which means you get to see your progressbar and window title updates.
So long as the thread is busy doing other things, these paint messages do not get processed, thereby looking like the application hung.
You have little choice but to move these operations to a seperate thread and have this processing code fire off progress messages every once in a while. The UI thread will be able to process these messages immediately, while your processing thread does it's job. You may want to look into using the BackgroundWorker class make this process easier.
|
|
|
|
|
Hello,
Is there a way to define a function that have an optional parameter whith value = today as following:
private function f(optional d as date =today)
end function
Thanks
Shay Noy
|
|
|
|
|
Yes, VB.NET does support this. I don't use VB.NET, so you'll need to google for the syntax.
http://www.google.com.au/search?sourceid=navclient&ie=UTF-8&rlz=1T4ADBS_enAU225AU226&q=vb%2enet+optional+parameter[^]
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
In C# it is OK too, can you show me how?
Thanks
Shay Noy
|
|
|
|
|
No
Google[^]
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
What is suppose to be?
No
Google[^]
Shay Noy
|
|
|
|
|
Did you click on the link Christian provided you?? It lists a bunch of articles on the use of optional parameters in C#.
|
|
|
|
|
Yes , I ve checked but I didn't find what I am looking for.
Thank you
Shay Noy
|
|
|
|
|
I've read the first three articles in the list Christian gave you. Yes, the answers to the C# Optional Parameter problem you're looking for are there.
They all start by saying that C# doesn't support optional parameters. Then they go into possible solutions to get aorund this limitation. If you bothered to read them, you would have found this out.
|
|
|
|
|
I know I saw it
"...The case he makes is pretty straightforward, namely, that C# doesn't support optional parameters and forces you to use overloading..."
They say that overloading is solution. It is what you suggested to me in one of our correspondence below.
What I've searched was optional date parameter with default value [today].
Thank you
Shay Noy
|
|
|
|
|
Yep. C# doesn't support option parameters, no matter what the type of the parameters are.
The absolute #1 skill you can have as a developer/programmer/whateverYouWantToCallIt is the ability to do research, not write code. You searched for a VERY specific occurance of a situation and stopped there. You didn't generalize your search parameters at all when you Googled for "optional date parameter with default value". You should have opened this up to possibly more hits by searching for "C# optional parameters".
|
|
|
|
|
Thank you for the tip
Shay Noy
|
|
|
|
|
No, despite my pleading, the C# team refuses to impliment them. They are stuck on the idea that it has to be complicated ( i.e. support for named parameters, etc )
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
No.
you can not specify an expression/variable while defining an optional parameter.
it needs to be a constant......
Ashish Sehajpal
|
|
|
|
|
Try this :-
Function f(Optional Byval d as Date = nothing) as object<br />
If d = nothing then<br />
d=System.Date.Today<br />
End If<br />
' rest of your code here<br />
End Function
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
Thank you , this is what I am doing but this function is a part of a dll that I wrote and it is used by 5 different programmers.
When they used this function then instead of seeing that the optional date is set to today they see that the optional date is set to nothing (#12:00:00#). It is not very elegant for the programmer.
Shay Noy
|
|
|
|
|
Agreed, but I see no other solution. I would suggest that you add a metadata description to the function and detail what will happen if the date passed to the function is nothing.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|