Newbie Question

T

Todd Carnes

I'm teaching myself C# and am creating my first c# DLL. I can get it to
work, but not the way I want it to work.

In my DLL, I have the following...

namespace Carnesoft.Astro
{
public class Change
{
public static double toJulianDayNumber(double dDay, int iMonth, int
iYear)
{
// some code to do something
}
}
}

I've written some test code in another project to make sure that these
methods work as expected that looks like this...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Carnesoft.Astro;

namespace testJDN
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void btnGo_Click(object sender, EventArgs e)
{
int month, year;
double day, jdn;

month = Convert.ToInt32(tbMonth.Text);
day = Convert.ToDouble(tbDay.Text);
year = Convert.ToInt32(tbYear.Text);

jdn = Change.toJulianDayNumber(day, month, year);
lblResult.Text = Convert.ToString(jdn);
}

private void frmMain_Load(object sender, EventArgs e)
{
Change Change = new Change();
}
}
}

The code works as is, but my question is...

How do I write my DLL so that I don't have to create a Change object in the
frmMain_Load event?

Is this possible, or will I have to instantiate every class that I have in
my DLL before I will be able to use the methods contained within them?

Thank you for any pointers (or URLs) you may give me in advance.

Respectfully,

Todd
 
B

Bob Powell [MVP]

If the method you are calling is static you don't have to create an instance
of the object in order to use it. The Type followed by the method name will
suffice.

You do need to create an instance of the class when you call instance
methods or use instance properties.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
R

Rudy Velthuis

Todd said:
jdn = Change.toJulianDayNumber(day, month, year);
lblResult.Text = Convert.ToString(jdn);
}

private void frmMain_Load(object sender, EventArgs e)
{
Change Change = new Change();
}
}
}

The code works as is, but my question is...

How do I write my DLL so that I don't have to create a Change object
in the frmMain_Load event?

Since the function is static already, there is no need to instantiate
one.

Actually, what you are doing in frmMain_Load() is pure nonsense. You
create an istance of class Change, call it Change as well (bad, bad)
and then leave the function. Since the instance called Change is local
to the function, it is not visible anymore, and is therefore "gone"
(using that word a little loosely, here).

To demonstrate what I mean, you are in fact doing something like:

private void frmMain_Load(object sender, EventArgs e)
{
Change foo = new Change();
}

After frmMain_Load ends, foo is not available anymore. So what you are
doing there has no effect at all. You can remove that code.
--
Rudy Velthuis http://rvelthuis.de

"Of all the enemies to public liberty, war is perhaps the most
to be dreaded because it comprises and develops the germ of
every other." -- James Madison
 
T

Todd Carnes

Rudy Velthuis said:
Since the function is static already, there is no need to instantiate
one.

Actually, what you are doing in frmMain_Load() is pure nonsense. You
create an istance of class Change, call it Change as well (bad, bad)
and then leave the function. Since the instance called Change is local
to the function, it is not visible anymore, and is therefore "gone"
(using that word a little loosely, here).

To demonstrate what I mean, you are in fact doing something like:

private void frmMain_Load(object sender, EventArgs e)
{
Change foo = new Change();
}

After frmMain_Load ends, foo is not available anymore. So what you are
doing there has no effect at all. You can remove that code.


Thank you for that explanation. I shall remove that "nonsense" code and see
what happens. :)

Todd
 
R

Rudy Velthuis

Todd said:
Thank you for that explanation. I shall remove that "nonsense" code
and see what happens. :)

Ok, sorry for my perhaps a little too harsh description. <g>

What I meant is that it makes no sense to do what you did there.
 
T

Todd Carnes

I feel like I just made a fool of myself. <grin>

I removed the class instance in the form load event and everything still
worked fine. :)

Honestly, I could have sworn I tried to do that last night and it wouldn't
work. I guess I should have given up and gone to bed sooner last night.

Thank you for taking the time to respond to my question.

Todd
 
T

Todd Carnes

Bob Powell said:
If the method you are calling is static you don't have to create an
instance of the object in order to use it. The Type followed by the method
name will suffice.

You do need to create an instance of the class when you call instance
methods or use instance properties.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


Thank you for taking the time to respond to my question. :)

Todd
 
P

Peter Duniho

I'm teaching myself C# and am creating my first c# DLL. I can get it to
work, but not the way I want it to work.

A suggestion: use a more specific subject than "Newbie Question". It
should not be necessary to read 90% of a post before one gets at least
some idea as to the actual question.

You seem to have gotten answers to the actual questions already, so I
won't belabor that point. But you should develop better posting skills if
you anticipate using newsgroups as a source of answers to your questions.

Thanks,
Pete
 
T

Todd Carnes

Rudy Velthuis said:
Ok, sorry for my perhaps a little too harsh description. <g>

What I meant is that it makes no sense to do what you did there.
--
Rudy Velthuis http://rvelthuis.de

"Distrust any enterprise that requires new clothes."
-- Henry David Thoreau (1817-1862)


Don't worry about it. No harm done. :)

Actually, you cleared up a misconception I had. I had thought that anything
in the form load event remained in scope for the life of the form.

Todd
 
T

Todd Carnes

Peter Duniho said:
A suggestion: use a more specific subject than "Newbie Question". It
should not be necessary to read 90% of a post before one gets at least
some idea as to the actual question.

You seem to have gotten answers to the actual questions already, so I
won't belabor that point. But you should develop better posting skills if
you anticipate using newsgroups as a source of answers to your questions.

Thanks,
Pete


Ok, I'll remember that if I ask another question here.

Todd
 
B

Bob Powell [MVP]

When you tried it before I bet that you hadn't actually made the method
static.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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

Similar Threads

Question about multiple namespace 11
Usercontrol - Loading into SplitContainer 8
Use of event handling 6
Generic 3
Screen Color 12
a question about binary serialization 3
Opening & Closing Forms 9
WaveInOpen 3

Top