|
An implicit cast is safer that using 'as', but I am lazy, so I prefer to use 'as'
|
|
|
|
|
<br />
object a = 123;<br />
<br />
try {<br />
string s1 = (string)a;<br />
Console.WriteLine("method 1 value: {0}", s1);<br />
}<br />
catch {<br />
Console.WriteLine("method 1 error.");<br />
}<br />
<br />
try {<br />
string s1 = a as string;<br />
Console.WriteLine("method 2 value: {0}", s1);<br />
}<br />
catch {<br />
Console.WriteLine("method 2 error.");<br />
}<br />
<br />
method 1 error. <br />
method 2 value:
use 'as' can replace try-catch block. haha...
|
|
|
|
|
leppie wrote: An implicit cast is safer that using 'as', but I am lazy, so I prefer to use 'as'
Implicit cast? Don't you mean an explicit cast?
If you use a cast, you will get the exception at the instruction trying to do the cast. If you use "as", you will get a null reference exception somewhere else in the code, when you try to use the reference from the failed cast. Or even worse, it will silently store the value somewhere, and you get the error message weeks later when someone tries to use the stored value...
I always try to write code so that the exception occurs where the error is, then it's a lot easier to locate the source of the error.
---
b { font-weight: normal; }
|
|
|
|
|
Guffa wrote: Implicit cast? Don't you mean an explicit cast?
Hehe, yes
|
|
|
|
|
First:
I'm a fan of strong typed programming (that i think it will prevent many kind of bugs )
So I recommand using string obj="abc";
Second:
But there are times that you should use weak types
then I prefer obj.ToString();
because that one day may be I want to pass another type of object to my method
for example:
object obj=new User();
then my code will not crash but there is one thing that I shoule be aware of :NULL Reference !!! (if obj is null my colde will crash either way)
Then we can a NullSafe static method somewhere in our code
<br />
public static string GetNullSafeString(object obj)<br />
{<br />
return (obj==null?null:obj.ToString());<br />
}<br />
This way I will take advantages of polymorphism (object.ToString() ) and be sure that nothing can harm my code.
Regards
|
|
|
|
|
agree with you about your first point. I know it from a book code complete (2nd)
beatles1692 wrote: First:
I'm a fan of strong typed programming (that i think it will prevent many kind of bugs )
So I recommand using string obj="abc";
P.S the reason I use object obj="abc"; above is to explain my question.
-- modified at 6:01 Wednesday 16th August, 2006
|
|
|
|
|
thanks to all of you. so kind guys.
|
|
|
|
|
WuJunyin wrote: string s4 = obj.ToString(); // method 3
This one is dangerous. While it may work for objects, It may fail for classes, which override the ToString() method.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
hi all, i have 12 buttons each shows a different hidden panel (visible=false) on my form.
i cant edit tab order of my panels because if i try to adjust tab order of a specific panel, all other components on the rest of 11 panels show up
and i do not know how to deal just with the selected panel ?
secondly, if i want to change something (add or remove component) on a specific panel, how can i bring it to front ?
(if i place panels from 1 to 12 respectively, then 1 remains at the bottom and 12 shows at the top)
let's say how i may bring panel8 to the top ?
help please ...
ps: while working on a panel, till now i was resizing the rest 11 panels and placing them at a far corner of my form. but finally when i resized them to their normal state, their components were acting strange due to their dock (anchor) properties. then i spent hours to fix their positions on panels.
|
|
|
|
|
|
guys.....
i am makin an online help for a project ..using java script i am using javascript for tool tip for few words... but that i have to hard core....and that is very very difficult for N number of words.... also for help the pages will increase /decrease from time to time .. and so it will be difficult to maintain and ofcourse it will be stupid to add tool tip for every word especially when that word is called N number of times on same page or other pages
so i was looking for datbase connection ... well there will be a word with tool tip of it in database ... so whenever a page loads ...the code will scan the page and search for words in database and if there is a word in database that is also in the page .. the tool tip should automatically come up .. this tool tip popup will be using javascrit ...
as far as i have gone .. i am not able to do much ... i am able to search /highlight the word on page .... but that not using database .. that i did using an ASP code which includeed a text box in hidden mode and a submit button which enables itselfs on page load... this is workin fine .... but as they say ... there is NO end to technology ... so i want it ....
kamal
-- modified at 5:52 Wednesday 16th August, 2006
|
|
|
|
|
I suggest that think more on the reader when you write. If you write out the whole words, instead of using strange abbreviations like "m", "bt", "b" and "shld", the text gets easier to read. Also, putting the text in proper sentences, instead of writing everyting in a vague unstructured flow, makes it a lot easier to follow.
Then I might have been able to read all of your post and perhaps give some advice. Now I just got halfways through the first paragraph before my mind got occupied by wondering why you write in a manner that does everything to confuse the reader...
---
b { font-weight: normal; }
|
|
|
|
|
got you ....but i dont think its very diffucult for a reader also to read bt to reada as but ....anyways ..... takin your consedration ill edit my post ....rest ill see how far you can help me
|
|
|
|
|
kamalsss wrote: but i dont think its very diffucult for a reader also to read bt to reada as but
No, it's not very difficult to understand, but you have to stop in the middle of the sentence to decipher that word, and then you have to start over. Every misspelled word breaks the flow, and if there are too many of them you don't get any flow at all.
Most people that possess a lot of knowledge read quite a lot (as information often is concealed inside things called "books" and "articles" ). They are used to read text with proper grammar and spelling, and get annoyed if you use bad grammar and text message abbreviations. The result is that they ignore your postings, leaving mostly newbie kids to answer your questions.
---
b { font-weight: normal; }
|
|
|
|
|
got you ... thats why i have modified my post ....hope you or someone knowledgable will answer my query ...
looking forward to hear from some one soon ...
|
|
|
|
|
read your post ... still looking strange ... would be better ... to write proper sentences ... makes the text more readable ... have found ... separating text in sentences ... one of the things that does ... most for readability ...
---
b { font-weight: normal; }
|
|
|
|
|
sir ... i have read my post twice ... as i have written it so i dont find any as such strange thing in my post it will be very thankful of yours if u tell me .. what i have to change ......
Thanks
|
|
|
|
|
A sentence starts with a capital letter.
A sentence ends with a period (or occationally with a question mark or an exclamation mark).
In your text you have nothing that separates the sentences. To make it worse, you are using some strange separator that you invented yourself (?) that consists of a random number of periods, and you are using it in the middle of sentences.
---
b { font-weight: normal; }
|
|
|
|
|
didnt got u ........................
|
|
|
|
|
Hi guys.....
I have a web browser control in my C# application.How do I handle the issue when browser get the errors like "Page not Found".
Thanks
|
|
|
|
|
Hi,
I am using .net 2005 for creating reports in Crystal reports.
I have one main report and 4 subreports on one print.
However I want to display only one subreport at a time and hide other 3 subreports dynamically in the code.
Is there any way to hide or superss the 'Sub-Report' or 'Section' of the report.
Plz help me. Its critical.
abhijeet
|
|
|
|
|
IN crystal Report
right click the sub report(main report)
FORMAT EDITOR->CHOOSE SUB REPORT(FOURTH ONE)->CHECK SUPPRESS BLANK SUB REPORT
ITS WORKING FINE
|
|
|
|
|
How to insert the image in database from C#gui?
How to retrive the image in C# GUI from database?
|
|
|
|
|
|
hi all..
how can i print a form in C#.net..
-- modified at 7:08 Wednesday 16th August, 2006
|
|
|
|