3 Replies - 1336 Views - Last Post: 19 November 2008 - 06:45 AM Rate Topic: -----

#1 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

C# Array issue

Posted 18 November 2008 - 10:23 PM

I usually am in the C++ forums but we are doing a project in C# for class and I am having some issues with an array inside a struct


Here is my struct declaration

public struct additionalQuestion
{
	public string strType;		 //Type of question checkbox or combobox
	public string strQuestion;	 //Question to be asked
	public string[] straQuestionStorage;
}



This is the code segment which is giving me an error. I get an error of: Use of possibly unassigned field 'straQuestionStorage'
newQuestion.straQuestionStorage[nIndex] = straDelimitedString[nIndex];



My guess is it has something to do with not really having a defined size for my array straQuestionStorage. Would i make some form of a constructor in the struct to define this? I have googled around but cannot find the answer to my question.

Thanks,

Topher

Is This A Good Question/Topic? 0
  • +

Replies To: C# Array issue

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: C# Array issue

Posted 18 November 2008 - 10:34 PM

Yeah. you need to create the array dynamically with new when you know the required size.
newQuestion.straQuestionStorage = new string[numElements];


If you're not married to the array, you could use one of the collection classes, ArrayList, or for .NET 2.0+ use List<string> so you can easily add strings to the collection dynamically.
Was This Post Helpful? 0
  • +
  • -

#3 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: C# Array issue

Posted 18 November 2008 - 10:36 PM

I figured it out.. used an arraylist and got it working... thanks for your help

This post has been edited by Topher84: 18 November 2008 - 11:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# Array issue

Posted 19 November 2008 - 06:45 AM

View PostTopher84, on 19 Nov, 2008 - 12:36 AM, said:

I figured it out.. used an arraylist and got it working... thanks for your help


If you are using .Net 2.0, use a generic list like List<string>. It's better on performance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1