Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
QuestionRetrieve name of Printer using SendMessage Pin
toBeH_S11-Sep-12 22:10
toBeH_S11-Sep-12 22:10 
AnswerRe: Retrieve name of Printer using SendMessage Pin
Rage12-Sep-12 6:40
professionalRage12-Sep-12 6:40 
GeneralRe: Retrieve name of Printer using SendMessage Pin
toBeH_S12-Sep-12 8:33
toBeH_S12-Sep-12 8:33 
GeneralRe: Retrieve name of Printer using SendMessage Pin
Rage12-Sep-12 21:25
professionalRage12-Sep-12 21:25 
GeneralRe: Retrieve name of Printer using SendMessage Pin
toBeH_S16-Sep-12 15:18
toBeH_S16-Sep-12 15:18 
QuestionRandom Permutation Interface Pin
Skippums11-Sep-12 15:58
Skippums11-Sep-12 15:58 
AnswerRe: Random Permutation Interface Pin
Dave Kreskowiak11-Sep-12 17:44
mveDave Kreskowiak11-Sep-12 17:44 
AnswerRe: Random Permutation Interface Pin
BillWoodruff12-Sep-12 0:05
professionalBillWoodruff12-Sep-12 0:05 
I find this an interesting programming problem, even though I wonder, like Dave K., why you'd want to do this (gambling program, card game shuffle and deal).

But, if someone with the intelligence, and skill, of Dave K. says this cannot be done as an extension, and that you will need to have wrappers for each of your Types you wish to "permutate:" I'd accept his opinion, and pursue his suggestions, if you must do this.

However, that doesn't stop me, out of curiousity, just starting to see how this might be done (but, not as an extension).

Here's some code I started, and will not have time to work-on further for a while, but perhaps it will be useful to you in some way:
XML
// Visual Studio 2012 RC
// Framework 4.5
// WinForms: one Form with one Button it named 'Permute
//
// build a List of Types to permute
private static List<Type> OkayCollectionTypes = new List<Type>()
{
    typeof(List<>),
    typeof(Stack),
    typeof(Queue),
    typeof(LinkedList<>)
};

// create some samples to play with
private static List<int> IntList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

private static Stack AStack = new Stack(IntList as IList);

// test stub for Permute function
private static dynamic Permute(dynamic permuteTarget)
{
    Type currentType = permuteTarget.GetType();

    // Note: we cannot use Type.GetType() here
    // an error will be thrown
    // see comments in the BtnPermuteClick EventHandler

    if(OkayCollectionTypes.Contains(currentType))
    {
        MessageBox.Show("okay : Type = " + permuteTarget.ToString());
    }
    else
    {
        return null;
    }

    // at this point we create a new instance
    // of 'permuteTarget that we permutate ?
    // to be explored ...
    return (permuteTarget);
}

private void BtnPermuteClick(object sender, EventArgs e)
{
    // this works
    // although it has no effect on AStack's contents
     AStack = Permute(AStack);

    // this will not work
    // because the type of IntList is:
    // System.Collections
    //  .Generic.List`1
        // [
            //[System.Int32,
            // ... snip ...
            //PublicKeyToken=b77a5c561934e089]
        // ]

    // if we tried to use Type.GetType() in the Permute method
    // an error would be thrown like this:
    //'System.Collections.Generic.List<int>' does not contain a definition for 'Type'

    // var mutatedList = Permute(IntList);
}
Obvious questions to be explored:

0. is using 'dynamic here the source of all problems ?

1. is the problem with trying use the 'Permute method on 'IntList here created by the fact that I am using direct assignment to create the contents of 'IntList, thus making its contents not usable for some reason ?

No, it is not, because initializing IntList in a for loop using 'Add: the Type of the dynamic parameter in the Permute method will look like this when examined at run-time using the Command window after reaching a break-point:
> ? currentType is IList
    false
    > ? currentType is List<int>
    false

    * the following edited for brevity
    > ? currentType.GetType()
    {Name = "RuntimeType" FullName = "System.RuntimeType"}
        [System.RuntimeType]: {Name = "RuntimeType" FullName = "System.RuntimeType"}
        base {System.Reflection.MemberInfo}: {Name = "RuntimeType" FullName = "System.RuntimeType"}
        AssemblyQualifiedName: "System.RuntimeType, ... snip ... PublicKeyToken=b77a5c561934e089"
        BaseType: {Name = "TypeInfo" FullName = 
        GenericParameterPosition: 'currentType.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
        GenericTypeArguments: {System.Type[0]}
        GUID: {97e69551-7329-39c5-ba61-2ca4b573e0e5}
        IsClass: true
        IsGenericType: false
    UnderlyingSystemType: {Name = "RuntimeType" FullName = "System.RuntimeType"}
Out of time: now.

best, Bill
<color>"When it comes to atoms, language can be used only as in poetry. The poet, too, is not nearly so concerned with describing facts as with creating images." Niels Bohr

AnswerRe: Random Permutation Interface Pin
Pete O'Hanlon12-Sep-12 0:18
mvePete O'Hanlon12-Sep-12 0:18 
GeneralRe: Random Permutation Interface Pin
Dave Kreskowiak12-Sep-12 2:21
mveDave Kreskowiak12-Sep-12 2:21 
AnswerRe: Random Permutation Interface Pin
BobJanova12-Sep-12 1:17
BobJanova12-Sep-12 1:17 
GeneralRe: Random Permutation Interface Pin
Pete O'Hanlon12-Sep-12 1:27
mvePete O'Hanlon12-Sep-12 1:27 
GeneralRe: Random Permutation Interface Pin
Skippums12-Sep-12 6:41
Skippums12-Sep-12 6:41 
AnswerRe: Random Permutation Interface Pin
PIEBALDconsult12-Sep-12 3:07
mvePIEBALDconsult12-Sep-12 3:07 
AnswerRe: Random Permutation Interface Pin
Skippums12-Sep-12 7:32
Skippums12-Sep-12 7:32 
GeneralRe: Random Permutation Interface Pin
BillWoodruff13-Sep-12 2:37
professionalBillWoodruff13-Sep-12 2:37 
GeneralRe: Random Permutation Interface Pin
PIEBALDconsult13-Sep-12 3:23
mvePIEBALDconsult13-Sep-12 3:23 
GeneralRe: Random Permutation Interface Pin
PIEBALDconsult13-Sep-12 3:27
mvePIEBALDconsult13-Sep-12 3:27 
GeneralRe: Random Permutation Interface Pin
Skippums13-Sep-12 7:27
Skippums13-Sep-12 7:27 
Questionconvert xdocument to integer Pin
rachel_m11-Sep-12 12:16
rachel_m11-Sep-12 12:16 
AnswerRe: convert xdocument to integer Pin
Pete O'Hanlon11-Sep-12 12:22
mvePete O'Hanlon11-Sep-12 12:22 
GeneralRe: convert xdocument to integer Pin
rachel_m11-Sep-12 16:04
rachel_m11-Sep-12 16:04 
GeneralRe: convert xdocument to integer Pin
Eddy Vluggen11-Sep-12 20:55
professionalEddy Vluggen11-Sep-12 20:55 
GeneralRe: convert xdocument to integer Pin
rachel_m12-Sep-12 4:44
rachel_m12-Sep-12 4:44 
GeneralRe: convert xdocument to integer Pin
PIEBALDconsult13-Sep-12 3:29
mvePIEBALDconsult13-Sep-12 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.