Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I have a collection of Users object, in each of those objects I want to raise an timeout event when the user has had a period of inactivity.

In the collection class I want to raise an event when a user gets timed out.

 

I've tried using withevents on the array of objects but that's not allowed, so how do I solve this?

 

TIA

Kejpa

Posted
Hi,

I have a collection of Users object, in each of those objects I want to raise an timeout event when the user has had a period of inactivity.

In the collection class I want to raise an event when a user gets timed out.

 

I've tried using withevents on the array of objects but that's not allowed, so how do I solve this?

 

TIA

Kejpa

Objectively, the array doesnt timeout, does it???

No. . . so why would you tie that event to the array???

and frankly. . . your array isnt an array of users, its and array of user sessions? right?

 

define a User object with properties/events related to the user, like name rank serial #

define a UserSession object with properties/events related to UserSessions like, datetime created, an activity Timer and a TimeOutEvent.

 

Your user can refer to its session, just as the Session can refer to the user.

On any method call by the user object reset the usersession timer

 

Make the collection a list of user sessions.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Hi,

No, the array doesn't time out. It's the objects in the array that is timing out. Maybe I was unclear about that.

 

I can't see how changing from an array of user objects to an array of userSession objects will help me raising an event in the collection class. :confused:

It's an array of objects (User or UserSession or something else) where something (timeout or log out or arrival of new data) happens in one of the objects in the array and the class keeping the objects needs to respond on that action.

 

Have I made myself better understood?

 

TIA

/Kejpa

Posted (edited)
Hi,

No, the array doesn't time out. It's the objects in the array that is timing out. Maybe I was unclear about that.

No you weren't unclear about that. . . and that is exactly why I am asking, 'why would you tie the event to the array?'

'Timeout' in your case, is an event. the timeout should be associated to the objects that are in the array.

 

But if you must. . .

Derive from ArrayList class and add events to that

 

Still. . .

The container shouldnt respond to internal changes to its contained objects.

The contained objects should respond to their own internal changes, themselves.

 

In the real world. . .

 

I have a job I go to everyday.

When I go to work I must punch in and put my time card in the 'In' slot.

If my boss needs something done, he looks to see who has cards in the 'In' slot uses them

when I am done I punch out and move my card over.

 

I am the contained object.

I Start my Session (punch in),

I Move my Token to the array ('in' slot)

I End my Session (punch out)

I remove my Token from to the array.

 

You are programming in the passive voice.

The array is a passive object in this scenario. Objects operate on it, it doesnt operate on objects.

 

You wouldnt say Stick.GetFechedBy(dog) would you?

no. . .

you would say Dog.Fetch(Stick), right?

 

as far as the UserSessions. . . that was just an observation. and offered only for consideration.

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Well Joe,

I don't know about your boss, my my boss acts on the event of my arrival at the desk with a "Greetings to thee, oh impressive Kejpa", of course, that is after offering me coffee ;)

See, the container acts on events of the contained object.

Different actions for different object, I'm the only one who gets coffee but some others get tea.

 

ArrayList, I'll have to have a look at that.

 

Thanx a bunch.

/Kejpa

Posted (edited)
Well Joe,

I don't know about your boss, my my boss acts on the event of my arrival at the desk with a "Greetings to thee, oh impressive Kejpa", of course, that is after offering me coffee ;)

See, the container acts on events of the contained object.

Different actions for different object, I'm the only one who gets coffee but some others get tea.

 

ArrayList, I'll have to have a look at that.

 

Thanx a bunch.

/Kejpa

Yes the container acts. . . but it is responding to events associated with the contained object. Take forms, for example. . . it contains buttons. the buttons have click events. it so happens that the delegate associated with the event is a method on the form, but still the event is associated with the button, not the form.

 

here is the way I would do what you want to do. . .

 

C# 'cause I dont do VB. (I can't stand vb)

