DirectCast invalid

  • Thread starter =?ISO-8859-1?Q?Bernard_Bour=E9e?=
  • Start date
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Hello

I have the following code:

Public Groupes As New Hashtable

Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = DirectCast(Entry.Value, Groupe)
===> THIS LINE GIVE ME AN ERROR DIRECTCAST IS NOT VALID
Next

Can someone tell me what I'm doing wrong ?

Thanks

Bernard

PS: Here is my class Groupe

Friend Class Groupe

Private mNom As String
Private mNum As Short
Private mCat As Quotes.CatABC

' Stockage pour le numéro ID de débogage.
Private mlDebugID As Integer


Public Property Nom() As String
Get
Nom = mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property


Public Property Num() As Short
Get
Num = mNum
End Get
Set(ByVal Value As Short)
mNum = Value
End Set
End Property


Public Property Cat() As Quotes.CatABC
Get
Cat = mCat
End Get
Set(ByVal Value As Quotes.CatABC)
mCat = Value
End Set
End Property

Public Sub New()
MyBase.New()
End Sub


Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
K

Ken Tucker [MVP]

Hi,

Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = CType(Entry.Value, Groupe)
Next

Ken
-----------------
Hello

I have the following code:

Public Groupes As New Hashtable

Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = DirectCast(Entry.Value, Groupe)
===> THIS LINE GIVE ME AN ERROR DIRECTCAST IS NOT VALID
Next

Can someone tell me what I'm doing wrong ?

Thanks

Bernard

PS: Here is my class Groupe

Friend Class Groupe

Private mNom As String
Private mNum As Short
Private mCat As Quotes.CatABC

' Stockage pour le numéro ID de débogage.
Private mlDebugID As Integer


Public Property Nom() As String
Get
Nom = mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property


Public Property Num() As Short
Get
Num = mNum
End Get
Set(ByVal Value As Short)
mNum = Value
End Set
End Property


Public Property Cat() As Quotes.CatABC
Get
Cat = mCat
End Get
Set(ByVal Value As Quotes.CatABC)
mCat = Value
End Set
End Property

Public Sub New()
MyBase.New()
End Sub


Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
C

Chris Dunaway

Did you specify the full namespace to the class in the DirectCast line?

e.g. Dim Gr As Groupe = DirectCast(Entry.Value, MyNamespace.Groupe)
 
C

Cor Ligthert

Bernard,

Why do you want to cast this? Your procedure, when it is complete, there is
nothing done, however even when the hashtable would have data, the cast is
in my opinion not needed and there will be still nothing done.

You have an empty hashtable from which you try to set the default value from
an in the procedure instanced object groupe, to its value. This instanced
object will go directly out of scope when the next is reached. However, you
don't have a default value.

Therefore the problem is not the cast however what you are doing.

First try to think what you want to achieve and if that does not succeed,
than we will when we can help you with telling how to achieve that.

Although it is maybe not the answer you expected, do I hope this helps.

Cor
 
B

Bernard Bourée

Hello

Thanks for your replies.

In fact the code is an extraction of the original and is not complete.
Here is how Groupes is completed:

Groupes.Add(0, "AMS")
Groupes.Add(1, "BRU")
Groupes.Add(2, "LIS")
Groupes.Add(3, "PAR")

I have used this DirectCAst function due to an answer I received from
this list.
But I didn't realy understood why I should convert a Groupe to Groupe
whith this DirectCast or even with CType ??

THanks for your help

Bernard
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Hello Cor

I haven't solved my problem yet.

Do you need more information or more code ?

Thanks for your help

Bernard


Bernard Bourée a écrit :
 
C

Cor Ligthert

Bernard,

What is the goal you want to achieve,

Now we see some code with a hastable and a classe, however what is the
connection between those two.

Cor
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Cor,
here is my goal

I have a class called Groupe that have two properties Nom and Num
(see code at the end)

Then I want to handle a collection of Groupe
So I have declared it as

Public Groupes As New Hashtable

and fulfiiled with

Dim Gr As New Groupe
Gr.Nom = "AMS3" : Gr.Num = 0
Groupes.Add(0, Gr)
Gr = New Groupe
Gr.Nom = "BRU" : Gr.Num = 1
Groupes.Add(1, Gr)
Gr = New Groupe
Gr.Nom = "LIS" : Gr.Num = 2
Groupes.Add(2, Gr)
Gr = New Groupe
Gr.Nom = "PAR" : Gr.Num = 3
Groupes.Add(3, "PAR")

Then I have the following code:
Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = DirectCast(Entry.Value, Groupe)
If Gr.Nom = sPays Then Return Gr.Num
Next

So I want to have access to a property of each element of the collection

Thanks for your help

Bernard
===========================================================

Friend Class Groupe
Private mNom As String
Private mNum As Integer
Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property
Public Property Num() As Integer
Get
Return mNum
End Get
Set(ByVal Value As Integer)
mNum = Value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Cor Ligthert a écrit :
 
K

Ken Tucker [MVP]

Hi,

Make the class serializable.

http://msdn.microsoft.com/library/d...rlrfsystemserializableattributeclasstopic.asp

Ken
----------------------
Cor,
here is my goal

I have a class called Groupe that have two properties Nom and Num
(see code at the end)

