Getting Started on a Dll?

Started by G_G, October 17, 2009, 11:29:03 pm

Previous topic - Next topic

G_G

I've really no idea where to start. I want to make my own dll to make some programming easier for me and a friend who's also getting into the c# scene.
I created a class library in a new project which is the dll.

So like maybe an example of a dll would help is all.

Thanks in advance!

Blizzard

October 18, 2009, 04:38:20 am #1 Last Edit: October 18, 2009, 04:40:02 am by Blizzard
Well, it depends on what the dll is for. i.e. You can make a dll that provides the functionality of a binary tree (i.e. class BinaryTree). Then using that dll will allow you to have the class BinaryTree in any project you want. So basically you implement the class BinaryTree in the class library project while you use another project to test it properly (yes, 2 projects in the solution). In this case in the testing project you need to add a reference to the dll project. Just right click on the References folder in your Solution Explorer, choose "Add Reference", the open the tab Projects and select your class library.

There's just one problem. Creating a dll that is really well portable into any project isn't simple. You have to create a good interface or else there's not really a point in making a dll.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Well like I know how to add dll's to my projects and use methods from them because I have a Zip extracting dll I found on google.

I just wanted to code one to see how it turned out but I tried and when I added the dll it was as if the method wasnt actually in it. So thats why I need help. I tried google havent found anything helpful yet then again I fail when it comes to google.

Anyways I'm gonna look into it some more and see what happens.

Blizzard

Then try making a dll for a list. It's not complicated.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Woo hoo! K well I fixed what I messed up, it wasnt reading the method because I placed it as private instead of public.

anyways my dll now reads from a text file!
String text = GGDLL.Read(file);


It works :)

Lol anyways I'm gonna play around and see what else I can add.

Blizzard

Don't forget to set the publicly available classes as public. If you don't set them explicitly as public, they won't be visible outside the dll.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Okay need some more help. Here's my code so far. This is just a test and for some reason its not working.
namespace GGLib
{
    public class Lib
    {
        public void Test(string test)
        {
            StreamWriter sr = new StreamWriter(@"C:\test.txt");
            sr.Write(test);
            sr.Close();
        }
    }
}

Now I created a whole new project, added the dll and the method does not show up. Did I not do something right? I lost my other dll's source.

Ryex

so you added a reference to the dll and a using GGLib line?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

yea here's my code in my totally different project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GGLib;

namespace GGLib_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Lib.Test("This is a test";
        }
    }
}

But the method doesnt exist. The Lib part does but not the .Test

fugibo

I have absolutely no experience with C#, but it looks like you've defined it as an instance method and you're trying to call it from the class.

winkio

should be
 using GGLib.Lib


and fix your parenthesis

Lib.Test("This is a test";



Blizzard

This code:
            Lib instance = new Lib();
            instance.Test("This is a test");

goes with this type of method definition:
            public void Test(...)


This code:
            Lib.Test("This is a test");

goes with this type of method definition:
            public static void Test(...)


And if you don't plan on instantiating the class, you should make it static as well.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

fugibo

Just to clarify for you, this is similar to

def method(argument)
  print argument, "\n"
end

vs.

def self.method(argument)
  print argument, "\n"
end


and

class Lib
  # stuff
end

vs.

module Lib
  # stuff
end


Though obviously the static class is missing the mix-in features of Module.

Blizzard

Yeah, that's pretty much it. Though, just like in Ruby you can bind static methods to non-static classes.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Okay thanks guys it works perfectly now :) *lv's up LF and Blizz*