|
It seems that there's indeed an abstract class for working with the structs. If you declare an enum, you're actually inserting named numbers;
MSDN wrote: An enumeration is a named constant whose underlying type is any integral type except Char.
..and then I ran into this post[^] from Brad Abrams, stating that both System.Enum and System.ValueType are implemented as reference-types.
If I understood it correctly, then there's a difference between the abstract System.Enum class and the declared enum that is based on a underlying type?
I are troll
|
|
|
|
|
I think enum values are treated like boxed ints by default, although IIRC it's possible to use other types for the values. Whatever it is there's runtime type information available to know what enum type the value belongs to.
And we're allowed to cast between enums in the somewhat optimistic hope that their underlying values correspond.
This much can be shown by running the following simple application (requires you to create a new windows forms app and drop a button on the form and wire up the click event, as I don't include the designer.cs file here).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
enum en { Zero, One, Two, Three };
enum fr { Nul, Un, Deux, Trois };
void foo(object obj)
{
if (obj is en) dbug("en:{0} => fr:{1}", obj, (fr)obj);
else if (obj is fr) dbug("fr:{0} => en:{1}", obj, (en)obj);
else dbug("?:{0} => en:{1}, fr:{2}", obj, (en)obj, (fr)obj);
}
void dbug(string s, params object[] args)
{
if (args.Length > 0) s = string.Format(s, args);
Debug.WriteLine(s);
}
void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
for (int i = 0; i < 20; i++)
{
object v = r.Next(4);
if (r.Next(100) < 33)
foo((en)v);
else if (r.Next(100) < 50)
foo((fr)v);
else foo(v);
Thread.Sleep(500);
}
}
}
|
|
|
|
|
|
OK. Given that System.Enum is declared as public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible , in your opinion, what should the following return?
Type.GetType("System.Enum").IsClass
Type.GetType("System.Enum").IsInterface
Type.GetType("System.Enum").IsValueType
My guess would be true, false, false when the reality is false, false, false.
As far as I can tell, Enum is the only type declared as a class in the System namespace hierarchy where Type.GetType("type name").IsClass returns false. I'm trying to find out why, and nothing I've read so far has answered that question.
BTW, not everything derives from Object . Interfaces do not derive from anything.
|
|
|
|
|
Adrian Cole wrote: I'm trying to find out why, and nothing I've read so far has answered that question.
We've already answered that. All value types return false for IsClass, just as the documentation specifies; that's why.
Anyone can write a class that derives from ValueType and it will do the same thing, there just aren't that many times where you want a value type that isn't a structure. Enum is one such case because it also has to be abstract, there's no way to make an abstract structure, so they made it an abstract value type class.
|
|
|
|
|
Yeah, I guess I was just too stubborn to have it sink in. For those that care, creating a class that derives from System.ValueType isn't allowed ... 'TestNamespace.TestClass' cannot derive from special class 'System.ValueType'.
|
|
|
|
|
Hello,
Recently I've decided to rewrite some of my C# code to F# (pratice, learn, maybe move and optimize some code for future). Unfortunately I've faced few problems.
Let's say I have a c# class that looks like this:
public class MersenneTwister : Random
{
private const int N = 624;
private uint[] mt = null;
public MersenneTwister(uint seed)
{
mt = new uint[N]
mt[0] = seed & 0xffffffffU;
for (int mti = 1; mti < N; ++mti)
{
mt[mti] = (69069 * mt[mti - 1]) & 0xffffffffU;
}
}
}
I have no idea how to create constants like N - I could write static let N = 24 but:
- After compilation, c#'s version does replace N with number, as consts should work, f# does not (it uses static field to represent N)
- I can't use static let if I don't have implicit constructor for type, and I can't have one, because my constructor has to implement some code.
I have to create mt array as mutable, but I can't use let (no implicit constructor), if I use member x.mt : uint32 array = Array.create N 0u instead, new array will be created each time I use mt property.
I will be grateful for any ideas.
I also must add that, I can't believe so many people say f# is much more expressive and easier then c#, I'm trying to learn it, but for now, each time I want to do something my way, I have to search informations in books and/or google a lot
|
|
|
|
|
Ravadre wrote: I can't believe so many people say f# is much more expressive and easier then c#
It's more expressive but it is certainly not easier.
Kevin
|
|
|
|
|
I wander - if i write those two lines
a. Object o1;
b. o1 = new Object();
1. In line a - the o1 is initialized to null - is this pointer will be allocate on stack in this place ?
2. when i write "new Object()" ( line b )- is in this step i allocate the new object on the heap ?
Thanks.
|
|
|
|
|
assuming both statements are inside some method:
1. yes the pointer resides on the stack, and no it is not initialized; only class members get initialized to zero/null/Nothing
2. yes
I suggest you buy and study a tutorial book on one of the .NET languages; it will explain it all, step by step, in a systematic way.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
|
one down, a gazillion more to go.
It takes more to stop them all.
O heck, wrong animal, I'll have to keep trying.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Like Luc said, it depends on where you write them.
And o1 is not initialized to anything, meaning it could have Any value (leftover bits from values that used to be on the stack but are now just hanging around), but you're not allowed* to use it (it's an error if you do, will not compile) so you won't see this value.
* well, there is unsafe code. In all the test I did (not that many..) it was just 0 anyway but that's not something I'd rely on.
|
|
|
|
|
Hello I'm a beginner with VB, and I was wondering if someone can help me?
How can you count and display the amount of times that the save button is clicked?
Thank you in advance, Aaron
|
|
|
|
|
AARONDAS wrote: Hello I'm a beginner with VB
Hopefully your plan for learning computer programming is not based on the notion of using internet forums as your source of instructional materials. If it is your plan you should either change it or give up. The choice is yours of course.
|
|
|
|
|
You excel at avoiding words such as buy, book, and study. Ran out of horses?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Actually it's fairly straightforward.
You start with one. Then comes two. Three. Four. Five.
How far do you need to count?
|
|
|
|
|
Hi,
do as below for displaying number of count of click event.
Public Class Form1
Public count As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
count += 1
Me.Label1.Text = count.ToString()
End Sub
End Class
|
|
|
|
|
Hi,
We are writing an WPF application using .NET 3.5 with Enterprise Library 4.1. We have created an custom handler - UIExceptionHandler within our exe project. When we try to invoke the ExceptionPolicy.HandleException(e, "UI Message Policy"); method, we encounter the below [1] error.
Find below my code for
namespace CName.DName.AName.UI
{
[ConfigurationElementType(typeof(CustomHandlerData))]
public class UIExceptionHandler : IExceptionHandler
{
public UIExceptionHandler(NameValueCollection ignore)
{
}
public Exception HandleException(Exception exception, Guid handlingInstanceId) {
DialogResult result = this.ShowThreadExceptionDialog(exception);
return exception;
}
private DialogResult ShowThreadExceptionDialog(Exception ex)
{return MessageBox.Show("Some Error Message", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
Also find below the implementation of the custom handler in the app.config file
<
add name="UIExceptionPolicy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler" />
<add type="CName.DName.AName.UI.UIExceptionHandler, CName.DName.AName.UI" name="UI Message Handler"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
FYI - When I tried implementing the UIExceptionHandler custom handler class in an DLL and provided the full name in the app.config, it worked fine. But this fails when I create the handler within the exe project. The name of our exe project is Test.
What might be wrong here?
[1]
The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, UIExceptionPolicy]) failed: The type 'CName.DName.AName.UI.UIExceptionHandler, CName.DName.AName.UI.' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type ConfiguredObjectStrategy, index 2)
Regards,
Vipul
Regards,
Vipul Mehta
|
|
|
|
|
I don't think anybody will be able to answer your question. Try wrapping it around a bit so that it fits on the page.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I got this resolved.
Within the project properties of my exe project the Assembly name was set as Test. So within the UIExceptionHandler class, I had to change the namespace from namespace CName.DName.AName.UI to namespace Test and this resolved the issue
Regards,
Vipul Mehta
|
|
|
|
|
hai all
I want chinese characters to suggest in intelligence when typing.
An exe available from google named "googlePinYinInstaller.exe".
It will suggest chinese characters.
I want the idea behind this pinyin.
How i can do this in c#.
thanks in advance
|
|
|
|
|
How IMEMode of each control is used for this concept?
Any idea
|
|
|
|
|
Hi there! I've just released an Alpha 1 of a new .NET database migration toolkit and hope you'll find it interesting.
It's pretty much inspired by Ruby on Rails (namely, ActiveRecord Migrations). Most notable features are: a special DSL for writing migrations
migration "Oxite" revision => 1:
version 20090323103239:
oxite_Language:
LanguageID type => Guid, nullable => false, primary-key => true
LanguageName type => AnsiString, length => 8, nullable => false
LanguageDisplayName type => String, length => 50, nullable => false
index "" columns => [LanguageName, [LanguageDisplayName, desc]]
automatic version tracking, automatic generation of "downgrade" migrations and generally a very seamless experience while dealing with the DB.
Project is open-source (MIT License) and is available at http://code.google.com/p/octalforty-wizardby/[^]
|
|
|
|
|
A post about this technology is less effective than a fullblown-article; here it might even attract flames, since it's not a programmingquestion.
An article OTOH doesn't have to contain a question, but can also be used to introduce new idea's or frameworks. Plus they're easier to find through Google
I are troll
|
|
|
|
|