Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Suggestion: New-Object's error message for static classes should explicitly describe correct use

Reply
 
Old 06-22-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Suggestion: New-Object's error message for static classes should explicitly describe correct use

https://connect.microsoft.com/feedba...3108&SiteID=99

DESCRIPTION

If you specify a static class name to New-Object, New-Object is aware that
the real problem is not being able to find a constructor, but the error
message does not point out the issue or its resolution clearly.
I think when New-Object sees that a class you attempt to instantiate is
static, it should clearly say so and provide the solution explicitly,
because it is relatively straightforward to do so.


REPRODUCTION STEPS

Current error message is:
PS> New-Object System.Math
New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Math.
At line:1 char:11
+ New-Object <<<< System.Math
Note that this is already distinct from nonsense class references:
PS> New-Object foo
New-Object : Cannot find type [foo]: make sure the assembly containing this
type is loaded.
At line:1 char:11
+ New-Object <<<< foo


EXPECTED RESULTS

I would like a result similar to the following:
PS> New-Object System.Math
New-Object : System.Math is a static class and cannot be created. In place
of the New-Object statement, use [System.Math].





My System SpecsSystem Spec
Old 06-22-2006   #2 (permalink)
/\\/\\o\\/\\/


 
 

Re: Suggestion: New-Object's error message for static classes shouldexplicitly describe correct use

the real problem is not being able to find a constructor

I think it is :

PoSH>[string].getconstructors() |% {"$_"}
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char[])
Void .ctor(Char, Int32)

PoSH>[math].getconstructors()

I also can not find a way to check if the type is static :

P0SH>[math].gettype() | fl *
P0SH>[string].gettype() | fl *

so it might be the exact problem, it can not find any constructors on a
static type (ofcourse).

Greetings /\/\o\/\/

Alex K. Angelopoulos [MVP] wrote:
> https://connect.microsoft.com/feedba...3108&SiteID=99
>
> DESCRIPTION
>
> If you specify a static class name to New-Object, New-Object is aware that
> the real problem is not being able to find a constructor, but the error
> message does not point out the issue or its resolution clearly.
> I think when New-Object sees that a class you attempt to instantiate is
> static, it should clearly say so and provide the solution explicitly,
> because it is relatively straightforward to do so.
>
>
> REPRODUCTION STEPS
>
> Current error message is:
> PS> New-Object System.Math
> New-Object : Constructor not found. Cannot find an appropriate constructor
> for type System.Math.
> At line:1 char:11
> + New-Object <<<< System.Math
> Note that this is already distinct from nonsense class references:
> PS> New-Object foo
> New-Object : Cannot find type [foo]: make sure the assembly containing this
> type is loaded.
> At line:1 char:11
> + New-Object <<<< foo
>
>
> EXPECTED RESULTS
>
> I would like a result similar to the following:
> PS> New-Object System.Math
> New-Object : System.Math is a static class and cannot be created. In place
> of the New-Object statement, use [System.Math].
>
>
>
>

My System SpecsSystem Spec
Old 06-22-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Suggestion: New-Object's error message for static classes should explicitly describe correct use

"/\/\o\/\/" <no@spam.mow> wrote in message
news:eNqOPgilGHA.2344@TK2MSFTNGP04.phx.gbl...
> the real problem is not being able to find a constructor
>
> I think it is :
>
> PoSH>[string].getconstructors() |% {"$_"}
> Void .ctor(Char*)
> Void .ctor(Char*, Int32, Int32)
> Void .ctor(SByte*)
> Void .ctor(SByte*, Int32, Int32)
> Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
> Void .ctor(Char[], Int32, Int32)
> Void .ctor(Char[])
> Void .ctor(Char, Int32)
>
> PoSH>[math].getconstructors()
>
> I also can not find a way to check if the type is static :
>
> P0SH>[math].gettype() | fl *
> P0SH>[string].gettype() | fl *
>
> so it might be the exact problem, it can not find any constructors on a
> static type (ofcourse).


Starting with .NET 2.0 (specifically C# 2.0) you can declare so called
static classes. In the metadata they are marked as abstract and sealed e.g.:

139 # [math] | %{$_.get_isabstract()}
True
140 # [math] | %{$_.get_issealed()}
True

BTW since [math] represents the runtime type I half expected this to work
but it didn't:

[math]::get_isabstract() # no worky

Mixing up the runtime type versus the static type is a usability liability
IMO. I wonder if it would have been better to add a typeof operator to the
language?

The other thing to check since not all languages may support this notion of
static classes like C# does is to check for a public constructor. If there
isn't one then you can't construct an instance of the type.

--
Keith


My System SpecsSystem Spec
Old 06-23-2006   #4 (permalink)
Jeffrey Snover [MSFT]


 
 

Re: Suggestion: New-Object's error message for static classes should explicitly describe correct use

> BTW since [math] represents the runtime type I half expected this to work
> but it didn't:
>
> [math]::get_isabstract() # no worky


:: is the notion to access a STATIC property/method of a type.
Get_IsAbstract() or IsAbstract is an INSTANCE property of the type
[System.RuntimeType]
so what you want is:

PS> [math] |gm *abst*


TypeName: System.RuntimeType

Name MemberType Definition
---- ---------- ----------
get_IsAbstract Method System.Boolean get_IsAbstract()
IsAbstract Property System.Boolean IsAbstract {get;}


PS> [math].isabstract
True
PS> [math].Get_IsAbstract()
True
--
Jeffrey Snover [MSFT]
Windows PowerShell Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
'Failed to enlist on calling object's transaction' error on Win64 .NET General
error message when trying to correct printer Vista networking & sharing
Static Events Used By Static Classes .NET General
Newer Firefox Version available message not correct Vista installation & setup
Not correct driver message Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46