Click here to Skip to main content
15,887,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

This is my method which returns 3 values in a list and is working fine:

class A.
C#
public static List<string> ABC()  {
  return  _lstValues;
 }



From windows form on button_click ,I am trying to call the ABC() :
C#
List<string> ListValues = ClassA.ABC();


But I am getting error:
C#
ex = {"The type initializer threw an exception."}


Can anyone please help me to fix this. Am I calling the function in a correct way?


Thanks.
Posted
Updated 15-Oct-14 3:26am
v2
Comments
Sinisa Hajnal 15-Oct-14 9:19am    
You have Class A and calling ClassA

By the code above you should call A.ABC()
[no name] 15-Oct-14 9:21am    
A List declaration requires a type such as List<string> is a list of strings.
Thomas Daniels 15-Oct-14 9:27am    
The type is actually provided, it just was considered as a HTML tag by the system, because it was not wrapped well in code blocks. I fixed that in my edit.
BillWoodruff 15-Oct-14 9:25am    
We are, unfortunately, not psychic: the code you show will not compile and is incomplete. _lstValues is not defined ... you are using a Generic List with no Type argument in <> brackets. Please correct these obvious errors and insert the missing pieces of the puzzle.
BillWoodruff 15-Oct-14 10:40am    
Still cannot see where the definition of Class A is, or where the value of _lstValues comes from. In a comment here you have one method 'CheckDataAccess, in another comment you have 'GetPersons ... how is anyone supposed to know how these are used ?

Please incorporate the code shown in your comments into your original post, and revise the original post so it's clear what's going on.

Maybe you have to initialize _lstValues before call it like :

C#
private List<string> _lstValues = new List<string>();


fill it with the values you will use and pay attenction to static keyword , in this context could have side effect.
Hope help.
 
Share this answer
 
v2
I made a little changes to your code, and came up with this fiddle. https://dotnetfiddle.net/ylc2Dx[^]

The code was now,

C#
// create the method, that returns List<string>
public static List<string> ABC()  {
  // initialize new list, and return that list
  List<string> list = new List<string>();
  // add three values
  list.Add("First value");
  list.Add("Second value");
  list.Add("Third value");
  // return the list
  return list;
}

// upon calling, it will return that instance of the list 
// created inside the method
List<string> newList = ABC();

// check the Type of the object, it is "System.Collections.Generic.List`1[System.String]"
Console.WriteLine(newList.ToString());


This now ensures that your new object will be created, three values would be added, then the list would be returned, and by the way, why are you returning an object, in this way, whereas it will be easy to write it this way

C#
List<string> list = new List<string>();
// add three (or more; or less) values to it here, using .Add() method


Any specific reason behind this brother?
 
Share this answer
 
v4
1 down vote


If I understand your request correctly, you have to do the following:
C#
public class Section 
{ 
    public String Head
    {
        get
        {
            return SubHead.LastOrDefault();
        }
        set
        {
            SubHead.Add(value);
        }

    public List<string> SubHead { get; set; }
    public List<string> Content { get; set; }
} 

You use it like this:
C#
var section = new Section();
section.Head = "Test string";

Now "Test string" is added to the subHeads collection and will be available through the getter:

C#
var last = section.Head; // last will be "Test string"

Hope I understood you correctly.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900