1

I am new in C# and would like to know if it's possible to create an array in C# like following:

rates[0]['logic_id'] = 12;
rates[0]['line_id'] = ""
rates[0]['rate'] = rateVal;
rates[0]['changed'] = isChanged;

rates[1]['logic_id'] = 13;
rates[1]['line_id'] = ""
rates[1]['rate'] = secvalue;
rates[1]['changed'] = isChanged;

Can I create rates array in C# with such values?

EDIT:

My goal is to send rates array to a specific Web API service that is running with PHP. They accept only array with abovementioned structure. That's why I want to achieve that.

23
  • 2
    That object notation doesn't exist in C#. Whats so bad about creating proper objects? Commented Nov 7, 2014 at 18:07
  • 1
    Wouldn't you rather have it strongly typed and create a Rate object? Commented Nov 7, 2014 at 18:07
  • 2
    types in C# exist for a reason... Commented Nov 7, 2014 at 18:08
  • 3
    This appears to be an XY Problem. You want to create an array to solve some problem, but the problem you haven't described. Unless you are literally using SO to understand collections (arrays, enumerables, etc) it would be in everyones best interest if we understood what you think an array would solve for. Commented Nov 7, 2014 at 18:11
  • 1
    @Oraz you already have your answer below... Commented Nov 7, 2014 at 18:15

4 Answers 4

4

The best approach here would be to create a Rate class that is held in a List<Rate>().

public class Rate
{
    public int LogicId { get; set; }
    public string LineId { get; set; }
    public decimal Rate { get; set; }
    public bool IsChanged { get; set; }
}

public void Populate()
{
    var rates = new List<Rate>();

    var rate = new Rate();
    rate.LogicId = 12;
    rate.LineId = string.Empty;
    rate.Rate = 0;
    rate.IsChanged = true;

    rates.Add(rate);
}

To access the values, you can loop through them:

foreach(var rate in rates)
{
    //Do something with the object, like writing some values to the Console
    Console.WriteLine(rate.LogicId);
    Console.WriteLine(rate.Rate);
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 - You should probably note how to access the values too.
@Dave Zych if send `rates' as a List to Web API, how can they separate the values. For example, get LogicID and LineID separately?
@Oraz to send this to an API you'll have to serialize it in some way, i.e. convert it to XML (if the API accepts XML) or JSON (if it accepts JSON). JSON is the more common, and you can use the Newtonsoft.JSON library to convert the list to a json array. The code is similar to string jsonForApi = json.Serialize(rates);
@DaveZych I've added reference to Newtonsof.Json library, but I cannot find any Serialize method for Json that takes List as a parameter. Can you help please? I am dealing with Windows Service in VS 2010 with .Net Framework 4.0.
I found it: var json = Newtonsoft.Json.JsonConvert.SerializeObject(rates)
2

You could solve it using arrays, but it's someway outdated the approach.

My suggestion is that you should use a List<Dictionary<string, object>>:

var data = new List<Dictionary<string, object>>();

data.Add(new Dictionary<string, object>());

data[0].Add("rate", rateVal);

And later you can access it like JavaScript using dictionary's indexer:

var rate = data[0]["rate"];

Update

OP said:

My goal is to send rates array to a specific Web API service that is running with PHP. They accept only array with abovementioned structure. That's why I want to achieve that.

No problem. If you serialize that list of dictionaries using JSON.NET, you can produce a JSON which will contain an array of objects:

[{ "rate": 2 }, { "rate": 338 }]

Actually, .NET List<T> is serialized as a JSON array and a Dictionary<TKey, TValue> is serialized as a JSON object, or in other words, as an associative array.

3 Comments

I need to send an array rates to a specific Web API service built with PHP. So is it possible to make a List of Dictionaries and after convert them into JavaScript array and send it to that web service?
@Oraz, that's a different question than what is posted. It may be best to solve this question first, then post a different question.
@Oraz Add JSON.NET to your solution/project's and a simple JsonConvert.Serialize giving the whole list of dictionaries will produce the desired output. For example, I expect a JSON like this: [{ "rate": 438, "otherProperty": "whatever" }, { "rate": 4101, "otherProperty": "whatever 2" }]
0

This can depend on your specific neeeds, mut maybe you just want a list of objects

first create a class:

class Rate
{
    public int LoginId { get; set; }
    public int? LineId { get; set; }
    public decimal RateValue { get; set; }
    public bool IsChanged { get; set; }
}

Then, in any method you want, just use:

List<Rate> Rates = new List<Rate>();
Rates.Add(new Rate() {LoginId = 1, LineId = null, RateValue = Rateval, IsChanged = false});
Rates.Add(new Rate() {LoginId = 13, LineId = null, RateValue = SecVal, IsChanged = false});

EDIT

My apologies for the terrible answer, edited to account for the errors

6 Comments

-1 - This won't even compile.
This also has, what most would consider, bad practices that being public fields.
@ErikPhilips - It has no public fields. It only has private fields, which is one of the big reasons it won't compile.
@MikeChristensen yes you are correct. I was looking at the example setting fields, didn't notice the missing access modifiers.
In the future I'd recommend testing your code in the IDE before posting it, until you are very, very familiar with C#.
|
0
public struct Rate
        {
            public int LoginId ;
            public int LineId ;
            public double RateValue ;
            public bool IsChanged;
        }

        public static void makelist()
        {
             List<Rate> Rates = new List<Rate>();
             Rates.Add(new Rate() {LoginId = 1, LineId = null, RateValue = Rateval,IsChanged = false});
        }

This method will only hold data, and not hold methods like a class.

With the data types defined in the struct, memory usage stays low as its only purpose is to store data. This is a midway between a variable and a class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.