Link to home
Start Free TrialLog in
Avatar of arthurh88
arthurh88

asked on

How do i create a simple array in a public class?

I have a string  in a public class (class name is City).  The string is called County, which may equal "King County" or "Lincoln County/Copiah County" (for example)....taken from Census data

It will never have more than 2 counties, either one or two.

So I need a simple string.split method.

In my class file I put:
Property County() as String

And in my VB code behind in my aspx page, I am unable to set County(0) = "some text"   and County(1) = "more text",  nor am I able to do a string.split on it.  I get an error

So I tried using a list instead.

In my class file I put:  
 Property County As List(Of String)

And in my vb code I put:  

            City.County.Add("hello")
And i get a null reference error.


I'm not sure how to setup a basic array (that will hold a max of 2 values) in my public class and then set them in my aspx.vb page.  Thanks for any help!
Avatar of HainKurt
HainKurt
Flag of Canada image

if City.County.contains("/") then
  County(0) = City.County.split("/")(0)
  County(1) = City.County.split("/")(1)
else
  County(0) = City.County
  County(1) = ""
end if

Open in new window

Avatar of arthurh88
arthurh88

ASKER

hi Huseyin, that didn't work.  I don't want to set  the variable "county".  I want to set city.county

county(1) is not a declared variable in my vb file.

Trying to directly set city.county(1) doesn't work either, even though in my class file I have
Property County() As String '

basically I just want to
Simply create a class file with an array string county().
Then in my aspx form page i want to set the strings class.county(0)  and class.county(1).

how do i do it?
i tried using
Property County() as array (in my class file)
but even still, I'm unable to set the array in my aspx form vb page:

city.county.add("hello") makes an error.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hah, clever.  yes i like that.  thanks!
Avatar of Fernando Soto
Hi arthurh88;

Please see comments in the code block below.
' This line only creates a variable to hold an Array of String but does not allocate storage space 
' So if you try to store values in it you will get Null Reference Exception
' Dim County() As String

' This will define a two element array with storage allocated
Dim County() As String = New String(1) {"", ""}

' Your code to define a List(Of String) has the same problem as above no space is allocated until you New it up.
' Try this way
Dim County2 As List(Of String) = New List(Of String)

Open in new window