Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variant Public variable in my class

Status
Not open for further replies.

YvonneTsang

Programmer
Sep 28, 2001
37
CA
I was wondering if you are able to create a variant public variable in a class. I have a class that one of the variables can be set to one and only 1 of 4 different types/classes. Am I able to do this? Should I just have the other classes inherit from my main class? What is the easiest way to approach this problem? I am new to C# and it has been ages since I did Java so I am having trouble remembering what to do.

Essentially:

class MainOne
{
public string type;
public variant info;

public MainOne(string typedetails)
{
type = typedetails
if (type = "a")
{
info = new a()
}

etc
}


class a
class b
class c
class d


All of these classes will write to a seperate SQL table and I want to keep it easy to work with and only have 1 class write to each table. Thanks in advance for all of your help.
 
C# (and .NET in general) doesn't have the Variant datatype like VB6 & VBA had. Use the Object datatype. But be aware of what's called "Boxing", which is where a value datatype (like structs, integers, floats, etc) get cast into an object, and then back again. It can be a performance hog (but sometimes it's necessary).

Chip H.
 
Couldn't you also do this with an overloaded constructor?

class test
{
public test( int a )
{
m_intA = a;
m_stringAtype = a.GetType().ToString();
}
public test ( float a )
{
m_floatA = a;
m_stringAtype = a.GetType().ToString();
}

public int A ()
{
get {
return m_intA;
}
}
public float A()
{
get {
return m_floatA;
}
}

Then he can either use an object or a set of member variables (m_intA, m_floatA) along with an "AType" variable so he can tell which variable it is from the outside.
Dean
---
goddette@san.rr.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top