Then I want to handle a collection of Groupe
So I have declared it as

Public Groupes As New Hashtable

and fulfiiled with

Dim Gr As New Groupe
Gr.Nom = "AMS3" : Gr.Num = 0
Groupes.Add(0, Gr)
Gr = New Groupe
Gr.Nom = "BRU" : Gr.Num = 1
Groupes.Add(1, Gr)
Gr = New Groupe
Gr.Nom = "LIS" : Gr.Num = 2
Groupes.Add(2, Gr)
Gr = New Groupe
Gr.Nom = "PAR" : Gr.Num = 3
Groupes.Add(3, "PAR")

Then I have the following code:
Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = DirectCast(Entry.Value, Groupe)
If Gr.Nom = sPays Then Return Gr.Num
Next

So I want to have access to a property of each element of the collection

Thanks for your help

Bernard
===========================================================

Friend Class Groupe
Private mNom As String
Private mNum As Integer
Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property
Public Property Num() As Integer
Get
Return mNum
End Get
Set(ByVal Value As Integer)
mNum = Value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Cor Ligthert a écrit :
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Ken

Thanks

Ken Tucker [MVP] a écrit :
Hi,

Make the class serializable.

http://msdn.microsoft.com/library/d...rlrfsystemserializableattributeclasstopic.asp

Ken
----------------------
Cor,
here is my goal

I have a class called Groupe that have two properties Nom and Num
(see code at the end)

Then I want to handle a collection of Groupe
So I have declared it as

Public Groupes As New Hashtable

and fulfiiled with

Dim Gr As New Groupe
Gr.Nom = "AMS3" : Gr.Num = 0
Groupes.Add(0, Gr)
Gr = New Groupe
Gr.Nom = "BRU" : Gr.Num = 1
Groupes.Add(1, Gr)
Gr = New Groupe
Gr.Nom = "LIS" : Gr.Num = 2
Groupes.Add(2, Gr)
Gr = New Groupe
Gr.Nom = "PAR" : Gr.Num = 3
Groupes.Add(3, "PAR")

Then I have the following code:
Dim Entry As DictionaryEntry
For Each Entry In Groupes
Dim Gr As Groupe = DirectCast(Entry.Value, Groupe)
If Gr.Nom = sPays Then Return Gr.Num
Next

So I want to have access to a property of each element of the collection

Thanks for your help

Bernard
===========================================================

Friend Class Groupe
Private mNom As String
Private mNum As Integer
Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property
Public Property Num() As Integer
Get
Return mNum
End Get
Set(ByVal Value As Integer)
mNum = Value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Cor Ligthert a écrit :
 
C

Cor Ligthert

Bernard,

Do you like hidden puzzles.
Next time show a sample that for us is more understandable.

When you use groupes it looks as an array of groupe

When you had used sometingh as hsGroupes than we could have probably found
easier your problem.

This error was not in your first sample by the way.
Gr.Nom = "PAR" : Gr.Num = 3
Groupes.Add(3, "PAR")
Groupes.Add(3, Gr)

I hope this helps,

Cor
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Hi

When I put the following line on the top of my class I have an error
message telling me that the Soap property does not exist !?
Imports System.Runtime.Serialization.Formatters.Soap

Bernard
 
C

Cor Ligthert

Bernard,

Did you look at my answer, your code works when you correct the error in the
previous part.

Cor
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Hi Cor and Ken

Sorry for being so confusing but I'm new in VB.NET and my problem was
complicated due to a first answer that misleaded me.

If I take the example given in the help section for hastable and
reproduced here above, it is basicaly waht I need BUT instead of placing
a string in the value of the hastable, I want to place an object that
is defined with my Class GROUPE which have two properties Name and Num.

My problem is that I want to have access in a loop to each properties
(Name and Num) for each element (Groupe) of my collection.

My problem is that (if I take the eaxmple given in the help) the code
myEnumerator.Name or myEnumerator.Num
does not work.

So my question is how can I have access to these properties?

Thanks for your help

Bernard



Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesHashtable

Public Shared Sub Main()

' Creates and initializes a new Hashtable.
Dim myHT As New Hashtable()
myHT.Add("First", "Hello")
myHT.Add("Second", "World")
myHT.Add("Third", "!")

' Displays the properties and values of the Hashtable.
Console.WriteLine("myHT")
Console.WriteLine(" Count: {0}", myHT.Count)
Console.WriteLine(" Keys and Values:")
PrintKeysAndValues(myHT)
End Sub

Public Shared Sub PrintKeysAndValues(myList As Hashtable)
Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
+ "-VALUE-")
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "{0}:" +
ControlChars.Tab _
+ "{1}", myEnumerator.Key, myEnumerator.Value)
End While
Console.WriteLine()
End Sub
End Class

' This code produces the following output.
'
' myHT
' Count: 3
' Keys and Values:
' -KEY- -VALUE-
' Third: !
' Second: World
' First: Hello
 
C

Cor Ligthert

Bernard,

Did you change that
Gr.Nom = "PAR" : Gr.Num = 3
Groupes.Add(3, "PAR")
Groupes.Add(3, Gr)

Than your code should run,
Now you are casting a string, while you are telling it is a groupe object.

It did run for me.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top