Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic Classes and Pointer to one of its Typeparameters [modified]

Generic Classes and Pointer to one of its Typeparameters [modified]

Scheduled Pinned Locked Moved C#
questionc++algorithmsbusinesshelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikone
    wrote on last edited by
    #1

    Hello everybody, i've been searching the forums for a similar question but wasn't successfull yet. So i decided to ask the question by myself. Is there ANY possibility to create a pointer in a generic class, which points to a variable of one of the specified typeparameters. A little example class myclass { unsafe T * myptr = null; } The compiler tells me he isn't able to declare a pointer of a "managed type" (I hope this is the correct translation so let me know if that doesn't make any sense) Is there a workaround to make this pointer point to the specified address AND act like a "native" pointer (please don't tell me about a void pointer - it just doesnt work for my purposes) ____________________________________ If this should not be possible (yeah, i almost gave up :)) is there a solution for my problem anyways? It's about associating some variable with another one. public class dbTable { ulong myvar = 0; dbColumn mycol = null; public dbTable() { mycol = new dbColumn(); mycol.MapVariable(ref myvar); } } public class dbColumn { public T mysecondvar; public MapVariable(ref T VarToMap) { mysecondvar = VarToMap; } } That example doesn't work of course because it will try to assign the value of "VarToMap" to "mysecondvar". When trying to retrieve "VarToMap"-address and assign it to "mysecondvar" the compiler throws out some errors too (the address wasn't meant to be the value of a ulong variable). I want to be able to get the following output when performing this code: dbTable mytable = new dbTable(); mytable.myvar = 5; Console.WriteLine(mytable.myvar.ToString()); Console.WriteLine(mytable.mycol.mysecondvar.ToString()); mytable.mycol.mysecondvar = 3; Console.WriteLine(mytable.myvar.ToString()); Console.WriteLine(mytable.mycol.mysecondvar.ToString()); This should produce: 5 5 3 3 So i want one Variable to keep the Value and another one to point at it BUT i need to be able to specify the type of both variables (void pointer would do the job but i'm not able to retrieve or assing values to the variable its pointing to because the "=" operator wasn't defined for!) I hope that someone has a solution for this problem (as already mentioned, i don't care about how it is realized as long as the requirements above are met) I would like to hear any kind of comment because i'm really desperated. If this i a ba

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups