Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ArrayList in VB.NET Class

Posted on 2012-04-12
3
Medium Priority
?
421 Views
Last Modified: 2012-06-27
I am creating my first ever custom class.

Code currently us...

Public Class AQL_SMS
    Public Username As String
    Public Password As String
    Public Destination As ArrayList
    Public Message As String
    Public Originator As String
    Public Max_Concat As String
    Public Sendtime As Date
    Public Unix_Sendtime As Integer
    Public ReplaceSMS As Boolean
    Public Flash As Boolean
    Public Dlr_URL As String

    Public Sub New(ByVal _username As String, ByVal _password As String)
        Username = _username
        Password = _password
    End Sub


    Public ReadOnly Property Destinations As String
        Get
            Dim tmpjoin As String = ""
            Dim str As String
            For Each str In Destination
                tmpjoin = String.Concat(tmpjoin, str.ToString & ",")
            Next
            tmpjoin = tmpjoin.Substring(0, tmpjoin.Length - 1)
            Return tmpjoin
        End Get
    End Property
End Class

Open in new window


This is a class based upon http://www.aql.com/sms/integrated/sms-api/

I have set Destination as an ArrayList as it should be able to contain multiple destination numbers.

I have declared in my progect code as follows...

Public Class frmClassView
    Public SMS As New AQL_SMS("my_username", "my_password")

    Private Sub btbSMS_Click(sender As System.Object, e As System.EventArgs) Handles btbSMS.Click
        SMS.Destination.Add("01234567891")
        SMS.Destination.Add("01234567891")

        tmrAutoSMSPropertyCheck.Start()
    End Sub

    Private Sub tmrAutoSMSPropertyCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrAutoSMSPropertyCheck.Tick
        lblUsername.Text = SMS.Username
        lblPassword.Text = SMS.Password
        lblDestination.Text = SMS.Destinations
        lblMessage.Text = SMS.Message
        lblOriginator.Text = SMS.Originator
        lblMax_Concat.Text = SMS.Max_Concat
        lblSendtime.Text = SMS.Sendtime
        lblUnix_Sendtime.Text = SMS.Unix_Sendtime
        lblReplaceSMS.Text = SMS.ReplaceSMS
        lblFlash.Text = SMS.Flash
        lblDlr_URL.Text = SMS.Dlr_URL
    End Sub
End Class

Open in new window


However I get the error...

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=WindowsApplication1
  StackTrace:
       at WindowsApplication1.frmClassView.btbSMS_Click(Object sender, EventArgs e) in C:\Users\terry\AppData\Local\Temporary Projects\WindowsApplication1\frmClassView.vb:line 5
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

at the

SMS.Destination.Add("01234567891")

Open in new window


line.

Why, and how do I fix this to work as I expect it to?
0
Comment
Question by:Terry Rogers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 37836497
The object is not yet declared.
You can easily solve it in your constructor

Public Sub New(ByVal _username As String, ByVal _password As String)
        Username = _username
        Password = _password
        Destination = new ArrayList()

    End Sub
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37836502
A Couple of things.
1.This line
Public SMS As New AQL_SMS("my_username", "my_password")

Open in new window

should give you an error as it should be
Public SMS As New AQL_SMS With {.Username = "my_username", .Password = "my_password"}

Open in new window

2.  you have to create your ArrayList (or initialise it) then pass it to your initialised class,e.g
SMS.Destination = New ArrayList From {"01234567891", "01234567891"}

Open in new window

or
Dim Dest As New ArrayList
Dest.Add("01234567891")
Dest.Add("01234567891")
SMS.Destination = Dest

Open in new window

0
 
LVL 1

Author Closing Comment

by:Terry Rogers
ID: 37836518
This worked perfectly. Thankyou! :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question