Chaos Project

Game Development => Sea of Code => Topic started by: G_G on October 17, 2009, 11:29:03 pm

Title: Getting Started on a Dll?
Post by: G_G on October 17, 2009, 11:29:03 pm
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!
Title: Re: Getting Started on a Dll?
Post by: Blizzard on October 18, 2009, 04:38:20 am
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.
Title: Re: Getting Started on a Dll?
Post by: G_G on October 18, 2009, 09:45:00 am
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.
Title: Re: Getting Started on a Dll?
Post by: Blizzard on October 18, 2009, 10:27:38 am
Then try making a dll for a list. It's not complicated.
Title: Re: Getting Started on a Dll?
Post by: G_G on October 18, 2009, 02:35:47 pm
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.
Title: Re: Getting Started on a Dll?
Post by: Blizzard on October 18, 2009, 02:48:38 pm
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.
Title: Re: Getting Started on a Dll?
Post by: G_G on November 04, 2009, 11:42:26 pm
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.
Title: Re: Getting Started on a Dll?
Post by: Ryex on November 04, 2009, 11:49:36 pm
so you added a reference to the dll and a using GGLib line?
Title: Re: Getting Started on a Dll?
Post by: G_G on November 04, 2009, 11:51:07 pm
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
Title: Re: Getting Started on a Dll?
Post by: fugibo on November 05, 2009, 12:13:18 am
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.
Title: Re: Getting Started on a Dll?
Post by: winkio on November 05, 2009, 12:42:07 am
should be
 using GGLib.Lib


and fix your parenthesis

Lib.Test("This is a test";


Title: Re: Getting Started on a Dll?
Post by: Blizzard on November 05, 2009, 03:27:18 am
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.
Title: Re: Getting Started on a Dll?
Post by: fugibo on November 05, 2009, 07:20:52 am
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.
Title: Re: Getting Started on a Dll?
Post by: Blizzard on November 05, 2009, 08:11:37 am
Yeah, that's pretty much it. Though, just like in Ruby you can bind static methods to non-static classes.
Title: Re: Getting Started on a Dll?
Post by: G_G on November 05, 2009, 07:39:34 pm
Okay thanks guys it works perfectly now :) *lv's up LF and Blizz*