Clearing a custom array

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

Adam Honek

Instead of the ZeroMemory API what would one use in VB.NET to
clean an array using a custom structure?

The .clear isn't a member of it by default.

Any suggestions?

Thanks,
Adam
 
Adam Honek said:
Instead of the ZeroMemory API what would one use in VB.NET to
clean an array using a custom structure?

The .clear isn't a member of it by default.

What exactly is not working?

\\\
Private Structure Test
Public Name As String
Public Number As Integer

Public Sub New( _
ByVal Name As String, _
ByVal Number As Integer _
)
Me.Name = Name
Me.Number = Number
End Sub
End Structure
....
Dim a() As Test = _
{ _
New Test("Hello", 12), New Test("Bla", 2) _
}
Array.Clear(a, 0, a.Length)
///
 
Hmmm,

It's not a VB.NET array.

It's a structure (VB6 Type) that gets put into an array hence:

Structure HelloWorld
dim Message as string
dim date as string
end structure

public LetsTry as HellowWorld()

It will compile as below but fall over when it tries to run that code:

'Before we start filling the email accounts array lets make sure it's nice
and clean
Array.Clear(LetsTry, 0, LetsTry.Length)

I guess it's because I'm not doing the {} array format your example below
shows.

Adam
 
When I try declare it as "New" it simply says arrays cannot be delcared with
the "New" keyword.

Adam
 
Adam Honek said:
When I try declare it as "New" it simply says arrays cannot be delcared
with the "New" keyword.

I suggest to post some code. Are you using VB6 or VB.NET? Note that this
group is dedicated to VB.NET programming.
 
maybe to simple ..... but why do you not assign a Nothing pointer ??? to the
var to clear it`s contents ( should work in VB6 and VB.Net )

:-)

Regards

Michel Posseth [MCP]
 
This seems to work but is it not the same as saying here is a null pointer
to this array?

Adam

Michel Posseth said:
maybe to simple ..... but why do you not assign a Nothing pointer ??? to
the var to clear it`s contents ( should work in VB6 and VB.Net )

:-)

Regards

Michel Posseth [MCP]


Adam Honek said:
Instead of the ZeroMemory API what would one use in VB.NET to
clean an array using a custom structure?

The .clear isn't a member of it by default.

Any suggestions?

Thanks,
Adam
 
This is all VB.NET 2005, no VB6.

Basically:

Public Structure EmailAccount
Dim sDesiredName As String

Dim sPOP3Server As String

Dim sSMTPServer As String

Dim sAccountName As String

Dim sAccountPassword As String

Dim sUserName As String

Dim sEmailAddress As String

End Structure

And then:

Dim a as EmailAccount.

How does one remove an entry from this array?

Adam
 
Yes it is the same as assigning null in C# ( C# Null = VB.Net
Nothing )

It will efectively clear all the elements and is much faster as other
methods

regards

Michel Posseth [MCP]



Adam Honek said:
This seems to work but is it not the same as saying here is a null pointer
to this array?

Adam

Michel Posseth said:
maybe to simple ..... but why do you not assign a Nothing pointer ??? to
the var to clear it`s contents ( should work in VB6 and VB.Net )

:-)

Regards

Michel Posseth [MCP]


Adam Honek said:
Instead of the ZeroMemory API what would one use in VB.NET to
clean an array using a custom structure?

The .clear isn't a member of it by default.

Any suggestions?

Thanks,
Adam
 
Adam Honek said:
Public Structure EmailAccount
Dim sDesiredName As String

Dim sPOP3Server As String

Dim sSMTPServer As String

Dim sAccountName As String

Dim sAccountPassword As String

Dim sUserName As String

Dim sEmailAddress As String

End Structure

And then:

Dim a as EmailAccount.

How does one remove an entry from this array?

The variable 'a' is not an array variable. As said previously, you may want
to use 'System.Collections.ArrayList' or 'System.Collections.Generic.List(Of
EmailAccount)' instead of the array.
 
Michel Posseth said:
Yes it is the same as assigning null in C# ( C# Null = VB.Net
Nothing )

It's true for reference types (arrays for example), but 'Nothing' has a
meaning for value types in VB.NET too.
It will efectively clear all the elements and is much faster as other
methods

Depends on how to define "clearing an array". Based on my understanding the
OP didn't want to remove the items from the array, he wanted to reset them
to their default values.

In addition, I suggest to check out the 'Erase' keyword :-).
 
Herfried,

You mean something as

Dim myString(100) as String
For each myStr as String in MyString
myStr = Nothing
Next

:-))

Cor
 
Cor,

Cor Ligthert said:
You mean something as

Dim myString(100) as String
For each myStr as String in MyString
myStr = Nothing
Next

:-))

Why not just use 'Array.Clear' as shown for this purpose?
 
Cor Ligthert said:
Why not just use 'Array.Clear' as shown for this purpose?

--
I just tried to show the difference and use of "IS" and "=" with the word
Nothing,

I even would not think about using what I was showing, even if there was not
an Array.Clear I would instance a new array at the same address myself. I
never look at ILS, but I just think that this is in fact happening with
Array.Clear.

Cor
 
Back
Top