473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Referencing and passing <this>

I've written a function that is intended to modify some of the properties of
whatever form is to it. The function, declared within frmTasks, looks like
this
public void FormActivated(Form sender){...}

and the call looks like this
frmTasks.FormActivated(this);

The error message is:
An object reference is required for the nonstatic field, method, or
property

I've tried including 'ref' in both the function definition and the call to
it, but that results in this:
Argument '1': cannot convert from 'ref FollowUpApp.frmCategories' to 'ref
System.Windows.Forms.Form'
Cannot pass '<this>' as a ref or out argument because it is read-only

So, how does one go about passing a form as an argument to a function that
resides outside the form's cs? BTW, I want to do this because I need to run
the same code on several forms within this application.
Thanks.
Nov 17 '05 #1
3 1368
Christopher,
public void FormActivated(Form sender){...}

and the call looks like this
frmTasks.FormActivated(this);

The error message is:
An object reference is required for the nonstatic field, method, or
property

Sounds like "frmTask" is the name of your form class. You need an
instance of the form to call the FormActivated method.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Christopher Weaver <we*****@verizon.net> wrote:
I've written a function that is intended to modify some of the properties of
whatever form is to it. The function, declared within frmTasks, looks like
this
public void FormActivated(Form sender){...}

and the call looks like this
frmTasks.FormActivated(this);

The error message is:
An object reference is required for the nonstatic field, method, or
property

I've tried including 'ref' in both the function definition and the call to
it, but that results in this:
Argument '1': cannot convert from 'ref FollowUpApp.frmCategories' to 'ref
System.Windows.Forms.Form'
Cannot pass '<this>' as a ref or out argument because it is read-only

So, how does one go about passing a form as an argument to a function that
resides outside the form's cs? BTW, I want to do this because I need to run
the same code on several forms within this application.


The problem isn't that you're failing to pass the parameter properly.
It's that FormActivated is an instance method, and you're calling it as
if it were a static method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
I get it now. So how do I find or create an instance name that I can use?
I tried this:

frmTasks TaskForm = new frmTasks();
Application.Run(TaskForm);

It compiles and runs just fine, but I still get the following on the line of
code that attempts to call the function [TaskForm.FormActivated(ref this);]
:

The type or namespace name 'TaskForm' could not be found
Cannot pass '<this>' as a ref or out argument because it is read-only

I've looked through the help text for the basics of how to reference another
module in C# and how to pass by reference but I can't find the definitive
information that I need. Any help at all will be greatly appreciated.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
Christopher,
public void FormActivated(Form sender){...}

and the call looks like this
frmTasks.FormActivated(this);

The error message is:
An object reference is required for the nonstatic field, method, or
property

Sounds like "frmTask" is the name of your form class. You need an
instance of the form to call the FormActivated method.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nachi | last post by:
Hi, Urgent Help appreciated.... I am getting resultset with first condition and when try to get the resutlset from second condition i am getting the above error in SQL200. I know that i am...
2
by: Francesco Moi | last post by:
Hello. I designed a form to edit some DataBase's fields. But some of these fields contain '&lt;' and '&gt;' characters. And these characters are '<' and '>' in HTML. So if want to edit these...
3
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test...
4
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML characters are changed to HTML code, i.e. &gt;strong&lt;...
9
by: Michael A. Covington | last post by:
C# won't let me do something elegant. I'm creating a class and want to give it "write to file" and "read from file" methods. I'm actually using the XML serializer, but that's not the problem. ...
4
by: lizhuo | last post by:
hi all: I reading "C++ Templates: The Complete Guide " Part II: Templates in Depth he write: template<typename T> class B { public: enumE{e1=6,e2=28,e3=496}; virtual void zero(E e = e1);
5
by: J.M. | last post by:
I am trying to use a software package in my program, the problem being different methods are used to store vectors. I have "my" vectors stored as vector<doublefrom the STL and need to pass a...
0
by: Gregory Gadow | last post by:
We have a number of development machines in our IT department, all running the same version of VS 2005 sp 1. Our company website and several compiled components were all written in VB.Net 2.0 using...
4
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.