Author Topic: Something new for me.  (Read 3362 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Something new for me.
« on: November 25, 2015, 12:15:41 PM »
I came across this code tonight and it has me stumped

Code - C#: [Select]
  1.  
  2.             Editor ed = default(Editor);
  3.             ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  4.  
  5.             PromptPointResult ppr1 = default(PromptPointResult);
  6.             ppr1 = ed.GetPoint(new PromptPointOptions("Select start point"));
  7.  
  8.             if (ppr1.Status != PromptStatus.OK) {return;}
  9.  
  10.             PromptPointResult ppr2 = default(PromptPointResult);
  11.             ppr2 = ed.GetPoint(new PromptPointOptions("Select end point"));
  12.             if (ppr2.Status != PromptStatus.OK) {
  13.                 return;
  14.             }
  15.  
The source is
http://adndevblog.typepad.com/autocad/2015/11/mleader-text-that-reflects-mleaderstyle-settings.html

The code compiles and executes after netloading.
My query is regarding the use of the default keyword.

My help file associates it's use with generics where the type is unknown.
https://msdn.microsoft.com/EN-US/library/xwth0h0d(v=VS.140,d=hv.2).aspx

It's very late for me and I'm a little brain dead but I don't understand why the keyword is used.

Can anyone venture an opinion ??

Regards,


added:
I probably should ask Balaji :)
« Last Edit: November 25, 2015, 12:23:56 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Something new for me.
« Reply #1 on: November 25, 2015, 12:26:11 PM »
My thought is that it initializes each variable to its default value.  Used in case you do not know its default value.  In the context that it is being used, I do not belive that it is required.  c# will automatically set the variable to its default value in its constructors.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #2 on: November 25, 2015, 12:32:56 PM »

Hi Keith,
I stepped through in the debugger and the values were null until initialised on the following line ; so it serves no 'obvious/demonstrable' purpose.

My initial thoughts were the same as yours ... but it's been nagging at me .

Thanks


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #3 on: November 25, 2015, 12:44:07 PM »
I posted a comment for Balaji ... perhaps we'll know tomorrow.

for a giggle and with respect

« Last Edit: November 25, 2015, 12:57:37 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Something new for me.
« Reply #4 on: November 25, 2015, 03:43:42 PM »

Given what was said in the help doc's it seems like a reasonable way to avoid using null as an initialiser which can only be a good thing in avoid null ref exceptions. Thanks for bringing it up :)

In the case of the Editor declaration above though, it is just wasted keystrokes and I'm surprised it compiles, perhaps it's optimised away by the compiler given the 'default' hint?
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Something new for me.
« Reply #5 on: November 25, 2015, 04:07:44 PM »

Given what was said in the help doc's it seems like a reasonable way to avoid using null as an initialiser which can only be a good thing in avoid null ref exceptions.

If the variable is not initialized in the constructor, and it is a reference type it will be set to null anyway.  At least that is my understanding of how it works.  A reference type has a default value of null.  Correct?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Something new for me.
« Reply #6 on: November 25, 2015, 04:24:50 PM »
...
A reference type has a default value of null.  Correct?

Right you are Keith, my bad, I didn't grok it on first read through.
I was hoping it was .net heading in a direction to eliminate null (like in F#) although they would most likely have to implement the 'Option' type as an alternative which is much safer to use and check.

The example in the doc's shows one of the very few places it would be useful (inside a generic list), elsewhere there is no real reason to use it given the context you are working with as far as I can see.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #7 on: November 25, 2015, 09:09:23 PM »

A web search found a few usages of  default(PromptPointResult)

Most were by Oleg ( fixo ) during the last 2 1/2 years.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #8 on: November 26, 2015, 02:12:49 AM »
From the Author :
Quote

Balaji has replied to your comment:

Hi Kerry,

I had implemented the code in this blog post initially in VB.Net and later used an online converter to do the C# conversion for the sake of creating this blog post. The converter had introduced this and i did not notice it. Sorry, I do not know what that means. But, the C# version did build and run ok in AutoCAD.

Please do let me know if you have any information on it.

This is the converter that i had used for my VB.Net to C# conversion :
converter.telerik.com

Thanks

Balaji

View the post and comment: http://adndevblog.typepad.com/autocad/2015/11/mleader-text-that-reflects-mleaderstyle-settings.html?cid=6a0167607c2431970b01b8d17b453d970c#comment-6a0167607c2431970b01b8d17b453d970c


added:
In light of this, it's possible that Oleg translated his usage from VB.net with the same translator.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Something new for me.
« Reply #9 on: November 26, 2015, 03:17:01 AM »
The most common usage is in a Generic class, not when you create a new object form a known type.

Code: [Select]
public T Foo<T>() {
 ...
 ...
 ...
 return default(T);
}
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Something new for me.
« Reply #10 on: November 26, 2015, 03:19:14 AM »
Probably converted from VB.NET where you can use

Code: [Select]
  Dim variable As T = Nothing
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #11 on: November 26, 2015, 03:19:41 AM »
The most common usage is in a Generic class, not when you create a new object form a known type.

Code: [Select]
public T Foo<T>() {
 ...
 ...
 ...
 return default(T);
}

Yes.
« Last Edit: November 26, 2015, 03:24:36 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something new for me.
« Reply #12 on: November 26, 2015, 03:23:57 AM »
Probably converted from VB.NET where you can use

Code: [Select]
  Dim variable As T = Nothing

I can't imagine that being the case. I doubt the original author would use generics for something like this ... it is VERY type specific.

Perhaps Balaji will post the VB.net code here if he happens to visit.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.