namespace UserMgmt
{
public enum SessionEvent
{
	SessionStart, 
	SessionRenew,
	SessionEnd
}

public class SessionList 
{
	public void OnSessionEvent(object sender, SessionEventArgs args)
	{
		//write code here to manage session events!
	}
}

public class SessionEventArgs: System.EventArgs
{
	private SessionEvent evt;

	public SessionEventArgs (SessionEventType aEvt) :base()
	{
		evt = aEvt;
	}
	public SessionEvent EventType
	{
		get
		{
			return aEvt;
		}
	}

	public UserSession Session
	{
		get
		{
			return session;
		}
	}
}

delegate void SessionEventHandler(SessionEvent eventArgs); 

public class User
{
	// this field actually represents many fields combined here for brevity
	string name_rank_serialNumber = "";
	// this method actually represents many method combined here for brevity
	protected virtual void Set_name_rank_serialNumber(string aName_rank_serialNumber)
	{
		name_rank_serialNumber = aName_rank_serialNumber;
	}

	// this method, too, actually represents many method combined here for brevity
	//Notice I make them virtual? this is so I can extend them if I find this class useful!!!
	protected virtual string Get_name_rank_serialNumber()
	{
		return name_rank_serialNumber;
	}
	
	// this property, actually represents many properties combined here for brevity
	public string Name_rank_serialNumber
	{
		get
		{
			return Get_name_rank_serialNumber();
		}
		set
		{
			Set_name_rank_serialNumber(value);
		}
	}
}

public class UserSession
{
	User user = null;
	SessionList sessionList;

	protected event SessionEventHandler m_SessionStart;
	protected event SessionEventHandler m_SessionRenew;
	protected event SessionEventHandler m_SessionEnd;

	public event SessionEventHandler SessionStart
	{
		add
		{
			m_SessionStart += value;
		}
		remove
		{
			m_SessionStart -= value;
		}
	}

	public event SessionEventHandler SessionRenew
	{
		add
		{
			m_SessionRenew += value;
		}
		remove
		{
			m_SessionRenew -= value;
		}
	}

	public event SessionEventHandler SessionEnd
	{
		add
		{
			m_SessionEnd += value;
		}
		remove
		{
			m_SessionEnd -= value;
		}
	}
	
	public void Start()
	{
		if (SessionStart != null)
			SessionStart(this, new SessionEventArgs(SessionEvent.SessionStart));
	}

	public void Renew()
	{
		if (SessionRenew != null)
			SessionRenew(this, new SessionEventArgs(SessionEvent.SessionRenew));
	}

	public void End()
	{
		if (SessionEnd != null)
			SessionEnd(this, new SessionEventArgs(SessionEvent.SessionEnd));
	}

	public UserSession(User aUser, SessionList aSessionList)
	{
		user = aUser;
		sessionList = aSessionList;
		SessionStart = new SessionEventHandler(aSessionList.OnSessionEvent);
		SessionRenew = new SessionEventHandler(aSessionList.OnSessionEvent);
		SessionEnd = new SessionEventHandler(aSessionList.OnSessionEvent);
	}
}
}

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Hey there,

monday and back at work ;)

 

You're right about who's acting and who's just keeping an eye in the User case. I'll change my view on that.

 

Still...

I'm listening to a number of serial ports where I get online values that will be used for further calculations. Which ports and the number of ports are dynamic depending on the location (and so I set that up in a database). The handling though has to be done in a central dedicated object, not in the communication objects.

And so we're back, one container object that will respond on child objects. If I knew that there were only to be a fixed number of ports I need to listen to, I'd declare them using WithEvents. For flexibility I'm using an array but I still needs get the events, don't I?

Should I have a closer look at delegates, maybe?

 

Have a great day

/Kejpa

Posted

 

Should I have a closer look at delegates, maybe?

/Kejpa

DEFINITELY!!! :)

 

Event on the port object delegated to a method on the Container object!

 

On PortObject Event,

Call delegated container method!!!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...