type VARIANT in C#

M

Martin

Hi,
I'm a beginner.

Is there type VARIANT in C#?

I use my class dbTextbox (which inherited from Textbox) to store and display
diferent values type from database. Therefore I want to create property
VALUE, which can have dif value types. How can I do this? Is there any idea?

Thanks for help
Martin
 
J

Jon Skeet [C# MVP]

Martin said:
Is there type VARIANT in C#?

No, but there is "object", which all types derive from eventually (sort
of, anyway- value types themselves don't derive from anything, but
their boxed equivalents do).
I use my class dbTextbox (which inherited from Textbox) to store and
display diferent values type from database. Therefore I want to
create property VALUE, which can have dif value types. How can I do
this? Is there any idea?

You can certainly declare it to return object.
 
M

Martin

I can't understand you well.
I does, what you wrote. My DBTextbox class:
public class dbText : System.Windows.Forms.TextBox
public object Value
{
get
{
return base.Text;
}
set
{
base.Text = value.ToString();
}

}

in a function of a form, I try to get value of the dbTextbox, which should be float, but I can't.
private void button1_Click(object sender, System.EventArgs e)
{
float sngValue = 1.234f;
dbtextbox1.Value = sngValue;
dbText2.Value = dbtextbox1.Value;
sngValue =(float) dbText2.Value.ToString() ;
}

How can I do this?

Thanks
Martin
 
J

Jon Skeet [C# MVP]

Martin said:
I can't understand you well.
I does, what you wrote. My DBTextbox class:
public class dbText : System.Windows.Forms.TextBox
public object Value
{
get
{
return base.Text;
}
set
{
base.Text = value.ToString();
}

}

in a function of a form, I try to get value of the dbTextbox, which
should be float, but I can't.
private void button1_Click(object sender, System.EventArgs e)
{
float sngValue = 1.234f;
dbtextbox1.Value = sngValue;
dbText2.Value = dbtextbox1.Value;
sngValue =(float) dbText2.Value.ToString() ;
}

Well, you're *actually* always returning a string at the moment. If you
want to be able to cast to float, you need to return a float. Instead
of just changing the Text property's value, you could have another
member variable which stored the value which was actually set, and
return that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top