473,378 Members | 1,469 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,378 software developers and data experts.

Instantiation Question Creating Too Many Objects

jm
I have a Form1 and two class files.

In the first class, I call the second class. The second class
references the Form1 notifyicon and changes the icon. It works.

The problems is, however, that because I am doing:

Form1 f = new Form1();

f.notifyIcon.Icon = myIcon;

everytime this second class method is called I get an extra icon in
the system tray. I realize this is because I am creating a new Form1
object with its own set of icons, etc.

My problem is that I do not know how to simply use the same Form1 that
I started out with, especially, since I started out on Form1, went to
class one, and then finally called something out of class two.

I know it is basic, sorry. Thank you for any help.
Nov 16 '05 #1
2 1754
Something like below should work...
Class 1
(
Form1 f = new Form1();
Class2 foo = new Class2();
f.notifyIcon.Icon = foo.YourMethod();

)
Class2
(

public Icon YourMethod()
{

return icon;
}

)
"jm" <jo*************@yahoo.com> wrote in message
news:c6**************************@posting.google.c om...
I have a Form1 and two class files.

In the first class, I call the second class. The second class
references the Form1 notifyicon and changes the icon. It works.

The problems is, however, that because I am doing:

Form1 f = new Form1();

f.notifyIcon.Icon = myIcon;

everytime this second class method is called I get an extra icon in
the system tray. I realize this is because I am creating a new Form1
object with its own set of icons, etc.

My problem is that I do not know how to simply use the same Form1 that
I started out with, especially, since I started out on Form1, went to
class one, and then finally called something out of class two.

I know it is basic, sorry. Thank you for any help.

Nov 16 '05 #2
Scott B (Was New Guy) wrote:
Something like below should work...
Class 1
(
Form1 f = new Form1();
Class2 foo = new Class2();
f.notifyIcon.Icon = foo.YourMethod();

)

[...snip...]

This would work if called from Class1 presuming you are creating the Form1
instance from inside the Class1 instance, but not if any work inside a
Class2 method should cause the notifyIcon to change.

You should pass a reference to the original instance of Form1 to the Class1
instance and on to the Class2 instance, for example with a public property
of Class1 and (!) Class2. This requires the Form1 instance to be known at
Class1 instances creation time:

public class Form1
{
private void Form1Method()
{
// whatever comes before...
Class1 myClass1Instance = new Class1();
myClass1Instance formToManipulate = this;
}
}

public class Class1
{
private Form1 originatingForm;

public Form1 formToManipulate
{
get { return originatingForm; }
set { originatingForm = value; }
}

private void someMethod()
{
Class2 myClass2Instance = new Class2();
myClass2Instance formToManipulateNotifyIcon = this.formToManipulate;
}
}

public class Class2
{
private Form1 formForNotifyIcon;

public Form1 formToManipulateNotifyIcon
{
get { return formForNotifyIcon; }
set { formForNotifyIcon = value; }
}

private void otherMethod()
{
Icon myIcon = this.getNewIconFromSomewhere();
this.formToManipulateNotifyIcon.notifyIcon.Icon = myIcon;
}
}

As an alternative, you can make the Class1 instance known to the Class2
instance and call something like

this.originatingClass1Instance.formToManipulate.no tifyIcon.Icon = myIcon;
Nov 16 '05 #3

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

Similar topics

5
by: Charles A. Lackman | last post by:
I am working on a application that has many custom made dlls. Actually, for this app each dll is a different form. I am able, with no problem to reference and instantiate them to use their public...
7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
7
by: John A Grandy | last post by:
I'm trying to get a decent idea of the relative performance of three types of implementations of data-access classes in ASP.NET 2.0. I believe this boils down to a more basic question regarding...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.