Creating a custom class/Namespace

R

ryan.d.rembaum

Hello,

I have code that I wish to use in many web applications. Basically
sort of stand utility stuff. So from Visual Studio Project I select
add a component and chose Component Class. Lets say I enter code at
the end of this question in to the code section.

How then would I reference this in a new Web Application (or in the
same web application for that matter?)

I have tried:
<%@ Import Namespace="ByteArray.ByteArray" %>
and
<%@ Import Namespace="ByteArray" %>

But when I run the page it just gives me a message that the Namespace
can't be found.

Thanks,
Ryan

------------CODE FOLLOWS-----------------------------

Imports System.Globalization
Namespace ByteArray
Public Class ByteArray
Inherits System.ComponentModel.Component
|Component Designer generated code...|

Public Function CreateTypeByte(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Convert.ToByte(arrChar(i))
Next
Return arrByte
End Function

Public Function CreateTypeHex(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrString As String()

arrString = strInput.Split(New Char() {"-"})
Dim arrByte(arrString.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Byte.Parse(arrString(i),
NumberStyles.HexNumber)
Next
Return arrByte
End Function
End Class
End Namespace
 
R

ryan.d.rembaum

I built and rebuilt the project and solution and get no skips and no
errors. Just a report of success...But then when I test my ASP page in
my browser, I get the message in the compiler output that my namespace
could not be found.

Any thoughts?

Ryan
 
J

Juan T. Llibre

re:
To reference that code in a new web Application, you'd have
to compile the code from the command-line, and reference
*that* assembly in your VS.NET project.

To compile a single source code file, use :

csc /t:library /out:utils.dll utils.cs

To compile all .cs files in a directory to utils.dll ,
use : csc /t:library /out:utils.dll *.cs

If you need to reference a .Net Framework assembly or assemblies,
add them, separated by slashes :

csc /t:library /r:system.dll /r:system.data.dll /out:utils.dll *.cs

If you're using VB.NET instead of C#,
use vbc and source files ending in .vb
instead of using csc and source files ending in .cs.

The syntax is the same for vb and c#.

Once you have compiled your assembly, place it in the /bin
directory of your application, and import the Namespace :

<%@ Import Namespace="ByteArray" %>

If you're working strictly with VS.NET, compile your assembly
as described and add a reference to it in your VS.NET project.

That will allow you to use Intellisense and get your
assembly's properties, methods, etc. when coding.






I built and rebuilt the project and solution and get no skips and no
errors. Just a report of success...But then when I test my ASP page in
my browser, I get the message in the compiler output that my namespace
could not be found.

Any thoughts?

Ryan
 
R

ryan.d.rembaum

Hi Juan,

Thanks for your help. I have tried what you suggested and am getting
some errors. Here, first, are the contents of my .vb file:
---------------------------------------------
Imports System.Globalization
Namespace ByteArray
Public Class ByteArray
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "

Public Sub New(ByVal Container As
System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer
support
Container.Add(Me)
End Sub

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent()
call

End Sub

'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component
Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region
Public Function CreateTypeByte(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Convert.ToByte(arrChar(i))
Next
Return arrByte
End Function

Public Function CreateTypeHex(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrString As String()

arrString = strInput.Split(New Char() {"-"})
Dim arrByte(arrString.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Byte.Parse(arrString(i),
NumberStyles.HexNumber)
Next
Return arrByte
End Function
End Class
End Namespace
---------------------------------------------

I then compile it using:

c:\WINNT\Microsoft.NET\Framework\v1.1.4322\csc.exe /target:library
/out:ByteArray.dll /r:System.dll
C:\Inetpub\wwwroot\testApp\ByteArray.vb

I get a bunch of errors of four basic types:

-Preprocessor directive expected.
-Single Line Comment or End-of-Line expected.
-Newline in constant.
-A Namespace does not directly contain members such as fields or
methods.

Do you know why I am getting them. If I take out the system generated
code, the errors decrease, but I thought that the system generated code
was supposed to make my class more usuable. (Plus, I still get some of
the errors even without the system generated code.)

Thanks,
Ryan
 
J

Juan T. Llibre

It looks like you're trying to use a Windows Forms class in ASP.NET.
'Required for Windows.Forms Class Composition Designer support






Hi Juan,

Thanks for your help. I have tried what you suggested and am getting
some errors. Here, first, are the contents of my .vb file:
---------------------------------------------
Imports System.Globalization
Namespace ByteArray
Public Class ByteArray
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "

Public Sub New(ByVal Container As
System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer
support
Container.Add(Me)
End Sub

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent()
call

End Sub

'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component
Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region
Public Function CreateTypeByte(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrChar As Char()

arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Convert.ToByte(arrChar(i))
Next
Return arrByte
End Function

Public Function CreateTypeHex(ByVal strInput As String) As
Byte()
Dim i As Integer
Dim arrString As String()

arrString = strInput.Split(New Char() {"-"})
Dim arrByte(arrString.Length - 1) As Byte
For i = 0 To arrByte.Length - 1
arrByte(i) = Byte.Parse(arrString(i),
NumberStyles.HexNumber)
Next
Return arrByte
End Function
End Class
End Namespace
---------------------------------------------

I then compile it using:

c:\WINNT\Microsoft.NET\Framework\v1.1.4322\csc.exe /target:library
/out:ByteArray.dll /r:System.dll
C:\Inetpub\wwwroot\testApp\ByteArray.vb

I get a bunch of errors of four basic types:

-Preprocessor directive expected.
-Single Line Comment or End-of-Line expected.
-Newline in constant.
-A Namespace does not directly contain members such as fields or
methods.

Do you know why I am getting them. If I take out the system generated
code, the errors decrease, but I thought that the system generated code
was supposed to make my class more usuable. (Plus, I still get some of
the errors even without the system generated code.)

Thanks,
Ryan
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top