Home All Groups Group Topic Archive Search About

Clearing a custom array

Author
6 May 2006 10:51 PM
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

Author
7 May 2006 12:07 AM
Herfried K. Wagner [MVP]
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> 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)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 12:21 AM
Adam Honek
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:utn51oWcGHA.3484@TK2MSFTNGP03.phx.gbl...
> "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
>> 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)
> ///
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 5:40 AM
Adam Honek
When I try declare it as "New" it simply says arrays cannot be delcared with
the "New" keyword.

Adam

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:utn51oWcGHA.3484@TK2MSFTNGP03.phx.gbl...
> "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
>> 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)
> ///
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 10:28 AM
Herfried K. Wagner [MVP]
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 4:37 PM
Adam Honek
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


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OaIeNEccGHA.380@TK2MSFTNGP04.phx.gbl...
> "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
>> 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.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 6:45 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 May 2006 2:09 PM
Michel Posseth [MCP]
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]


Show quoteHide quote
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schreef in bericht
news:emtbd%23VcGHA.3856@TK2MSFTNGP03.phx.gbl...
> 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
>
Author
7 May 2006 4:36 PM
Adam Honek
This seems to work but is it not the same as saying here is a null pointer
to this array?

Adam

Show quoteHide quote
"Michel Posseth [MCP]" <mic***@posseth.com> wrote in message
news:uXn7oAecGHA.4932@TK2MSFTNGP03.phx.gbl...
>
> 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" <AdamHo***@Webmaster2001.freeserve.co.uk> schreef in bericht
> news:emtbd%23VcGHA.3856@TK2MSFTNGP03.phx.gbl...
>> 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
>>
>
>
Author
7 May 2006 5:02 PM
Michel Posseth [MCP]
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]



Show quoteHide quote
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schreef in bericht
news:%23G1FZRfcGHA.2456@TK2MSFTNGP04.phx.gbl...
> This seems to work but is it not the same as saying here is a null pointer
> to this array?
>
> Adam
>
> "Michel Posseth [MCP]" <mic***@posseth.com> wrote in message
> news:uXn7oAecGHA.4932@TK2MSFTNGP03.phx.gbl...
>>
>> 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" <AdamHo***@Webmaster2001.freeserve.co.uk> schreef in bericht
>> news:emtbd%23VcGHA.3856@TK2MSFTNGP03.phx.gbl...
>>> 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
>>>
>>
>>
>
>
Author
7 May 2006 6:48 PM
Herfried K. Wagner [MVP]
"Michel Posseth [MCP]" <mic***@posseth.com> schrieb:
> 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 :-).

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 May 2006 5:43 AM
Cor Ligthert [MVP]
Herfried,

You mean something as

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

:-))

Cor

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:u8xEHbgcGHA.4892@TK2MSFTNGP02.phx.gbl...
> "Michel Posseth [MCP]" <mic***@posseth.com> schrieb:
>> 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 :-).
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
8 May 2006 9:31 AM
Herfried K. Wagner [MVP]
Cor,

"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> 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?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 May 2006 10:00 AM
Cor Ligthert [MVP]
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
>> 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?
>
> --
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