.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

Trying to understand Array being reference types

Posted By:      Posted Date: April 14, 2011    Points: 0   Category :Windows Application

Below I'm pasting a simple application to roll 5 die 10 times and display each roll on screen.  the problem I'm having is that the output will look like this


3  4  6  3  1
3  4  6  3  1
3  4  6  3  1

but when i step through I different values for each roll

Here's the code


namespace DiceRoller
    class DiceManager
        private int numOfDice;
        private int[] diceRoll;

        public DiceManager(int numOfDice)
            NumOfDice = numOfDice;

        public int NumOfDice
            get { return numOfDice; }
            set { numOfDice = value; }

        public int[] DiceRoll
            get { return diceRoll; }
        // Roll dice and sets the diceRoll Array to values between 1 and 6.
        private void RollDice()
            diceRoll = new int[NumOfDice];
            Random rand = new Random();
            for (int i = 0; i < diceRoll.Length; i++)
                diceRoll[i] = rand.Next(1, 6);

        public void DisplayResults()
            foreach( int dieValue in diceRoll )
                Console.Write("{0}\t", dieValue);

View Complete Post

More Related Resource Links

The Reference.svcmap changes the types in my published functions

The WCF service has functions like: Public Function SaveCustomer(Byval dsCustomer as Entities.dsCustomer) as Boolean Implements ILSService.SaveCustomer Dim mgr as new DAL.Datamgr Return mgr.SaveCustomer(ds) End Function This was all fine until suddenly when I update the reference in the client. Suddenly the function above excepts another dataset instead. If I change the values in the reference.svcmap everything works as it should. But for every change to go through that is kind of a hassle. I tried to remove the datasets and make new ones for the one causing the trouble but then other functions become wrongly updated. Anyone have any clue where to start looking for the error? Best Regards Tomas

Deserializing a JSON array of mixed types



I'm having trouble deserializing a JSON array of mixed types using the DataContractJsonSerializer class. I've spent a bunch of time looking for a solution to no avail, so I thought I'd go ahead and ask here.

Basically, I am getting a JSON string like the one below. I'd like to get the array to deserialize into an List where position 0 has an Int32, position 1 has a String, and position 2 has an instance of my custom class.

"Mr. Smith",
"name":"main street",

Error: Object reference not set to an instance of an object for an array in vb.net


I have the following code

Dim ls_case_no() As String = Nothing
 Dim li_case_count As Integer = 0

  Dim li_case As String = ""
         li_case = xmlLocNode.InnerText
                                            If Not li_case = "" Then
                                                'li_case = li_case.Replace("-", "")
                                                ls_case_id = li_case

                                                ReDim Preserve ls_case_no(li_case_count)
                                                li_case_count = li_case_count + 1
                                                ls_case_no(li_case_count - 1) = ls_case_id
                                            End If

 myNode = xmlLocNode.SelectSingleNode("nc:CaseTrackingID", nsMgr)
                                             If Not myNode.InnerText = "" Then
                                                Dim li_case As String = myNode.InnerText
                                               lbl_caseno.Text = li_case
                                                ls_case_core = li_case
                                            End If

 If Not ls_case_no.Length = 0 Then
            For d = 0 To ls_case_no.Length - 1
                If Not ls_case_no(d) = ls_case_core Then

NullReferenceException in array? Object reference not set to an instance of an object.


I am receiving a NullReferenceException, but I can not find where it is.

The code:

string[] strAccount = null;
DateTime[] dtFirstPurchDate = null;
DateTime[] dtLastPurchDate = null;
TimeSpan[] dtFirstLastPurchDiff = null;


//Start iterating through the original gridview with line item totals
for (int i = 0; i <= GridViewECI.Rows.Count-1; i++)


if (i == 0)
//set first gridview records so we can use it to review next lines of data

                   strAccount[counter] = GridViewECI.Rows[i].Cells[0].Text;
                    dtFirstPurchDate[counter] = DateTime.Parse(GridViewECI.Rows[i].Cells[1].Text);

<-- error occurs here at dtFirstPurchDate with Object reference not set to an instance of object

Null Reference Exception while trying to initialise an Integer array


Hi ,

I am getting System.NullReferenceException:Object reference not set to an instance of an object

while executing the below code..!!

Dim arr() As Integer

Dim g,j As Integer



While g > 0
                arr(j) =0
                g = g - 1
                j = j + 1
End While

Could anybody help me out..!!!

Thanks in advance

A better way to reference your wizard steps using named steps

Note: this article uses the plain vanilla but the concepts apply equally well to its popular counterpart .

By far the most common way that I see wizard steps reference in code snippets is by their index.

SharePoint Tutorial - Content Types

SharePoint content types provide users with a way to manage and organize content in a more meaningful way. It's a reusable of settings you want to apply to a particular type of document in a library or item in a list.

UML Diagram Types

UML provides a number of diagram types as a mechanism for entering model elements into the model and showing overlapping sets of models elements and their relationships. UML does not specify what diagrams should be created or what they should contain, only what they can contain and the rules for connecting the elements. The diagram types include:

UML Diagram Types - 2

Further diagram types include:

Activity Diagrams - a generic flow chart used much in business modelling and sometimes in use case modelling to indicate the overall flow of the use case. This diagram type replaces the need for dataflow diagrams but is not a main diagram type for the purposes of analysis and design.

Ready made List Types provided by SharePoint

SharePoint provides some out of the box List Types. Some of the most used ready made list types that SharePoint provides currently are listed below:

Tuples, Anonymous Types, and Concrete Types

Tuples can save you a great deal of repetitive tasks. They can also lead to completely unreadable code in a language like C#. Tuples are data values that contain N items. The .NET framework supports tuples containing 1 to 8 members. To use tuples with more than 8 members, you use another tuple for the member of an octtuple

Content Types in MOSS 2007

In this article I will try to describe the one of the best feature in Microsoft office Share Point 2007 called Content Types.

Content types are simply collection of contents.Each content in a Content type can have different settings.Basically it is a reusable collection of settings which you want to apply to a particular category of contents.

Data Types - Date and Time in SqlServer

Date and time values can be stored with either the DATETIME or SMALLDATETIME data type. The difference between the two is that SMALLDATETIME supports a smaller range of dates and does not give the same level of precision when accounting for time. The DATETIME data type can hold values from January 1st of 1753 to December 31st of 9999. The time is stored to the 1 three hundredths of a second and each value takes up 8 bytes of storage. The SMALLDATETIME data type can hold values between January 1st 1900 and June 6th of 2079. The time is tracked to the minute and each value takes up 4 bytes of storage. The majority of business applications can live happily with SMALLDATETIME, however, if you are in an environment where each second matters or you need to make estimates to the distant future (or past) then you have to resort to DATETIME. If you fail to specify the time when inserting a value into a DATETIME or SMALLDATETIME column, a default of midnight is used. If you fail to specify the date portion the default of January 1, 1900 is used.

Different types of applications in WPF

With Windows Presentation Foundation coming closer to its release date with Vista almost being upon us, more and more people are taking a look at programming in WPF

Date and Time Data Types and Functions

The following sections in this topic provide an overview of all Transact-SQL date and time data types and functions. For information and examples that are common to date and time data types and functions

Frequently Asked Questions - SQL Server Data Types

This is good and precise introduction and Frequently Asked Questions - SQL Server Data Types
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend