Question Class and Array Error

cortes

Member
Joined
May 24, 2010
Messages
5
Programming Experience
3-5
Hello. Can you help me please

Public Class qqq
Public xxx as string
End Class

'This block form1
Public Class main

Public aaa() as qqq

Public Sub www()
redim aaa(3)
Dim bbb as qqq = new qqq
bbb=qqq(1)
ccc=bbb.xxx 'Error Line
end sub

end class

Thanks.
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
Welcome to the forum :D

When you post code please use the code tags it makes it easier for people to read.

What error are you receiving? You're asking us how to fix an error but you haven't specified what the error you received was.

Going by what I can see, I would assume you're getting an error because ccc isn't declared. So you would need to have
VB.NET:
Dim ccc As String
somewhere. Also on the line that says
VB.NET:
bbb=qqq(1)
surely you want it to say
VB.NET:
bbb=aaa(1)
as aaa is the array.

You also aren't putting anything into the aaa array so you wont be able to get anything back from it.
 

cortes

Member
Joined
May 24, 2010
Messages
5
Programming Experience
3-5
thans for your reply but I was misspelled, the previous code.

Public Class qqq
Public xxx as string
End Class

'This block form1
Public Class main

Public aaa() as qqq

Public Sub www()

redim aaa(3)

Dim bbb as qqq = new qqq

bbb=aaa(1)
ccc=bbb.xxx 'Error Line
end sub

end class
 

cortes

Member
Joined
May 24, 2010
Messages
5
Programming Experience
3-5
I found the problem.

ccc=bbb.xxx 'error line

because bbb.xxx is null.

bbb.xxx = "abc"
aaa(1) = bbb
bbb = aaa(1)
If IsDBNull(bbb.xxx) = False Then 'No Error

MsgBox(bbb.xxx)
End If
----------------------


'bbb.xxx = "abc"
'aaa(1) = bbb
bbb = aaa(1)
If IsDBNull(bbb.xxx) = False Then 'Error Line

MsgBox(bbb.xxx)
End If

I don't understand . pfff :S I'm about to go crazy...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,728
Location
Sydney, Australia
Programming Experience
10+
Just because you make an egg carton doesn't mean it contains any eggs. Creating an array doesn't mean it contains anything. Each element is Nothing by default. If you don't put anything into the array in the first place then you can't get anything out of it.

Also, your IsDBNull calls are useless because DBNull and Nothing are two completely different things. If you want to test whether something is Nothing then that's exactly what you have to do.

Finally, all your meaningless identifiers make it very hard to follow what your code is supposed to be doing. Even throwaway code can have meaningful identifiers that make it more readable, e.g.
VB.NET:
Public Class SomeClass
    Public SomeString As String
End Class
VB.NET:
Dim myArray As SomeClass()

ReDim myArray(3)

myArray(0) = New SomeClass
myArray(0).SomeString = "Hello World"
 

cortes

Member
Joined
May 24, 2010
Messages
5
Programming Experience
3-5
thank you @jmcilhinney
but now I have a problem like this.

Public Class SomeClass
Public SomeString() As qqq
End Class

Public Clas qqq
Public nnn as string
End Class

ReDim SomeClass(3) 'No Error

ReDim SomeClass(2).SomeString(4) 'Error Line


I want to change SomeString bound
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,728
Location
Sydney, Australia
Programming Experience
10+
Apparently my advice about meaningless identifiers didn't get through.

As for the issue, your code doesn't make sense. I think you need to provide a FULL and CLEAR description of what you're actually trying to achieve, along with a decipherable code example if appropriate. In the mean time, I would suggest some reading on arrays.

Arrays in VB .NET
 
Top Bottom