5 Replies - 864 Views - Last Post: 26 February 2012 - 07:25 AM Rate Topic: -----

#1 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Type not defined error when creating a class

Posted 26 February 2012 - 02:05 AM

Hey guys, while I was searching for answers to questions (regarding Images) here on DIC, I came across various solutions on the net. So I thought of collecting all the good ones and make it as a Class so that I can use them on other programs if I ever come across a similar situation.

I found the following solution on the net and I tweaked it a bit to use it as functions. First I put it inside a Module and it works just fine. However when I created a Class and put it inside it, it showed up some errors. (they are in the code's comments)

Public Class MyImageClass

    'function to merge 2 images into one
    Public Function Merge(ByVal img1 As Image, ByVal img2 As Image) As Image    'Type 'Image' is not defined

        Dim bmp As New Bitmap(Math.Max(img1.Width, img2.Width), img1.Height + img2.Height)  'Type 'Bitmap' is not defined
        Dim g As Graphics = Graphics.FromImage(bmp) 'Type 'Graphics' is not defined

        g.DrawImage(img1, 0, 0, img1.Width, img1.Height)
        g.DrawImage(img2, 0, img1.Height, img2.Width, img2.Width)
        g.Dispose()

        Return bmp

    End Function
	
End Class


I tried importing the System.Drawing namespace to this class which didn't change anything either. I've never really touched the OOP side of VB.NET before, this is my first attempt creating a class.

Can anyone please tell me what I'm missing here?

Is This A Good Question/Topic? 0
  • +

Replies To: Type not defined error when creating a class

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 924
  • View blog
  • Posts: 926
  • Joined: 30-September 10

Re: Type not defined error when creating a class

Posted 26 February 2012 - 03:38 AM

Hi,

It sounds like you need to add a reference to the System.Drawing.dll assembly:

1) Go to solution explorer in Visual Studio
2) Right click on 'My Project' and click 'Open'
3) On the screen that is subsequently displayed, navigate to the 'References' section using the menu on the left
4) Click 'Add', go to the '.NET' tab, and navigate to 'System.Drawing' and double click on it.

That will add a reference to the assembly to you project. Note that those instruction are for Visual Studio Ultimate 2010, different versions may be slightly different, but should be largely similar.

You then just need to import the System.Drawing namespace from that assembly using Imports System.Drawing

EDIT: Although, the fact it works it a module but not a class seems a bit odd...

This post has been edited by CodingSup3rnatur@l-360: 26 February 2012 - 03:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Type not defined error when creating a class

Posted 26 February 2012 - 03:48 AM

sweet! Did that and it works! Thanks, man :)
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Type not defined error when creating a class

Posted 26 February 2012 - 06:40 AM

erm...small question again. I build the Class and got the .dll. And I made a reference to that .dll from another program.

The functions defined in the Class appear in the program but when I try to use them, it shows the error Reference to a non-shared member requires an object reference.

What could be the cause for this error?

The code of my Class is as posted in my first post above. And here's the code from the program.

Imports nK0deImageClass.MyImageClass

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim image1, image2 As Image

        image1 = Image.FromFile("C:\Users\nK0de\Downloads\determined.jpg")
        image2 = Image.FromFile("C:\Users\nK0de\Pictures\RedHair.png")

        PictureBox1.Image = Merge(image1, image2)
	''Reference to a non-shared member requires an object reference' occurs under the Function Merge

    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 966
  • View blog
  • Posts: 3,721
  • Joined: 02-July 08

Re: Type not defined error when creating a class

Posted 26 February 2012 - 06:56 AM

Either make an instance of that class and then call the function(<instance>.Merge) or rebuild the dll with the Shared keyword for the function(Public Shared Function).
Was This Post Helpful? 1
  • +
  • -

#6 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Type not defined error when creating a class

Posted 26 February 2012 - 07:25 AM

tried them and both works. I added the Shared keyword. Easier that way. Thanks a lot. :)

This post has been edited by nK0de: 26 February 2012 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1