|
Hi
try this: (you find a complete collectionbase class that implement IBindingList, .....)
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=0B47DA60-DF5F-4A28-ABF7-6F8EB977FCFD
|
|
|
|
|
Is it at all possible to kill the error 'Not all code paths return a value'?
I have several classes that need to throw exceptions when some methods are called or the user tries to set some properties. ('You cannot bla bla bla on a NullObject') We're talking *lots* of different places that will throw this particular exception.
Since I am a structured kind of person, I'd like to standardize the message and information that is logged. Therefore I thought it would be smart to defer the actual throwing and logging to a common method that can be called from all the places.
However, when I do, the C# compiler complains that not all code paths return a value.
The code does exactly the same thing as before (throws an exception) except it's maintainable now, so how do I make the compiler stop bugging me about this ""error""?
I'm using VS 2005
Thanks on beforehand
/Joachim
P.s. I tried using #pragma, but it seems the compiler thinks this is a real error, and says '161' is an invalid warning number... Or did I do it wrong? Ds.
--
Ubi Signo?
|
|
|
|
|
I don't think you'll be able to suppress this error.
The only workaround I can think of is to let the method you use to throw the exception return a value, but this seems quite awkward, too.
I guess it would be better to log in the constructor of your exception or add an event handler for unhandled exceptions and perform your logging there.
Regards,
mav
|
|
|
|
|
mav.northwind wrote: I guess it would be better to log in the constructor of your exception or add an event handler for unhandled exceptions and perform your logging there.
Regards,
mav
Ah. Good idea. Didn't think of that. Will put the logic in the constructor for now.
Thanks for your suggestions.
Regards
/Joachim
--
Ubi Signo?
|
|
|
|
|
joachimF wrote: Is it at all possible to kill the error 'Not all code paths return a value'?
No.
joachimF wrote: I have several classes that need to throw exceptions when some methods are called or the user tries to set some properties
The compiler is intelligent enough to realise this. There must be some other particular path through the method that is not returning a value or throwing an exception.
joachimF wrote: I tried using #pragma, but it seems the compiler thinks this is a real error, and says '161' is an invalid warning number
That is correct. This is a real error. The code cannot compile while it exists.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Colin Angus Mackay wrote:
> The compiler is intelligent enough to realise this. There must be some other particular path through
> the method that is not returning a value or throwing an exception.
Are you sure? Perhaps there is an error in my code then.. Here it is;
public interface SomeInterface
{
void ChangeState();
int SomeFunction(int arg);
int SomeMethodWithSideEffect();
int SomeProperty
{
get;
set;
}
}
class MessyNullObjectVersion : SomeInterface
{
public void ChangeState() { throw new ApplicationException("Cannot set properties or change state of null objects."); }
public int SomeFunction(int arg) { return arg; }
public int SomeMethodWithSideEffect() { throw new ApplicationException("Cannot set properties or change state of null objects."); }
public int SomeProperty
{
get { return 0; }
set { throw new ApplicationException("Cannot set properties or change state of null objects."); }
}
}
class MaintainableNullObjectVersion : SomeInterface
{
public void ChangeState() { NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject(); }
public int SomeFunction(int arg) { return arg; }
public int SomeMethodWithSideEffect() { NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject(); }
public int SomeProperty
{
get { return 0; }
set { NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject(); }
}
}
class NullObjectHelper
{
public static void Throw_InvalidToCallOrSetOnNullObject()
{
throw new ApplicationException("Cannot set properties or change state of null objects.");
}
}
The 'Messy' version will compile just fine, but the 'Maintainable' version will say something on the lines of 'SomeMethodWithSideEffect - not all code paths return a value'.
I have a hard time seeing how 'SomeMethodWithSideEffect' can avoid throwing an exception... Please explain.
--
Ubi Signo?
|
|
|
|
|
oh... I see now... The method SomeMEthodWithSideEffect() isn't actually throwing the exception, its calling another method to do that. I must have misunderstood what you were doing. The compiler doesn't know about the other method or that it only ever throws an exception, it just knows where it is to call it.
I would suggest that your NullOjectHelper does what it needs to do in order to construct the exeption and do the logging you need. Then return the exception object and you then throw it from where you need it. So your code becomes:
public int SomeMethodWithSideEffect()
{
throw NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject();
}
class NullObjectHelper
{
public static Exception Throw_InvalidToCallOrSetOnNullObject()
{
return new ApplicationException("Cannot set properties or change state of null objects.");
}
} Then the compiler can see that an exception is being thrown.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Another good suggestion for a workaround.
Thanks!
Regards
/Joachim
--
Ubi Signo?
|
|
|
|
|
The problem ONLY exists in this line
public int SomeMethodWithSideEffect() <br />
{ NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject(); }
as that method MUST return something. So another workaround is to have an overloaded helper
class NullObjectHelper { <br />
public static void Throw_InvalidToCallOrSetOnNullObject() <br />
{ throw new ApplicationException("Cannot set properties or change state of null objects."); <br />
} <br />
<br />
public static int Throw_InvalidToCallOrSetOnNullObject() <br />
{ throw new ApplicationException("Cannot set properties or change state of null objects."); <br />
} <br />
}
Then
public int SomeMethodWithSideEffect() <br />
{ return NullObjectHelper.Throw_InvalidToCallOrSetOnNullObject(); }
|
|
|
|
|
Another interesting suggestion. However, the compiler doesn't seem to allow overloads that differ in return type only. There has to be some parameter differences to make it work...
Thanks anyway.
Regards
/Joachim
--
Ubi Signo?
|
|
|
|
|
doh!! Course not... silly me!
|
|
|
|
|
Hi,
I want to convert the string which is in english to some other language like danish, italy...
Thanks
Warm Regards
Prakash-B
|
|
|
|
|
You cannot convert the string in other languages. You just can change the format of the text including its indention. If you want to change the text, you have to store the text indifferent languages in some external file which may be a resource file and then read the text based on the current culture of the system.
Akif
|
|
|
|
|
PrakashBhaskar wrote: I want to convert the string which is in english to some other language like danish, italy
Then you need to hire a translator, or get a friend who can speak those languages to help.
CultureInfo knows nothing about these languages except that they exists. It is used to select the correct language resource, however, a human still has to define what the resources are for each language in which they are to be available.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Hi,
Thanks for your information.
Thanks
Warm Regards
Prakash-B
|
|
|
|
|
So the basics of my app are completed, everything i'll need is complete (except directplay server) and it works, perfectly i might add. but it only works on MY computer whenever i try to run it on another it exits with a error code and a text saying: "Failed security policy check". if i try to run the app from my comp but from another computer's drive (via lan) the debbuger kick in and i get the above message+ "Unverifiable assembly". google had ssomething to say about the problem but i don't know how to implement the solution found there:
"A temporary method of solving this issue is to simply give the LocalIntranet
zone more trust. This can be done in the .NET Framework Configuration
control panel applet. A permanent solution is to modify your security policy
to give FullTrust to all assemblies signed with your key."
to the problem as stated there (which i think is my problem):
"If you're running (and remote debugging) your app from a local network
share, then I suspect that you're running into a permissions issue. Programs
that run on the CLR use something called code access security (CAS), which
assigns trust based upon evidence it can gather from the code itself. This
evidence is then compared against the security policy, and a permission
grant is generated."
anyway i'll be including the debug output when i run the app on another conputer, i hope that my post isn't too long and boring.
"'forms.exe': Loaded '\\Homepc\2games (d)\forms\Debug\forms.exe', Symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorwks.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\msvcr71.dll', Symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\fusion.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_49459abc\mscorlib.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorsn.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\urlmon.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\system32\version.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\assembly\GAC\System\1.0.5000.0__b77a5c561934e089\System.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\assembly\NativeImages1_v1.1.4322\System\1.0.5000.0__b77a5c561934e089_70640dd3\System.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\assembly\GAC\System.Drawing\1.0.5000.0__b03f5f7f11d50a3a\System.Drawing.dll', No symbols loaded.
'forms.exe': Loaded 'C:\WINDOWS\assembly\NativeImages1_v1.1.4322\System.Drawing\1.0.5000.0__b03f5f7f11d50a3a_842e96f6\System.Drawing.dll', No symbols loaded.
The thread 'Win32 Thread' (0x1c0) has exited with code 0 (0x0).
'forms.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\diasymreader.dll', No symbols loaded.
'forms': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'forms': Loaded 'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.
'forms': Loaded 'c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
An unhandled exception of type 'System.Security.Policy.PolicyException' occurred in Unknown Module.
Additional information: Unverifiable assembly '\\Homepc\2games (d)\forms\Debug\forms.exe' failed policy check.
The thread 'Win32 Thread' (0x218) has exited with code -532459699 (0xe0434f4d).
The program '[3192] forms.exe: forms' has exited with code 0 (0x0).
The program '[3192] forms.exe: Native' has exited with code 0 (0x0)."
m$: It is normal for your screen to go blank and for your computer to restart itself several times
|
|
|
|
|
You should start reading about CAS (code access security).
MSDN has some articles on the topic that will help you learn how your application behaves differently when run from an untrusted source (for example, a mapped network drive).
Regards,
mav
|
|
|
|
|
i don't really want to know how the app behaves i just want it to run, how can you make an untrusted source trusted so my program can run properly, i really need this program to run on just a couple of computers so modifying stuff would be posible but what do i need to modify?
i've read something realted to my problem but i have no ideea as to how i could generate the certificates required or integrate them inot my code
m$: It is normal for your screen to go blank and for your computer to restart itself several times
-- modified at 7:36 Tuesday 3rd January, 2006
|
|
|
|
|
navvara the infantryman wrote: don't really want to know how the app behaves i just want it to run,
This is a heavily misguided response to a perfectly reasonable answer to your question. Your problem is related to Code Access Security - a feature of the .NET framework - if you dont care to understand for yourself an important feature you are working with then don't expect any more help from the kind people here.
|
|
|
|
|
if my reply was abusive then i am sorry it's just the result of seing my work waste away when i thought it was completed and running perfectly + not having time to read all that documentation as my project is due on monday
what i should have wrote is "i'm a dummy and don't know what all that means have you got a straightforward solution? (magic bullet, something simple?)"
PS: from what i have read my app's assembly is checked when run but i don't know even if my app has one or how to make one or check it
m$: It is normal for your screen to go blank and for your computer to restart itself several times
-- modified at 8:00 Tuesday 3rd January, 2006
|
|
|
|
|
I didnt say it was abusive, I said it was misguided.
|
|
|
|
|
|
Hi all, i need to call the Windows XP File Properties window from my application (giving it the path of the file or folder to open its roperties window)
Hints:
1- my application is in VB.Net
2- operating system : Windows XP Prffessional
Please, if any one could help, reply me as soon as possible
Thank u all
|
|
|
|
|
First u posted in C# forum ur vb.net question but anyway here is one example
http://www.kdkeys.net/forums/678/ShowPost.aspx
|
|
|
|
|
First: Sorry for posting in the wrong forum, but i posted my question in the VB.Net forum also if any one need to continue the discussion there.
Seconed: It was not my question "How to find a file/or Folder properties?"
i mean how to call the actual 'File/Folder Properties Window' embeded in Windows XP OS
Thank u
|
|
|
|