Help me: How to use the "new" keyword to create an object instanc

  • Thread starter Thread starter sli23
  • Start date Start date
S

sli23

When i run the following C# code in Asp.net page, always got error promt in
VisualStudio:"use the new keyword to create an object instance"

using Microsoft.CommerceServer.Runtime;
...
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();
......
 
Mark Rae said:
When i run the following C# code in Asp.net page, always got error promt
in
VisualStudio:"use the new keyword to create an object instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();



--
Mark Rae
ASP.NET MVP
http://www.markrae.net

if add new key words like you said:
ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

Compiler Error
Message:CS0118:"Microsoft.CommerceServer.Runtime.CommerceContext.Current" is
a 'property' but is used like 'type'
 
After Add new , got this Complier Error message:
CS0118:"Microsoft.CommerceServer.Runtime.CommerceContext.Current" is a
'property' but is used like a 'type'

Mark Rae said:
When i run the following C# code in Asp.net page, always got error promt
in
VisualStudio:"use the new keyword to create an object instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();
 
ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

I do not think it will work
The whole statement (array brackets for example) indicates that
GetSelector() is a method and not a constructor.

So it must be like this.
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();


Most likely some of the properties are null and C# throws an error with a
helpful hint to use "new keyword to create an object".
I never worked with Commerce server but my bet would be that
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"]
returns a null. Hence you get that exception when trying to call
GetSelector() on null.

George.


Mark Rae said:
When i run the following C# code in Asp.net page, always got error promt
in
VisualStudio:"use the new keyword to create an object instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();
 
Mark Rae said:
When i run the following C# code in Asp.net page, always got error
promt in VisualStudio:"use the new keyword to create an object
instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

So it must be like this.
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

That's what the OP originally had...

Yes he did. But he was wrong in assuming that he is missing 'new' statement
there.
He is simply were getting a null somewhere in that chain and the whole thing
blew up with a helpful warning to use new. I believe that is what Visual
Studio does when encounters an attempt to apply method (or property ) to
null.
 
Hi George and Mark, Thanks a lot for your help! You gave me accurate
diagnosis and help me solved my issue now. Appreciated for your valued time!



George said:
Mark Rae said:
When i run the following C# code in Asp.net page, always got error
promt in VisualStudio:"use the new keyword to create an object
instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

So it must be like this.
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

That's what the OP originally had...

Yes he did. But he was wrong in assuming that he is missing 'new' statement
there.
He is simply were getting a null somewhere in that chain and the whole thing
blew up with a helpful warning to use new. I believe that is what Visual
Studio does when encounters an attempt to apply method (or property ) to
null.
 
Here is my changed code under Gegorge's light:
Microsoft.CommerceServer.Runtime.Targeting.ContentSelector
adContentSelector=null
if(Microsoft.CommerceServer.Runtime.CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"]!=null);
{
adContentSelector=Microsoft.CommerceServer.Runtime.CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();
}

If the above code is unexpected, could you please let me know.
Cheers,

Steven

George said:
Mark Rae said:
When i run the following C# code in Asp.net page, always got error
promt in VisualStudio:"use the new keyword to create an object
instance"

using Microsoft.CommerceServer.Runtime;
..
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

ContentSelector adContentSelector = new
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

So it must be like this.
ContentSelector adContentSelector =
CommerceContext.Current.TargetingSystem.SelectionContexts["Advertising"].GetSelector();

That's what the OP originally had...

Yes he did. But he was wrong in assuming that he is missing 'new' statement
there.
He is simply were getting a null somewhere in that chain and the whole thing
blew up with a helpful warning to use new. I believe that is what Visual
Studio does when encounters an attempt to apply method (or property ) to
null.
 
Back
Top