Avoiding Boxing / Unboxing On Unknown Input

Feb 25, 2011

I am creating an application that parses an XML and retrieves some data. Each xml node specifies the data (const), a recordset's column-name to get the data from (var), a subset of possible data values depending on some condition (enum) and others. It may also specify, alongside the data, the format in which the data must be shown to the user.

The thing is that for each node type I need to process the values differently and perform some actinons so, for each node, I need to store the return value in a temp variable in order to later format it... I know I could format it right there and return it but that would mean to repeat myself and I hate doing so. So, the question: How can I store the value to return, in a temp variable, while avoiding boxing/unboxing when the type is unknown and I can't use generics? P.S.: I'm designing the parser, the XML Schema and the view that will fill the recordset so changes to all are plausible.

[Code]...

View 1 Replies


ADVERTISEMENT

C# - How Is The Boxing/unboxing Behavior Of Nullable<T>

Sep 23, 2010

Any variable of type Nullable<T> can be assigned to null. For instance:

int? i = null;

At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T>:

public static implicit operator Nullable<T>(object box);

But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it isn't):

int? i = new object();

Then I realized that perhaps the Nullable<T> type could define an implicit conversion to some arbitrary reference type that can never be instantiated, like this:

public abstract class DummyBox
{
private DummyBox()
{ }

[code]....

But this seems to imply that there is a custom implicit conversion from Nullable<T> to object:

public static implicit operator object(Nullable<T> value);

This is clearly not the case as object is a base class for all types, and user-defined implicit conversions to/from base types are illegal (as well they should be).It seems that object x = i; should box i like any other value type, so that x.GetType() would yield the same result as typeof(int?) (rather than throw a NullReferenceException).

So I dug around a bit and, sure enough, it turns out this behavior is specific to the Nullable<T> type, specially defined in both the C# and VB.NET specifications, and not reproducible in any user-defined struct (C#) or Structure (VB.NET).This particular boxing and unboxing behavior appears to be impossible to implement by hand. It only works because both C# and VB.NET give special treatment to the Nullable<T> type.

Isn't it theoretically possible that a different CLI-based language could exist where Nullable<T> weren't given this special treatment? And wouldn't the Nullable<T> type therefore exhibit different behavior in different languages?How do C# and VB.NET achieve this behavior? Is it supported by the CLR? (That is, does the CLR allow a type to somehow "override" the manner in which it is boxed, even though C# and VB.NET themselves prohibit it?)Is it even possible (in C# or VB.NET) to box a Nullable<T> as object?

View 2 Replies

Changing Unknown Number Of Label Captions Using Unknown Length Array

Oct 22, 2009

Using Access 2003 and VB code, I have created a form with a number of labels. The labels will display an employee's names. I have named the labels name1, name2, name3...etc. Using an ADO data set and DCount I have determined the number of names in an Access table (the number of names can vary). What I want to do is for each record in succession to pick the first name and surname from the data set , combine them into a full name (this part has been successful), and then set the full name into the label caption in succession. Example: full name 1 into name1, full name 2 into name2, full name 3 into name3... until all names appear as labels on the form.

I created a counter to and a variable to increment the label number but when I address the caption property of the variable containing the new label (e.g. name1, name2..etc.) it throws an errer at runtime. Here is the code:

[Code]....

View 8 Replies

C# - Any Boxing With A Big List Of Booleans?

May 7, 2011

I need a list of booleans (size anywhere from 200 to 200k). If i use System.Collections.Generic.List<bool> I'm going to suffer from serious boxing issues (correct me if I'm wrong). What are the alternatives? (of course i know i can use a boolean array but i need to be able to add and remove things easily from the array)

[Code]...

View 2 Replies

Avoiding ListView Duplicates

Jan 19, 2009

Im adding listviewitems and want to avoid duplication. Now currently, I have resorted to looping through the listview and comparing each items text property against what Im adding. But for a big table this is very slow. The ListView has a .Contains Method but this does not seem to do what I would have assumed; for example [code]Is there a way of doing this so I dont have to loop through all the items?

View 6 Replies

Avoiding Multiple IF Statements?

Sep 14, 2010

I have the following code block. I'm tired of typing the same IF statement to check if the string has any lenght before assignning them to the class properties of Employee.

