|
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
|
|
|
|
|
I am having UserControl1.ascx and WebForm1.aspx Files in my project. I have included Usercontrol1.ascx in WebForm1.aspx page. UserControl1.ascx contains Login Info (user name, password and login sumit button) and the fields are validated with requiredfield validator. webform1.aspx contains registration details and submit button. here also the fields are validated with requiredfield validator. now, if i press the login button of usercontrol1.ascx the registration fields are getting validated and also if i press registration form submit button the login fields are getting validated. what i want is the login submit should validate only (username and password) and registration submit button should validate only the registration form fields. how do i resolve this problem. pls. help me out...
|
|
|
|
|
Hi,
Make both the validator enable = false on webform page load event write the following code.
//declare instance for required field validator
RequiredFieldValidator rv1 = new RequiredFieldValidator();
//declare instance for usercontrol
System.Web.UI.UserControl wui = new UserControl();
//find the usercontrol
wui = (System.Web.UI.UserControl)(this.FindControl("WebUserControl11"));
//find the requiredfield validator (which is in usercontrol)
rv1 = (System.Web.UI.WebControls.RequiredFieldValidator)(wui.FindControl("RequiredFieldValidator1")); //searching usercontrol requiredfield validator
button click event (this is in webform) write the following code
req1.Enabled = true;// this requiredfield validator is in your webform
RequiredFieldValidator r1 = new RequiredFieldValidator();
System.Web.UI.UserControl wui = new UserControl();
wui = (System.Web.UI.UserControl)(this.FindControl("WebUserControl11"));
r1 = (System.Web.UI.WebControls.RequiredFieldValidator)(wui.FindControl("RequiredFieldValidator1"));
req1.Validate();
if (IsValid)
{
r1.Enabled = false;
--statement--
}
************* user control **************
usercontrol page load event write the following code
RequiredFieldValidator reqq1 = new RequiredFieldValidator();
reqq1 = (System.Web.UI.WebControls.RequiredFieldValidator)(Page.FindControl("req1"));
reqq1.Enabled = false;
RequiredFieldValidator1.Enabled = false;
reqq1.EnableClientScript = false;
RequiredFieldValidator1.EnableClientScript = false;
usercontrol button click event write the following code
RequiredFieldValidator1.Enabled = true;
RequiredFieldValidator reqq1 = new RequiredFieldValidator();
reqq1 = (System.Web.UI.WebControls.RequiredFieldValidator)(Page.FindControl("req1"));// searching webpage requiredfield validator
RequiredFieldValidator1.Validate();
if(RequiredFieldValidator1.IsValid)
{
reqq1.Enabled = false;
--statement--
}
Thanks
Warm Regards
Prakash-B
|
|
|
|
|
hello
i want to create new SmartDeviceApplication .
the Emulator apper but my form dose not.
Pleeeeeeeeeease help me soon as you can
Ramiforu2002@hotmail.com
Ramiforu2002@yahoo.com
Rami Alkhalyleh
Rami Alkhalyleh
|
|
|
|
|
could anybody teach me how to implement the code about auto detect web drive's status of connect/disconnect and also connect/disconnect function ? thanks very much !
|
|
|
|
|
R2409 wrote: web drive's
What's a "web drive"?
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
|
|
|
|