User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 423,881 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,214 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 2112 | Replies: 1
Reply
Join Date: Jan 2006
Posts: 1
Reputation: Zefram is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zefram Zefram is offline Offline
Newbie Poster

Newbie question:Unable to pass reference to an object

  #1  
Jan 13th, 2006
I have to write new "control" and I just start to hate myself and C# because I do not understand why this piece of trivial code cannot work.

public partial class TrendViewerControl : Control

private System.Timers.Timer myTimer;

public TrendViewerControl()
{
Timer myTimer = new Timer();
myTimer.Interval = TimerDelay;
myTimer.Tick += new EventHandler(TickTack);

myTimer.Enabled = !myTimer.Enabled;

InitializeComponent();
}

private void DrawGrid()
{
myTimer ... HERE COMES THE ERROR, myTimer is NULL
}

I tried the same for any int value, it worked without problem, int variable has been set in constructor and this value could be used in DrawGrid. But if I try the same thing with any object, timer, pen... it just fails. I have got some tutorials but there are exactly same examples of the thing I am trying to do and they work. myTimer is null but the Timer works somehow (I tried it). As if I just cannot have reference to it (what does garbage collector do? by the way). I just try to start with C#, therefore be merciful
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Location: Its the internet... i am everywhere lol
Posts: 274
Reputation: f1 fan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Newbie question:Unable to pass reference to an object

  #2  
Jan 13th, 2006
simple
ALWAYS write code AFTER the InitialiseComponent() call. The control does not exist until that code has completed. It is the code the IDE puts in to create the control and all the child controls on it.

Also, i dont know if it was just a mistake on the post but you should be inheriting from UserControl not Control.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 4:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC