Main Difference Between A Class And A Type And A ValueType

Dec 17, 2009

what's the main difference between a Class and a Type and a ValueType.and is a Delegate a Type? or an eventhandler/event (the actual event itself)is every object a type? or is it only true in VB

View 3 Replies


ADVERTISEMENT

Convert A String To Generic Type T When T Is A Valuetype?

May 1, 2012

fast way in VB to go from a string to a generic type T constrained to a valuetype (Of T as Structure), when I know that T will always be some number type?This is too slow for my taste:

Return DirectCast(Convert.ChangeType(myStr, GetType(T)), T)

But it seems to be the only sane method of getting from a String --> T. I've tried using Reflector to see how Convert.ChangeType works, and while I can convert from the String to a given number type via a hacked-up version of that code, I have no idea how to jam that type back into T so it can be returned.

I'll add that part of the speed penalty I'm seeing (in a timing loop) is because the return value is getting assigned to a Nullable(Of T) value. If I strongly-type my class for a specific number type (i.e., UInt16), then I can vastly increase the performance, but then the class would need to be duplicated for each numeric type that I use.

It'd almost be nice if there was converter to/from T while working on it in a generic method/class. Maybe there is and I'm oblivious to its existence?

Conclusion:Testing the three provided implementations below and my original DirectCast/ChangeType form, @peenut's approach of using a prepared delegate to fetch the Parse method from a basic type works. No error checking is done, however, so implementors need to remember to only use this with valuetypes that have a Parse method available. Or extend the below to do error checking.

All runs were done on a 32bit system running Windows Server 2003 R2 with 4GB of RAM. Each "run" is 1,000,000 executions (ops) of the method to be tested, timed with StopWatch and reported back in milliseconds.

Original DirectCast(Convert.ChangeType(myStr, GetType(T)), T):

1000000 ops: 597ms
Average of 1000000 ops over 10 runs: 472ms
Average of 1000000 ops over 10 runs: 458ms

[code]....

Comparatively, peenut's approach is almost 200ms faster when executed 1,000,000 times in a tight loop, so his approach wins out.

View 32 Replies

.net - Faster Way To Convert From A String To Generic Type T When T Is A Valuetype?

Jan 6, 2011

Does anyone know of a fast way in VB to go from a string to a generic type T constrained to a valuetype (Of T as Structure), when I know that T will always be some number type?This is too slow for my taste:

Return DirectCast(Convert.ChangeType(myStr, GetType(T)), T)

But it seems to be the only sane method of getting from a String --> T. I've tried using Reflector to see how Convert.ChangeType works, and while I can convert from the String to a given number type via a hacked-up version of that code, I have no idea how to jam that type back into T so it can be returned.

I'll add that part of the speed penalty I'm seeing (in a timing loop) is because the return value is getting assigned to a Nullable(Of T) value. If I strongly-type my class for a specific number type (i.e., UInt16), then I can vastly increase the performance, but then the class would need to be duplicated for each numeric type that I use.

It'd almost be nice if there was converter to/from T while working on it in a generic method/class. Maybe there is and I'm oblivious to its existence?

Conclusion:Testing the three provided implementations below and my original DirectCast/ChangeType form, @peenut's approach of using a prepared delegate to fetch the Parse method from a basic type works. No error checking is done, however, so implementors need to remember to only use this with valuetypes that have a Parse method available. Or extend the below to do error checking.

All runs were done on a 32bit system running Windows Server 2003 R2 with 4GB of RAM. Each "run" is 1,000,000 executions (ops) of the method to be tested, timed with StopWatch and reported back in milliseconds.

Original DirectCast(Convert.ChangeType(myStr, GetType(T)), T):

1000000 ops: 597ms
Average of 1000000 ops over 10 runs: 472ms
Average of 1000000 ops over 10 runs: 458ms

[code]....

Using System.Reflection and calling InvokeMethod to get at the Parse method:

1000000 ops: 12213ms
Average of 1000000 ops over 10 runs: 11468ms
Average of 1000000 ops over 10 runs: 11509ms
Average of 1000000 ops over 10 runs: 11524ms

[code]....

Konrad's approach to generate IL code to access the Parse method and store the call into a delegate:

1000000 ops: 352ms
Average of 1000000 ops over 10 runs: 316ms
Average of 1000000 ops over 10 runs: 315ms

[code]....

peenut's approach of using a delegate to access the Parse method directly:

1000000 ops: 272ms
Average of 1000000 ops over 10 runs: 272ms
Average of 1000000 ops over 10 runs: 275ms

[code]....

Comparatively, peenut's approach is almost 200ms faster when executed 1,000,000 times in a tight loop, so his approach wins out. Although, Konrad's wasn't far behind and is itself a fascinating study of things like ILGenerator.

View 4 Replies

Main Difference Between Programming In Java And Vb?

Nov 5, 2010

what is the main difference between programming in java and vb. i am interested in database development.A friend of mine is far better than me adviced to move over to java. His reasons include the structure and reiability

View 6 Replies

Pass A Nullable Valuetype Into A Non Nullable Valuetype?

Sep 17, 2009

I accidently wrote some code today that was like this[code]...

I immediately noticed the issue, but I had already hit the run button. It compiled successfully, I ran it through to the section and it threw an exception.

You can't do this in C#, it gives a compile error "cannot convert from 'int?' to 'int'".

Is there an 'Option Explicit' type switch that I can turn on to ensure that this sort of error does not occur again?

View 1 Replies

Can't Make Difference Between Public Class And Private Class And Friend And Protected Friend Class

May 15, 2009

I can't make difference between public class and private class and friend and protected friend class.

View 1 Replies

Class Has Been Create By Main Class Only?

Feb 27, 2011

I'm try to create a class has been create by main class only by protect sub New but how do I create it ?

Public MustInherit Class Rune
Protected MustOverride Function Create() As Rune
Public Shared Function Factory(Of T As Rune)() As T
'I can't use "T As {Rune, New}" because Sub New isn't Public
'Return (New T).Create
End Function
End Class

[Code]...

View 2 Replies

C# - Get The Type For A Class By Sending Just The Name Of The Class Instead Of The Class Itself As The Parameter?

Sep 11, 2009

Dim a as Type=GetType(className) would gimme the type. But I have only the name of the class as string. I want something like GetType("class1") which would return the type.

View 2 Replies

.net - Difference Between Declaring And Constructing A Value Type Object?

Oct 2, 2009

I've been working in .NET for some time now, but occasionally I still get confused by a disparity between the framework and my prior experience in C++.In .NET, all objects are either value types or reference types. Reference types are allocated on the heap while value types are allocated on the stack (in the current CLR implementation, anyway). I get that. However, at least in VB.NET, you can still define a constructor on a value type.

Public Structure Coordinates
Public x As Integer
Public y As Integer

[code]....

View 3 Replies

How To Call A Class From A Main Form

Mar 5, 2010

How would you call a class from a main form? In VB6 you would call a module by just simply calling the module name.

View 2 Replies

How To Refer To Main Window Class

Sep 6, 2011

I created a modal dialog as a WPF window that changes some settings in the main window application. How can I refer to a member of the main window?

View 2 Replies

C# - Difference Between MustInherit And Abstract Class?

Mar 4, 2011

explain to me the differences between an abstract class and a class marked MustInherit?

Both can implement shared and instance constructors and logic. Both can/must be inherited.

So why use one over the other and what is the difference?

Edit: Sincerely apologize I have got my languages mixed up. Will mark correct answer as soon as I am allowed.

View 2 Replies

Difference Between Private Sub / Function And Class

Oct 30, 2010

What is the difference between Private Sub /private Function / Private Class and to use them?

View 4 Replies

Difference Between Timer Class And Object?

Nov 25, 2010

I'm using vb10. I need to use a timer for forms to turn off and on and I was going to use a timer control and I noticed there is a timer class. What is the difference? Should I use the timer class instead?

View 5 Replies

Difference Between Using DataContext Class And SqlConnection?

Mar 2, 2012

I asked a question earlier where I was told a simple way to "bind data to objects" is to just run a SqlConnection(connectionString). The response also included a comment saying I could get fancy with L2S and Entity Frameworks, so I looked deeper into those. It seems all you have to do with the DataContext object is point to the database. Why would SqlConnection be a benefit? What is the difference (or pros/cons) of using either one of these? Is one more "standard"? Is one more modern?

View 1 Replies

Unable To Cast COM Object Of Type 'System.__ComObject' To Class Type?

May 14, 2009

Unable to cast COM object of type 'System.__ComObject' to class type 'rjsDocMan.Folder'.Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.The code where it is flagging the error is:

<div style="color: Black; background-color: White;">
treeview += AddChildren(Session("DocMan").RootFolder)
</div>

[code].....

View 4 Replies

Class Not Registered While Loading Main Form

Apr 1, 2009

We have a Dell laptop used to run an in-house VB.NET program. It got a virus. We had to wipe the hard drive and reinstall Windows from scratch. We use Windows XP Pro, which is what was installed on it before. But now our VB program won't run. After trying and despairing of getting remote debugging to work, I actually installed Visual Studio 2005 on the machine, just long enough to get information about what was making the program fail. And the failure was most peculiar. It isn't in our code. It's in the VB setup code that loads the main form. And it says, "Error loading form: class not registered."The gory details are given below. Now here's the strange thing. Not every VB.net program fails on this computer. And the program that fails, fails only on this computer; it has run successfully on several other Windows XP systems with .NET Framework 2.0 installed.[code]

View 3 Replies

Set And Get A Value From A Class Property Within The Main Event Of A Module?

Jul 8, 2011

