Question about Objects

Phiban

Member
Joined
Jul 11, 2006
Messages
22
Programming Experience
Beginner
Hey Guys

I'm pretty much new to VB.net and was hopeing that you may be able to help me. Im currently trying to simulate Multiplexing and De-multiplexing in vb.net. Basicall What im doing is reading in 80 bytes from a file to a byte array until the end of File is returned, Once 80 bytes has been read in it gets stored in a class called Create_Packet. This class basically takes the byte() and has variables such as Sequence_Number etc.

Is there a way that i can take the byte array and the other information created and place it in an object, because idealy i want to place (if possible) this object on a shared queue to be read by another class called Read_Packet. If so how would this Read_Class extract the individual pieces of information i.e. the byte array and the Sequence number?

Is this possible or am I just a complet noob who should never touch programming again :eek:

Any help that you could provide would be most welcome.

Thanks in advance :)
 
Oh, ok can you please explain what u mean by
Any why are you only trying to pass the 81st byte?
Also Im not sure what you mean by
bear in mind im prettty new to this stuff
our Queue should be strong type Of CreatePacket, if not you must cast the object to this type and possible other types contained by the queue before using it.

I created this class as it will be used (later) by mulitple forms, i just thought this was the way to do it?
 
Your queue should be declared like this (or possibly Public/Private/Shared etc):
VB.NET:
Dim q As New Queue(Of CreatePacket)
That would mean only object instances of type CreatePacket is allowed to add to this queue, and also the returned type is then CreatePacket and not Object.

When you use the generic Object type Queue class you are boxing any type into Object, and that is also what is returned. The class Object does not have a get_buffer member, CreatePacket does. What you did 'manually' accessing the get_buffer method on an Object type instance is called late binding and should be avoided. So to use specific member types when you only get an Object you can cast it directly (when you know it is exactly that type) like this:
VB.NET:
Dim cp As CreatePacket = DirectCast(myobject, CreatePacket)

When you say Return bytes(80) you are returning element 81 of an array named 'bytes'. The return type of your function is Byte, and not array of bytes which is type Byte(). Types and arrays is elementary beginner reading, somehow it seems you missed the first few chapters in the book.
 
Cool, Yeah the array issue was an oversight on my attention to code. One question tho, When i use Dim cp as CreatePacket = DirectCast(myobject, createpacket) what should "myobject" refer to? and how would i apply it to my code in the above? I know it seems like im out of depts here, and you would be right, however im just interested. I appologies to buggin you
 
You don't have to cast if you use the Queue(Of CreatePacket), then what it dequeues is already of type CreatePacket.
 
Thnaks man, so in my class which tries to take the buffer from the queue should be as follows yes?
However when i run this i get the following error "Object reference not set to an instance of an object." pointing to Dim cp As CreatePacket = DirectCast(x1, CreatePacket), I know its something stupid which i have done :S Also i my way of writing the buffer once retieved correct?

VB.NET:
Imports System.IO
Public Class test
    Inherits System.Windows.Forms.Form
    Dim file_name As String = "C:\test.txt"
    Dim fileStream As FileStream = New FileStream(file_name, FileMode.Create, FileAccess.Write)
    Dim bytes() As Byte
    Dim CreatePacket As CreatePacket
    Dim ForwardQueue As ForwardQueue

    Dim x1 As Object


+Region " Windows Form Designer generated code "



    Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReadByte()
    End Sub

    Private Sub ReadByte()

        
        Dim cp As CreatePacket = DirectCast(x1, CreatePacket)


        cp = ForwardQueue.q.Dequeue
        x1 = cp.get_buffer()


    
        For i As Integer = 0 To x1.Length - 1
            fileStream.WriteByte(x1(i))


        Next i

        fileStream.Close()


    End Sub
End Class
The code for my queue is a follows:

VB.NET:
Imports System
Imports System.Collections.Generic.Queue(Of Object)



Public Class ForwardQueue

    Dim Createpacket As CreatePacket
    Public Shared q As New Queue

    Public sub push(ByVal value As Object) 
        q.Enqueue(value)

       
    End sub

    Public Function pop() As Object
        Dim x As Object
        x = q.Dequeue()
        Return x

    End Function


End Class
 
Last edited:
No.

Your queue should be this single line of code (if you absolutely need it Shared):
VB.NET:
Shared FQ As New Queue(Of CreatePacket)
Create and and queue a new packet like this:
VB.NET:
Dim packet As New CreatePacket
'perhaps put some bytes into the packet too?
FQ.Enqueue(packet)
Dequeue like this:
VB.NET:
Dim dequeuedpacket As CreatePacket = FQ.Dequeue
Dim bytes() As Byte = dequeuedpacket.get_buffer()
 
Ok when i do the above queue creation i get the following error " 'System.Collections.Queue' has no type parameters and so cannot have type arguments."
I have included the import "System.Collections.Generic.Queue(Of Object)" I have tried it also with "System.collections.Queue" Does it matter that it is in a seperate class?
 
You don't have to import anything to use the Queue(Of T) class in VB 2005, so perhaps what you've been telling us in your user Primary Platform all along is wrong?
 
Im using VS2005, even if i remove the imports i still get the same error, would it help if i actually sent u my code?
 
Last edited:
Very strange, I've never had to do that. Especially since there exist no System.Collections.Queue namespace. The generic class Queue(Of T) belongs to the System.Collections.Generic namespace. The object Queue belongs to System.Collections namespace.
 
Indeed it is :S

Well i think im going to give up, nothing seems to be working. Ive tried pushing a string i.e. a file name to the queue but when i pop it off its doesnt contain a string :(

Cheers for all your help tho!
 
No, there is nothing more to it than what has been said, if you still can't make the queue work I suggest you re-read all the posts in this thread, because then there must be some vital information that have slipped your mind. Especially run the code in post 7 and make sure you understand the basic functionality of it.
 
Ok this was my bad, i got the queue working now, what seems to be the problem is that when i pass the byte array to the queue the array contains nothing. Im sooo sorry for draging this topic out. I really appricate all the help you've given me!
 
Back
Top