Accessing control properties...

I just thought I'd mess around with C# and see what the hell it has to offer and - I feel like a complete newbie!

I don't know whether it is my VS2005 playing around, but I cannot seem to assign any values to control properties.

For example, I get the error on this line:

myLiteral.Text = str.ToString;   //str is a stringbuilder....

The error i get is:

Error      1      An object reference is required for the nonstatic field, method, or property '_Default.myLiteral'      c:\inetpub\wwwroot\c_stream\streaming.aspx.cs      48      9      http://localhost/c_stream/

What is going on!


LVL 11
Ramesh SrinivasTechnical ConsultantAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

shy_talkConnect With a Mentor Commented:
Still not altogether sure how far you've got with this, and I'm no ASP guru, but it sounds like you are trying to use "this" and your other control references in a static method, which of course can be called when no object instance has yet been created. , e.g. MyClass.InstanceCount can be zero; or there may be more than one instance to which "this" could refer, in which case the reference would be ambiguous.

"this" is how an instance refers to itself. There is, to my knowledge no explicit at the class level meaning the class itself.

Sorry if you already know all this (!). Good luck!

Not sure why it would work OK in VB, though.
At first glance, I'd say you need to call ToString with brackets, as it is a method, not a property.
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Yes, that is prob true, but even with brackets I get the same error :(

I cannot seem to reference any controls like this. Works fine when I create a VB.Net project.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Ramesh SrinivasTechnical ConsultantAuthor Commented:
When I installed visual studio 2005, I did set the default language option to VB.NET.

Now that I have created a C# project, maybe it has bugged because of some operations occuring during installation??!

Ramesh SrinivasTechnical ConsultantAuthor Commented:
When I type Literal1.    intellisense does nto show any properties e.g., Text, ID etc etc...

It is doing this for any controls i place on the page.  If I use the word this. then intellisense shows me the name of the control i placed on the page and also its properties. But when I try to compile I get an error:

Error      1      Keyword 'this' is not valid in a static property, static method, or static field initializer      c:\inetpub\wwwroot\c_stream\streaming.aspx.cs      48      9      http://localhost/c_stream/

Damn it.
Ramesh SrinivasTechnical ConsultantAuthor Commented:
I think it was because I was referencing the control from a static method as you mentioned.

A bit confusing seen as tho in vb you can reference a control from a sub function etc etc.

Not at all - shy
All Courses

From novice to tech pro — start learning today.