the project uses a submain as startup object which is located in a module, thy so the application will be started in tray. Within the main() event i need to deserialize a xml file and with it set the properties of a class and then set what the application will do.The problem is, after I deserialize it and put set the property values I try to get them in the same event(main) but when it gets the value it returns as nothing(if string) or 0(if integer) but in the debugging i saw it setting the values.Is there a way to do that, within the main() event of a module to set the properties of a class and then get them without being nothing/0 ?

View 6 Replies

Use The Update In The Main Class To Access The List?

Jan 15, 2012

i need to use the update in the main class to access the list in the footballadmin and then display each in a list box but i cant get it to work.

Imports Football
Public Class FootballAdmin
Private fTeam As List(Of FootballTeams)

[Code]......

View 10 Replies

VS 2008 Can Call A Class From Main Form

Dec 14, 2011

As of the moment I have 24 buttons that are keys to a piano.24 of the button codes have a call method that calls the note to be played.24 of the private subs that are called have an .wav audio playback command within them. I was hoping I could move the .wav private subs into a separate class, and just call them from my main form, therefore cleaning up the lines of code I have at the moment. [code]That example only counts for one Button "The Key of C", and the audio file that is called with it. Multiply that by 24 and you get a big mess of repetitive junk.Can I move the audio subs into a class and call them from my main form?

View 16 Replies

Winforms - Serialize The Main Class In My VB Solution?

Apr 5, 2011

I'm trying to serialize the main class in my VB solution. I've added the Serializable attribute at the top of my class like so:

[code]...

I'm guessing this is because you can't serialize the form that is attached to the class or something, but I really don't know what I'm doing. Can I serialize all of the objects contained in my Form1 class somehow, without getting this error? I don't want to store any data about the form controls, I just need to save all the objects that I've defined at the top of the Form1 class.

View 1 Replies

.net - Get A Base Class Method Return Type To Be The Subclass Type?

Nov 23, 2009

I have a copy function that I'd like to override in subclasses to return the type of the subclass. Here are my interfaces:

Public Interface IBase(Of T)
Function Copy() As T
End Interface

[Code]....

View 4 Replies

Difference Between Inheriting And Creating Object Of A Class?

Jun 29, 2010

When we can access the public methods via object then why is there need of inheritance.

View 2 Replies

Difference Between These Two Initialization Methods For A Class Member

Nov 4, 2011

Whats the difference between these two initialization methods for obj? I've seen both of these, but know know if there is an appropriate time to use one vs the other. I found this post which covers C#, but not sure if the same applies to VB.Net.[code]

View 3 Replies

VS 2008 - Difference Rijndael Vs RijndaelManaged Class

Aug 6, 2009

What's the difference? I'm going to create a password encrypting program, and I wanted to know what's the basic difference about the managed class and the "not managed" class.

View 4 Replies

.net - Can't Set 'Value' Of A Custom ValueType?

Apr 30, 2012

I have created a custom ValueType:

Private Structure MyValueType
Private _integerValue As Integer
Public Sub New(initValue As Integer)

[code]....

But I can't work out how I can test the value such as this:

Dim v As New MyValueType(3)
Dim x As New MyValueType(4)

[code]....

Error:Operator '=' is not defined for Types MyValueType and MyValueType

So how do I define Operators for my ValueType (I know this must be simple but I can't find an exmaple anywhere!)?Note I don't want to test If v.Equals(x)

View 1 Replies

How To Constrain To A ValueType

Jun 22, 2011

I've just spotted this thread:>> [URL] Here is a C# article on the subject:>> [URL] how to constrain to a ValueType?

I,E:
Public Class ExampleClass(Of T As System.ValueType)
End Class

View 10 Replies

Accessing Function In Alother Class From The Main Form?

Feb 12, 2011

I have a new solution with 5 projects inside. a data access layer, database, 2 class libraries, and the main form. These are all components that I had worked on to get to this point of combining into 1 solution. Now what I have run into is I can not the actual functions in the class libraries. I can get to the class level, but that is it. ere is a sample line of code I need to get working. Once I get this one line, the rest will be obvious: Temperature is on the main form. get for options is "Equals, GetHashCode, GetType", etc... Before this I had bult, compiled, and referenced these classes separately, and it worked fine. I know this is going to be an easy answer, I'm just not seeing it for some reason.

View 15 Replies

Create A Sub Main Inside A Class Which Is Itself Part Of A Project

May 17, 2009

why, when i create a sub main inside a class which is itself part of a project, i need to create it as a shared sub like this.[code]

View 2 Replies

Declare Main Class On FrmMain - Working With Classes?

Jun 3, 2009

Currently I declare my main class on frmMain. as public MyClass as new clsMyClass Thus anywhere in the application I would address it as: frmMain.MyClass.Function(MyParam) This does not look as neat as I would like it. Where can I load this Class so that I can address it as: MyClass.Function(MyParam) from anywhere inside the application.

View 6 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved