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 :)
 
Thanks

Cheers for the speedy reply :) Sorry to ask a stupid question but could you explain or give an example of class memeber/properites?

How will the other class be able to extract the diffrent data types? is there an example you could link me to if not no worries

Again thanks for your help :)
 
Ah I now (Kind of) understand, sorry i didnt know what you ment by member etc, I'l try and implement this in the morning as its 4 am where Iam, I will let you know how i get on, chances are ill have more issues :S thanks for help!
 
Now i have a queue problem!

Ok so thanks to your help Ive managed to make the object with the required data, Im able to Enqueue but however when i try and remove it from the queue I get an error telling me the queue is empty :s

Below is how i tried to implement it.

So in my Main Forum I have create a object called Create Packet, which accesses get and set methods from the CreatePacket class. I then try and push the object onto the queue.


VB.NET:
            CreatePacket.Input_Buffer(bytes)
            CreatePacket.Set_Type_Field(2)
            CreatePacket.Set_ID_Num(ID)
            CreatePacket.Set_SequenceNum(SequenceNum)
            ForwardQueue.push(CreatePacket)
'Code for Queue system

VB.NET:
Imports System
Imports System.Collections.Queue

Public Class ForwardQueue
    Private test As New Queue

    Public Function push(ByVal value As Object)
       test.Enqueue(value)

    End Function

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

End Class
In the ForwardQueue class I want to return the object which i placed on the queue. In another class called testQueue i have the following

VB.NET:
Private sub ReadPacket
Dim x as Object

x = ForwardQueue.pop()
x.get_SequenceNum

end Sub

Is there something im doing wrong that it saying the queue is empty?
Thanks in advance
 
Your example works fine when I test it, here is simplified test:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] q [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Queue([/SIZE][SIZE=2][COLOR=#0000ff]Of [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]q.Enqueue([/SIZE][SIZE=2][COLOR=#800000]"hello queue 1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
q.Enqueue([SIZE=2][COLOR=#800000]"hello queue 2"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = q.Dequeue[/SIZE]
 
Ok, so Ive changed the data type would recieve as in from a Object to a string, however im still getting a message saying its empty is there any reason why this could be happening? the way i declaired the instance of the class, Is because the queue is located in a diffrent class? etc Please Help :s
 
No, that doesn't matter as long as you're using the same instance of that class to enqueue and dequeue.. doh...
 
Ok so I know what was happening! In the main class where the pop was happing i made the following declaration Dim ForwardQueue = new ForwardQueue, so by my understanding it was making a new Object refrence and hence the queue would be empty, so if I'm correct, how can i make a call to pop something off the queue in a diffrent class.

i.e. In my main class i make a new refrence to ForwardQueue and push something onto the queue, then in the other class i need to refrence the queue to pop off the object? any ideas?
 
Ok i think i may have solved my own problem, please correct me if i have done this incorrectly. I declaired the Queue as Public shared TEST as Queue
In my test class i can read the object fine :) however if there is another way I'd love to hear it because I'm trying to learn as much as possible about vb.net

Howver upon fixing this i have discovered a new problem (By the way im really sorry about asking for help with all these problem, I hope I'm not taking advanage of this forum) when i store the object on the queue it contains a byte array of size 80 however when i try to read this byte() back It doesnt write whats contained in the buffer to the file? Any ideas?

VB.NET:
Dim file_name = "C:\test.txt"
Dim fileStream As FileStream = New FileStream(file_name, FileMode.Create)
Dim ForwardQueue As New ForwardQueue
  Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReadByte()
    End Sub

    Private Sub ReadByte()
        Dim x As Object
        Dim bytes(80) As Byte
        
        x = ForwardQueue.test.Dequeue
    
        bytes(80) = x.get_buffer()

       For i As Integer = 0 To bytes.Length - 1
            FileStream.WriteByte(bytes(i))

        Next i

        fileStream.Close()
    End Sub
Also here is the code for returning the buffer in the CreatePacket class which just takes in the byte()
VB.NET:
Public Function get_buffer() As Byte
        Return bytes(80)
    End Function
 
Last edited:
Why are you only trying to pass the 81st byte? Would seem likely that you passed the full array. Your 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. Your queue instance within ForwardQueue class should not be declared Shared, an instance of ForwardQueue could be made Shared for common usage throughout the application if there was no other sensible way that object instance could be passed around good OOP. And why have you created this ForwardQueue class anyway? It doesn't add any functionality to the Queue class.
 
Back
Top