Is there any better to accomplish this?
Dim title = txtTitle.Text.Trim
Dim firstName = txtFirstName.Text.Trim

[code].....

View 6 Replies

Generic DAL Avoiding Reflection?

Oct 13, 2010

I'm working on a new DAL for my latest project. As the performance is essential I'm avoiding late binding/reflection as much as possible. One central part is the DAL function for populating Business Objects.One class is called in order to connect to the database with the given SQL (stored procedures can't be used in this environment) and the given Business Object or List is populated. As different Business Object types will be populated by the same function I made an Interface that all of the Business Objects implements with the Sub called "Fill". The question is - will reflection be used in this scenario? My thought was as I'm "describing" the objects trought the interface the compiler doesn't need to use Reflection.

[Code]...

View 3 Replies

Does The .NET "If" Operator Cause Boxing?

Mar 9, 2010

Those of us who've worked in VB/VB.NET have seen code similar to this abomination:

Dim name As String = IIf(obj Is Nothing, "", obj.Name)

I say "abomination" for three simple reasons:IIf is a function, all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#.Because IIf is a function, it thus incurs the overhead of a function call. Again, though this isn't a big deal, it just doesn't feel right to someone who expects for it to behave as a ternary operation intrinsic to the language.IIf is non-generic and therefore accepts parameters of type Object, which means the following call boxes (I believe) a total of three integers:

' boxes 2nd and 3rd arguments as well as return value 'Dim value As Integer = IIf(condition, 1, -1)

Now, in some more recent version of VB.NET (I'm not sure what the number is), the If operator was introduced, which works exactly the same way as the IIf function but (as I understand it) without the same shortcomings. That is to say, it does provide short-circuiting and it is an intrinstic VB operation. However, I'm not sure about the last part. The MSDN documentation doesn't seem to indicate whether If boxes its arguments or not.

View 2 Replies

Avoiding 'Microsoft Excel' Prompt

Feb 15, 2012

I'm writing the following bits of code for a SaveFile Dialog box in Visual Basic 2010.NET. The code works well as the XLSX file is created and opens in Microsoft Excel. However, the problem that I am having is that if the program overwrites a file, the user is getting two prompts. One prompt is from the program's own SaveFile dialog confirming the choice. The other is from Microsoft Excel asking you the same. It's as if the program is running in the background.[code]

View 1 Replies

Avoiding Creation Of SQL Queries In DataSet

Sep 26, 2011

I want to avoid the creation of SQL queries inside the dataset. Here's the screenshot:

Dim search_by_dateofcanvass = sqlcomm.CommandText = "SELECT [#], Material_Name, Brand, Unit, Discount, Supplier, Category, Date_of_Canvass, City, ListPrice, Material_Description, NetPrice, Area FROM CanvassTable WHERE (Date_of_Canvass = " & DTPcanvass.Text & ")"
Me.CanvasstableTableAdapter.search_by_dateofcanvass(Me.FinalDatabaseDataSet.CanvassTable, datepick)

By the way that query can be use to search for date.

View 3 Replies

Avoiding Double Data In Datagridview?

Sep 16, 2011

If dr("iname").ToString = DataGridView1.item(0).cells.item(0).valueThen Then
MsgBox("Similar items are not allowed.", MsgBoxStyle.Critical)
Exit Sub
End If

I need to avoid displaying similar data from my mysql database table iname to the gridview, but I can't identify the right syntax/code for that?

View 1 Replies

Avoiding Duplicate Values In Database?

Jun 21, 2010

I want to insert number for City_Id and City_name , i wrote a query for auto generate number for City_Id,working fine but in the city name i want to avoid inserting duplicate value like if Mumbai is a City ihave already saved then system should not allow that name again. neither in small or in capital or mixed letter.

View 4 Replies

Avoiding Namespace Declaration In Each File?

Sep 25, 2009

One feature of C# compiler is that namespace declaration by default follows the folder name structure in the project. By e.g. you have MyProject1MyDrawing and inside you create a class MyPoint the namespace for MyPoint will be MyProject1.MyDrawing

In VB I cant see any namespace declaration(apparently it implicitly 'inherits' the project(MyProject1) namespace). If I create a MyPoint and MyLine clases in the MyDrawing folder I need to surround each of created classes with "Namespace MyProject1.MyDrawing"..."End Namespace".

Is it possible to automatically name the namespaces like in C# by the folder name?

View 10 Replies

Avoiding Referential Integrity While Updating

Jul 4, 2009

I have an MS Access database and an MS Visual Basic 2008 front end.I get this error when trying to update a record that has related records:The record cannot be deleted or changed because table 'X' includes related records.I only want to update one field in the row in the 'parent' table which is not the primary key field of either record but the 'Update' command seems to want to rewrite the whole datarow. Rewriting the whole row causes the error (as this updates the primary key). If I switch off refrential integrity checking in the underlying database this solves the problem except that I want to maintain referential integrity so its not really a solution. I have tried beginEdit and EndEdit with no success - same error.Is there a way to update just one field in a row and write that back to the database?

View 2 Replies

Avoiding The Video Source Window?

May 17, 2012

I need the Logitech HD Webcam C310 to be preselected.When the Main Form opens the Webcam must be running allready, withoute having to select it from the Video Source Window.

View 1 Replies

Avoiding Updating A Form From A Thread?

Sep 8, 2009

I've seen a good bit written on avoiding updating a form from a thread.However,I was wondering if that was a problem if the thread exists in an object that programatically builds the form?Thus the form and associated logic exist within the same "thread space." Seems on the face of it like it would work since everything in the form should be in a single dispatch queue.

View 8 Replies

Forms :: Avoiding Clashing Functions?

Jul 26, 2010

This particular function is used to save the returned results for a ping sent across the internet. Unfortunately the code won't let me Use the streamwriter method or the fileopen method, because it says:"The process cannot access the file 'C:UsersChrisDocumentsPinging DataFilesave2.txt' because it is being used by another process."

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
FileOpen(1, "C:UsersChrisDocumentsPinging Datacount.txt", OpenMode.Input, OpenAccess.Read)

[code].....

View 1 Replies

[2008] Avoiding The Requirement Of .net Framework?

Jan 31, 2009

i get a lots of errors with my application because users doesn't have the .Net Framework installed so i was thinking, can't i take every .net framework resource that my application uses and needs, and add it as a reference in my application so that the user does not need to install .Net Framework?

View 6 Replies

.net - Avoiding Overlapped Bookings In Booking System Application?

Aug 22, 2011

If a booking is present in database on 12/27/2011 at 5:00 pm for 2 hours and i try to make a new booking on 12/27/2011 between 5 to 7 pm then my code generates an error message. Even if i try to book at 4:00 pm for 2 hours it generates an error message as the 2nd hour will be overlapped here with already made booking from 5:00 to 7:00.

Now here comes the problem part. When day changes it doesn't generate error message i.e. IF a booking is there on 12/27/2011 at 11:00 pm for 3 hours then it should not allow new booking till 12/28/2011 2:00 am but when i try to book 12/28/2011 at 1:00 am it saves it in the database and doesn't generate an error message. I want an error message generated in such a case.

I am using two separate fields in database one for time and one for date. Both of them have DateTime datatype.

newtime refers to time on which i'm trying to make new booking addednewtime refers to time after adding the duration to time on which i'm trying to make new booking

[Code]...

View 1 Replies

.net - Elapsed Time With Environment.TickCount() - Avoiding The Wrap

Apr 16, 2009

Does the absolute value protect the following code from the Environment.TickCount wrap?

[code...]

View 5 Replies

Avoiding Cross Process Calls When Doing Word Automation

Oct 5, 2010

I've got a Word Addin in VB.net and VSTO that exposes a COM compatible object via Word.COMAddins.Object, so that the addin functionality can be called External to Word, without accesses to Word itself being cross-process.The technique worked in VB6, but with VB.net, it still works, but it's much slower than the same code running directly from the addin via a task pane, as if the calls are all cross process when they shouldn't be.Anyone who's ever done Word automation knows that code that runs perfectly acceptably INPROC with Word (in this case the instance of the ADDIN that Word itself loads), will generally run unacceptably slowly out of process (or cross process).[code]And slow as in a factor of about 10 to 1; what takes 3 seconds to run when run directly from the ADDIN via the task pane, takes ~30seconds to run when called from external code through the COMADDIN.object object. I'm guessing that I'm running into some sort of issue with .net APPDOMAINS or something and what +really+ constitutes cross proc calls in .net, but I've found nothing so far that would even hint about this sort of thing.My next step, barring some mystical insight, will be to code up a repro, which could get tricky because of the shear number of elements in play.

View 1 Replies

Avoiding Exception Of Request QueryString If Parameter Not Passed?

May 2, 2012

If the parameter variable is not passed then this code throws an exception of "Conversion from string "" to type 'Date' is not valid."

This is my code.
Public Shared ReadOnly Property Request_projectStartDate() As Date
Get
Dim _value As String = Nothing
[Code] .....

View 2 Replies

Avoiding Loading Of Form When Experimenting With Debug.Write?

Jun 17, 2011

loading of form when I experiment with Debug.Write code?I deleted the form,placed code in a standard module before running the program but am getting an error message.With the form existing I get what I want but I just don't need to wait for the form to load and then to click cancel to close it.Is it not possible to have just a standard module in a VB 2010 Express project? If not can't I place the code in form1.vb but stop it from loading every time I test something?

View 10 Replies

Avoiding Microsoft Excel Prompt - User Is Getting Two Prompts

Feb 7, 2012

I'm writing the following bits of code for a SaveFile Dialog box in Visual Basic 2010.NET. The code works well as the XLSX file is created and opens in Microsoft Excel. However, the problem that I am having is that if the program overwrites a file, the user is getting two prompts. One prompt is from the program's own SaveFile dialog confirming the choice. The other is from Microsoft Excel asking you the same. It's as if the program is running in the background. Does anyone know how I can write this code to save the file as an Excel file? [Code]

View 1 Replies

Avoiding Print Dialog When Printdialog() Called For .rdlc?

Mar 4, 2011

I am using Printdialog() to get my report printed (.rdlc)I want to avoid this dialogue and send the report directly to the printer without user interference.

View 1 Replies

C# - Avoiding Deadlocks And TimeOuts When Processing Huge Data?

Oct 14, 2010

I have code in an ASP.NET form that needs to, depending on user entry create messages in the database. We are speaking of potentially thousands of db entries. How do I protect against deadlocks, I mean apart from using Transactions and setting IsolationLevel to Serializable, as well as using WITH(NOLOCK) statement on my select statements since I don't mind a dirty read.

[Code]...

View 1 Replies

Avoiding Flicker When Drawing Multiple Bitmaps With Graphics.DrawImage

Mar 15, 2011

I need to draw a bitmap background with one or more moving bitmaps in the foreground; something like a chessboard where pieces move across it. I use a timer-tick event to redraw the background and then draw the relocated foreground image like the code below. But I get flicker, even though I set the form to use double-buffering. The culprit seems to be redrawing the background to refresh where the foreground images used to be; no flicker if I replace the background DrawImage with a simple Graphics.Clear. I think what I want to do is draw the background, and the foreground bitmap(s), in memory and then render it all. I used to do this with double-buffering, BitBlt, and .Refresh in VB6 but I haven't found the equivalent in VB2010. This must be a pretty common graphics requirement; what's the trick to it?

Dim Gfx As Graphics
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
Dim rectBrd As Rectangle 'use as clipping region for drawing

[Code].....

View 11 Replies

Multithreading - .net Avoiding Cross Thread Exception With Extension Method?

Jan 13, 2011

I am trying to implement a solution for updating form controls without using a delegate.

Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public Module MyInvoke

[code]....

I get a cross threading error as though i didnt even use this method:Cross-thread operation not valid: Control 'ComboBox1' accessed from a thread other than the thread it was created on.

View 3 Replies

VS 2010 - Input Validation To Verify User Selected Input For 'cb Length Of Stay'

Jan 15, 2011

I need some help with the input validation of my application. Attached is the code that I have written as well as a screenshot of the front end. I specifically need input validation to verify that the user selected input for cbLengthOfStay is a number between 1-10 as well as input validation to verify the user input for txtMedication, txtSurgicalCharges, txtLabFees and txtRehabilitation is a positive numerical number. Please feel free to critique my attached code. [Code]

View 2 Replies

Creating User Input With Textbox.text Input Then Displaying In Messagebox?

Feb 19, 2012

If my Category and Forum selection is incorrect I am sorry and feel free to move it. I'd like to have the users input in the message box that will show. For example: There is a textbox and the user puts in their name as Bob. Then the message box would be: What my main goal is, is being able to include the users input they type inside the textbox inside the message box.

View 6 Replies







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