473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

DirectCast invalid

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
Nov 21 '05 #1
15 1323
Hi,

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

Ken
-----------------
"Bernard Bourée" <be*****@bouree.net> wrote in message
news:e6*************@TK2MSFTNGP12.phx.gbl...
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
Nov 21 '05 #2
Did you specify the full namespace to the class in the DirectCast line?

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

Nov 21 '05 #3
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


Nov 21 '05 #4
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #5
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 :
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #6
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
Nov 21 '05 #7
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 :
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

Nov 21 '05 #8
Hi,

Make the class serializable.

http://msdn.microsoft.com/library/de...classtopic.asp

Ken
----------------------
"Bernard Bourée" <be*****@bouree.net> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
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 :
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

Nov 21 '05 #9
Ken

Thanks

Ken Tucker [MVP] a écrit :
Hi,

Make the class serializable.

http://msdn.microsoft.com/library/de...classtopic.asp

Ken
----------------------
"Bernard Bourée" <be*****@bouree.net> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
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 :
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


Nov 21 '05 #10
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
Nov 21 '05 #11
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

Ken Tucker [MVP] wrote:
Hi,

Make the class serializable.

http://msdn.microsoft.com/library/de...classtopic.asp

Ken
----------------------
"Bernard Bourée" <be*****@bouree.net> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
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 :
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


Nov 21 '05 #12
Bernard,

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

Cor
Nov 21 '05 #13
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
Nov 21 '05 #14
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
Nov 21 '05 #15
Cor

Yes it works fine.
Thanks for your help.

Bernard
Cor Ligthert wrote:
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

Nov 21 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sahil Malik | last post by:
I can't find it .. what am I missing? - Sahil Malik http://dotnetjunkies.com/weblog/sahilmalik
4
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) ...
11
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
1
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can...
4
by: Vagabond Software | last post by:
Here is the code: Dim amountDue As Double amountDue = Math.Round(DirectCast(strOrderTotal, Double), 2) This code produces an invalid cast exception. The String variable strOrderTotal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.