5

Update #2 - Mystery Solved

I have figure out the issue - it was my misunderstanding of the keyword static when being use in java inner classes. I assumed that static meant static in the traditional sense - as with c#. In the case of Java, static inner classes have a slightly different meaning. I personally would have used a different keyword other than static to achieve the same effect to eliminate the confusion.

Here are a couple good links that explain what static inner classes mean in java.

link1 link2

Sorry for sending everyone of a wild goose chase :)

Original Post

In java I can write the following:

public class UseStaticMembers {
    private Holder holder;

    holder.txt1 = "text";
    holder.txt2 = "text";

    CallSomeMethod(holder);
}


static class Holder {
    public string txt1;
    public string txt2;
}

But I cannot do this in C#. I get the following error: "Cannot declare a variable of static type 'Holder'" on the line: "private Holder holder;"

How can I achieve the same effect in C# (if I can).

Update #1

Here is an example of how this pattern is used to optimize custom list adapters. As you can see I can't just access the static member via the static class name but need to reference it via an variable. It needs to be passed to the Tag.

public class WeatherAdapter extends ArrayAdapter<Weather>{

Context context; 
int layoutResourceId;    
Weather data[] = null;

public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    WeatherHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new WeatherHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

        row.setTag(holder);
    }
    else
    {
        holder = (WeatherHolder)row.getTag();
    }

    Weather weather = data[position];
    holder.txtTitle.setText(weather.title);
    holder.imgIcon.setImageResource(weather.icon);

    return row;
}

static class WeatherHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}

}

5
  • 1
    If you want an instance of Holder, why make it static? Feb 24, 2012 at 16:22
  • @IUnknown you should know what is the difference between static and instance members. They are different.
    – nawfal
    Feb 24, 2012 at 16:23
  • 1
    what effect are you trying to achieve?- ie what does using a static class property in this way in java do that an instance property does not? Feb 24, 2012 at 16:23
  • @JonnyCundall The above code is how I implement custom list adapter in android. I was trying to use the same pattern in monodroid (c#) but c# does not seem to have the same language support for static as java.
    – IUnknown
    Feb 24, 2012 at 16:26
  • @nawfal I know the difference :) I am trying use the same pattern for optimizing custom list adapters.
    – IUnknown
    Feb 24, 2012 at 16:27

4 Answers 4

3

Because you cannot create an instance of the static type in C#.

You can directly access the methods and the properties of the static type in c#.

Access Static class members

Staticclass.PropetyName;
Staticclass.methodName();

Static class

public static Staticclass
{
  public type PropetyName { get ; set; }

  public type methodName()
  {
    // code 
    return typevariable;
  }
}

So there is no need to create an instance of Static type, which is illegal according to C# language grammar.

4
  • That is what I thought. I know I can access the members via the static class name as you wrote. I needed to pass it to another method - that was the requirement - like I can do in Java: CallSomeMethod(holder);
    – IUnknown
    Feb 24, 2012 at 16:24
  • In that case you need not pass. You can access tx1 and txt2 from anywhere by just calling Holder.tx1 etc
    – nawfal
    Feb 24, 2012 at 16:25
  • 1
    If that's the case I would make the class a Singleton; it's a pattern using statics that will ensure that only one instance exists, and can be retrieved statically, but once gotten it can be used as if it were an instance class. Have a look at csharpindepth.com/Articles/General/Singleton.aspx
    – KeithS
    Feb 24, 2012 at 16:27
  • @KeithS Thanks that was my next option - did not what to make a singleton until I knew for sure I could not do this in C#.
    – IUnknown
    Feb 24, 2012 at 16:35
0

Static classes in .NET are not instantiated, so there is really no point in storing them to be used later. You simply use them:

Holder.txt1 = "text";
Holder.SomeMethod();

So, in the above, if I modify txt1, then txt1 will be changed for EVERY object that calls it moving forward. That is why it is called static; it's values are set and static for everything that calls it. Hopefully that helps.

If not, here is the MSDN link: http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

It should clarify static a little more :)

0

Do you actually want to instantiate Holder? You don't need an instance to access static members.

If you really do, just remove static from static class Holder { ... }.

3
  • @IUnknown WeatherHolder.imgIcon and WeatherHolder.txtTitle are not static in your update. Make up your mind: do you want static members or not? BTW, the update won't even compile (in C#) unless you remove static from static class WeatherHolder { ... }. Feb 24, 2012 at 16:39
  • your right sorry fixed the original code above - copied from the test code. Still the problem remains. I want a static "holder" class that I can reference in code. C# intprets the updated code above as "does not allow a non=static field in a static class". This seems to be a fundamental difference between Java and c# regardin statics.
    – IUnknown
    Feb 24, 2012 at 16:47
  • So, if txt1 and txt2 are not static, why do you feel you should make their enclosing class static? Do you actually want a static instance of Holder? Feb 24, 2012 at 17:08
0

Because in C# private Holder holder; creates an instance of Holder class, and you cannot create an instance of Static class. Just use the class.

Holder.txt1 = "text";
Holder.txt2 = "text";
CallSomeMethod(holder);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.