473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C#--Determining a property's value type

I need help determining the correct syntax for achieving something:

I have a property and in the set accessor I want the code to determine if the value is of a certain type. If it's not then I need it to throw an error and if it is then it should proceed as normal. I was trying to do it thusly:

if (! value == typeof(XXXXX))

but the compiler doesn't like that so I'm assuming my syntax is wrong.

Thank you in advance.
Aug 8 '07 #1
3 1342
Plater
7,872 Expert 4TB
You are close, try this:
if (! value.GetType() == typeof(XXXXX))

I need help determining the correct syntax for achieving something:

I have a property and in the set accessor I want the code to determine if the value is of a certain type. If it's not then I need it to throw an error and if it is then it should proceed as normal. I was trying to do it thusly:

if (! value == typeof(XXXXX))

but the compiler doesn't like that so I'm assuming my syntax is wrong.

Thank you in advance.
Aug 8 '07 #2
nateraaaa
663 Expert 512MB
I need help determining the correct syntax for achieving something:

I have a property and in the set accessor I want the code to determine if the value is of a certain type. If it's not then I need it to throw an error and if it is then it should proceed as normal. I was trying to do it thusly:

if (! value == typeof(XXXXX))

but the compiler doesn't like that so I'm assuming my syntax is wrong.

Thank you in advance.
I am not sure why you want to do all of this in the setter of your accessor. Why not set to value then where you assign this value to a variable or control check for the value type and throw your error. Accessors should only be used to get and set. The other logic that you want to add should be in another method.

Expand|Select|Wrap|Line Numbers
  1. private string accessor;
  2. public string Accessor
  3. {
  4. set{accessor = value;}
  5. }
  6.  
  7. private void CheckValueType()
  8. {
  9. if (!accessor == typeof(XXXXX))
  10. {lblLabel1.Text = accessor.ToString()}
  11. else
  12. {throw error;}
  13.  
  14. }
Nathan
Aug 8 '07 #3
Plater
7,872 Expert 4TB
I am not sure why you want to do all of this in the setter of your accessor.
I missed that part. If you try and send an object that doesn't match the correct type an exception will be thrown anyway. You're making more work for yourself when it's already been done for you.
Aug 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Andrew | last post by:
It seems that in C#, given an instance "a" of some value type "A", one can pass a by reference to functions with signatures of either "void f(object obj)" or "void g(ref A obj)". But passing a into...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
5
by: Dave | last post by:
I'd like to create a Registry key value where the Registry key value type is REG_QWORD. I'm using SetValue(name As String, value As Object), and I understand that the object type used in this...
4
by: Mathieu Cartoixa | last post by:
Hi, I have been annoyed in one of my recent projects with a problem related to the explicit implementation of an interface on a value type. I will take an example to show the problem. Say we...
3
by: Rich | last post by:
Another noob question for you all . . . I have functions written in C++ that I want to call from C#. I need to be able to pass a reference to a value type (like an int) so that the function can...
4
by: Smithers | last post by:
Are delegates value types or reference types? I was just pondering the fact that delegates are immutable... Does this mean that they are value types and not reference types? Thanks.
24
by: carnold | last post by:
Hello, I'm a developer coming from C++ and Java ... I've going thru "Effective C#" (which highly recommend for people coming from other languages wanting to learn C#), and it states that "value...
2
by: =?Utf-8?B?c2FtZWVt?= | last post by:
Dear All up to my level that I have known the StringBuilder class is reference type. Someone telling that StringBuilder class is Value type. Hence I want to clarify which is right? and how? ...
5
by: romiverma | last post by:
hi, i have used sql2000 stored procedure with 2 date parameters to create crystal report 8.5. when i am sending date parameters from vb6 and calling the report, its giving error "the specified value...
4
by: Allwin Jeba | last post by:
This is a basic question which is confusing me.. Here is the sample code.. class Program { static void Main(string args) { c obj = new c();
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.