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

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();
......
 
S

sli23

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'
 
S

sli23

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();
 
G

George

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();
 
G

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();

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.
 
S

sli23

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.
 
S

sli23

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top