Thread: Getting type

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Getting type

    Hi,

    Can anyone please suggest how I can get type of a Stream i.e. whether its a memorystream or a filestream?

    AK

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    This is a way to do it:
    Code:
    try{
      object of mem stream = explicit cast to memstream <- object o
    catch( InvalidCastException) {
    try {
      object of filestream = explicit cast to filestream <-object o
    } catch ( InvalidCastException) {
      //do some more error handling
    }

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Eww...that's a pretty rough way to do it, GanglyLamb. There's a couple of ways to do this in C#.

    Using as

    The first way uses the as keyword:
    Code:
    void DoWork(object obj)
    {
    	MemoryStream memStream = obj as MemoryStream;
    	if (memStream != null)
    	{
    		//obj is a MemoryStream
    	}
    	else
    	{
    		FileStream fileStream = obj as FileStream;
    		if (fileStream != null)
    		{
    			//obj is a FileStream
    		}
    		else
    		{
    			//obj is not a FileStream or a MemoryStream
    		}
    	}
    }
    Coding in this pattern has some ugly implications; the nesting quickly becomes out of control if you're doing this. Each extra type you're checking becomes another level of nesting. You can remove some of the nesting by putting a return at the end of each if block.
    Code:
    void DoWork(object obj)
    {
    	MemoryStream memStream = obj as MemoryStream;
    	if (memStream != null)
    	{
    		//obj is a MemoryStream
    		return;
    	}
    
    	FileStream fileStream = obj as FileStream;
    	if (fileStream != null)
    	{
    		//obj is a FileStream
    		return;
    	}
    
    	//obj is not a FileStream or a MemoryStream
    }
    Personally, I like to limit the scope of my local variables, so I'd use:
    Code:
    void DoWork(object obj)
    {
    	{
    		MemoryStream memStream = obj as MemoryStream;
    		if (memStream != null)
    		{
    			//obj is a MemoryStream
    			return;
    		}
    	}
    
    	{
    		FileStream fileStream = obj as FileStream;
    		if (fileStream != null)
    		{
    			//obj is a FileStream
    			return;
    		}
    	}
    
    	//obj is not a FileStream or a MemoryStream
    }
    Using Reflection

    The second way is also pretty simple.
    Code:
    void DoWork(object obj)
    {
    	if (obj.GetType() == typeof(MemoryStream))
    	{
    		MemoryStream memStream = (MemoryStream)obj;
    		//obj is a MemoryStream
    		return;
    	}
    
    	if (obj.GetType() == typeof(MemoryStream))
    	{
    		FileStream fileStream = (FileStream)obj;
    		//obj is a FileStream
    		return;
    	}
    
    	//obj is not a FileStream or a MemoryStream
    }
    Is this better than the first way? Well, it's pretty easy to read, but I'm not sure how expensive reflection is.

    Why are you doing this at all?

    In general, though, when you need a kind of switch statement for types, a red flag pops up in my mind. Consider using some sort of polymorphic behavior instead of this kind of solution.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Yeah pianorain you are right, personally I dont use the solution I posted earlier myself.

    I myself use typeof ( since its the most readable way ) ... but I wanted to see how Angkar would respond ( why ? check my post at the "C# connect to site" thread. ) .

    ( guess maybe im overreacting... if so it's probably because im having exams right now )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM