|
Well I kind of like the as even though it resembles vb code. I'm used to type casting in c++ where I constantly check for null values but I would much rather throw an exception.
But then again exceptions in .NET are very costly and therefore as would work better.
But then again if your not using reflection you should know what the object was and it would be easier to test for null.
I guess would say the () is better in that it throws an exception unless it is a popular function in that case I would use as to keep it running fast.
My opinion, critices and comments welcome
nick
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Ista wrote:
I guess would say the () is better in that it throws an exception unless it is a popular function in that case I would use as to keep it running fast.
Umm, the last part of your sentence does not make any sense, care to elaborate? Casting using "as" will not allow your code to run faster than standard casting mechanisms between types, if standard casting fails it will throw a InvalidCastException . If an "as" cast fails returning null and you access a method or property to your null object you will get a NullReferenceException .
-Nick Parker
|
|
|
|
|
Well I guess I was misunderstanding in that I thought it would just return null instead of a casting exception.
doh!
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
I use as when its possible for some object to not be castable to the type I want.
For example:
private void MyEventHandler(object sender, EventArgs e)
{
ToolBarButton b = sender as Button;
MenuItem mi = sender as MenuItem;
if( b != null )
{
}
else if( mi != null )
{
}
} You would so something like this where the same event handler is used for two different events. In this case standard behavior dictates (at least it used to) that when you click the Print button on the toolbar you don't show the Print dialog box. While clicking the Print command in the menu you should display the Print dialog box.
Unfortunately my example has one slight flaw, the way the ToolBar control works makes it hard to use my example. I still chose to use that example because it is something we should be familiar with.
You should use a cast when you don't expect the cast to fail (an exceptional condition calls for an exception to be thrown).
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
Okay so I am now totally confused
Nick said if it cant cast to that type I get a cast exception.
James says if it cant cast it just returns null.
Well i guess I'll just test it myself, but in theory:
C++:
if cast to something thats not castable I get a null value(0), which is sweet
then i just do an if an can ignore the horrible exception delay
but if i cant I dont see what the difference is rather than code taste
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Ista wrote:
Nick said if it cant cast to that type I get a cast exception.
James says if it cant cast it just returns null
I think you are confusing the two methods of casting we are talking about here. If you do something like:
Object o = new Something();
Something s = (Something)o;
and your cast fail, it will throw an InvalidCastException (Assuming Something is derived from Object ). However if we are talking about an "as" casting and you doing something like:
Object o = new Object();
Something s = o as Something();
if(s != null)
{
}
Failing to check if s is not equal to null and trying to access a member of s would throw an NullReferenceException if the cast for s was returned as null.
-Nick Parker
|
|
|
|
|
I think it was the fact that I misunderstand or bypassed some of your answer.
I have a bad habit of checking for null after I cast from the MFC days.
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
If the successful typecast is crucial in performing the function, go ahead and use var = (type) object; . If, however, there are alternate ways of performing the function, the as keyword is often better; even if you throw an exception anyway, because catching and rethrowing an exception is very expensive.
void Function()
{
try
{
IReferenceService refSvc = (IReferenceService) GetService(typeof(IReferenceService));
}
catch (Exception exc)
{
throw new UsefulException(message, exc);
}
}
void Function()
{
IReferenceService refSvc = GetService(typeof(IReferenceService)) as IReferenceService;
if (refSvc == null)
{
throw new UsefulException(message);
}
}
|
|
|
|
|
Ista wrote:
But then again exceptions in .NET are very costly and therefore as would work better.
How costly are exceptions in .NET and why? I can see how excessively coding around exceptions would not be smart... but at least in ordinary C++, exceptions aren't really expensive, as far as I know.
|
|
|
|
|
http://msdn.microsoft.com/vcsharp/using/understanding/perf/default.aspx?pull=/library/en-us/dndotnet/html/dotnetperftips.asp
this is a link to one article. I spent 2 weeks going from link to link reading about how the CLR reacts and timing. This is a must read:
http://msdn.microsoft.com/vcsharp/using/understanding/perf/default.aspx?pull=/library/en-us/dndotnet/html/fastmanagedcode.asp
nick
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
|
so actually if do an
IS
followed by an
AS
would be much faster if your not completely sure of the object
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Hi,
I developed a .net control in XP with .NET framework 1.0.
It works fine when used in XP or Windows 2000, but when I use the application in NT 4.0 service pack 6 with .NET 1.0, it simply fails to load ... i get the message "Unable to load file ‘MyControl.SplitButton’"
MyControl.SplitButton is my assembly - MyControl.SplitButton.dll.
Leave alone not working, NT simply fails to even load the asembly (as per the above message).
Do you think I will need to rebuild the assembly in NT 4.0
Any thoughts please....
|
|
|
|
|
Check this link out
http://www.dotnet247.com/247reference/msgs/30/150431.aspx
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
I have built simple calculation console applications on VS C#.NET and i can not get the applications to run on another system.
The other system is XP Pro with .NET 1.1 installed.
The application error I get when running the app is: The application failed to initialize properly (0xc0000135.) Click on OK to terminate the application.
Can anyone tell me or provide me with a link to help me get past this hurdle?
|
|
|
|
|
My first suggestion would be to stick the VS.NET CD into the system it doesn't work on and install the remote debugging tools. Set up a connection between your development box and the remote box.
Then you might be able to work out what went wrong
|
|
|
|
|
In the IBindingList example on Microsoft they use class properties in a struct.
I have been using provate variables.
What is the proper design?
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
|
so is this for beginners or programmers who are advanced and just want to polsih off best pratices.
Looks to me like its a beginners book
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
I haven't read the book, class design could be categorized as both beginners and advanced, knowing the proper way to design a specific class requires a good deal of understanding all of which can start when you are a beginner.
-Nick Parker
|
|
|
|
|
Well yeah but I get turned off by books that start out with
"Whats is polymorphism" Its like duh, I've been programming c++ and Java for 5+ years and I'm really more for design patterns than an introduction to programming
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Ista wrote:
"Whats is polymorphism" Its like duh, I've been programming c++ and Java for 5+ years and I'm really more for design patterns than an introduction to programming
Well, maybe you should check out Design Patterns[^].
-Nick Parker
|
|
|
|
|
well alrighty then. It looks like a must buy
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Don't see a form property that will allow me to leave a form open while accessing the parent form... how is this functionality achieved...???
thanks,
vince
|
|
|
|
|
Form1 fm = new Form1();
fm.Show();
modal is
fm.ShowDialog();
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|