Resolved Using struct

RobertbNZ

Active member
Joined
Jun 26, 2020
Messages
37
Programming Experience
Beginner
I'm having trouble using a struct. Why doesn't this work: -
C#:
        Changes Changed = new Changes();
        Changed.Skip = true;
        struct Changes
        {
            public bool Skip;
            public bool EMPNO;
        }
The line "Changed.Skip = true; is rejected with messages: -
IDE1007 The name 'Changed' does not exist in the current context
IDE1007 The name 'Changed.Skip' does not exist in the current context
In researching this I found this site where I replaced their example of struct with mine by posting this code: -
C#:
using System;               
public class Program
{
    public static void Main()
    {
        Changes Changed = new Changes();
        Changed.Skip = true;
        Console.WriteLine(Changed.Skip);
        Console.WriteLine(Changed.EMPNO);
    }
}
struct Changes
{
    public bool Skip;
    public bool EMPNO;
}
This worked perfectly, producing the expected result
True
False
I have tried moving my definition of Changes to match this code structure, first to follow the the class containing Changes Changed = new Changes();, and then to the end of the object where it follows
C#:
namespace MyJSv
{
    
}
In all cases I get the two IDE1007 messages from the 2nd of these statements, but there is no error on the first. If I write "Changed." there is no Intellisense list.
C#:
        Changes Changed = new Changes();
        Changed.Skip = true;
 
Your first code snippet makes no sense. It suggests that you have tried to declare a structure inside a method. Why would that be a good idea? If that's not what you did, you should post the code to show what you actually did.

I don't understand why so many people are determined to make their life more difficult where structures are concerned. Treat a structure the same way you would treat a class. How would you declare a new class? You'd create a new code file named after that class, right? Do the same with a structure. Don't even declare it after an existing class. Structures are first class types so treat then that way.
 
You'd create a new code file named after that class, right? Do the same with a structure. Don't even declare it after an existing class. Structures are first class types so treat then that way.
So I tried this. I created this object
C#:
namespace MyJSv
{
    public struct Changes
    {
        public bool Skip;
        public bool EMPNO;
    }
}
and then I wrote
C#:
        private Changes Changed = new Changes();
        Changed.Skip = true;
Now "Skip" is coloured turquoise, like Changes, indicating that it's a type, and I get message CS1519 TWICE for this line: -
C1519 Invalid token '=' in class, struct, or interface member declaration

I don't really want to have to generate an external class, so I tried to use a normal bool field like this. I can easily generate a series of definitions like this at the start of the interface class JSPG2Client (see this previous query if you want some context)
C#:
        private bool Changed_Skip = false;
        Changed_Skip = true;
Now, as well as two occurrences of message C1519, I also got message IDE1007: -
IDE1007 The name 'Changed_Skip' does not exist in the current context
Same results if I omit 'private', which is the default anyway.

BTW, I have used basically the same code with an internally-defined struct in my VB.NET project without any difficulty. Are the rules different with VB.NET? Also, I didn't see anything in MS documentation saying that I couldn't define a struct within a class.
 
BTW, I have used basically the same code with an internally-defined struct in my VB.NET project without any difficulty.
Doesn't work for me in VB.NET:
VB.NET:
Imports System

Module Program
    Public Structure Changes
        Public Skip As Boolean
        Public EMPNO As Boolean
    End Structure

    Private Changed As Changes = New Changes
    Changed.Skip = True

    Sub Main(args As String())
    End Sub
End Module

I get an error on line 10: BC30188 Declaration expected.
 
This is a code statement, which belongs in a method.
Thanks John, this is the crucial comment that led to my AHA! I couldn't understand why the assignments were causing errors, because it seemed to me that what I was written was identical in form to previous code that I'd written that was OK. There was no problem when I organized the code like this. I get errors when the statements are written where I've commented them out, but not when they're written within the set method.
C#:
{
    public class JSPG2Client
    {
...
        private Changes Changed = new Changes();
        bool Changed_Skip = false;
        //Changed.Skip = true;
        //Changed_Skip = true;
        private string _Function = null;
        private string Function // => Request.Function
        {
            set
            {
...
                Changed.Skip = true;
                Changed_Skip = true;
            }
        }
So this was the critical factor that I didn't understand. This problem had nothing to do with struct, or the fact that it was defined internally. Thus I get the same results with
C#:
        private struct SetByUser
        {
            public bool Skip;
            public bool EMPNO;
        }
        private SetByUser Changed = new SetByUser();

So thanks John, you've sorted it for me.
 
Don't skip the basic topics when learning a new language ;)
 
Don't skip the basic topics when learning a new language ;)
You're right, but like all developers I want to get on as quickly as possible and leverage what I already know. Are there any tutorials aimed at experienced VB.Net programmers who are learning C#. If not, then MS should develop some ASAP, particularly as it has been made clear that VB is not going to be developed further, and so there'll be many like me who are making a transition. It is a real pain reading stuff that tells us what we already know, while omitting key ideas we need to know because the obvious implication from our previous knowledge is wrong.
 
I want to [...] leverage what I already know.
And yet you didn't. As was pointed out, what you were doing would have been no more valid in VB than in C#. The only thing allowed at the type level in either language is declarations and any assignments must be initialisations of declared fields. If you had leveraged what you already knew, there would have been no issue in the first place.
 
Sadly, there are not an awful lot of tutorials on moving from VB.Net to C# by Microsoft themselves. But there are blogs wrote by MVP's and what not who have covered some of the transitioning expectations and they provide some pointers on Developer Blogs, but you will need to search through them yourself, as I can't think of any particular one which you might find useful. It's very easy for some of us who write in a variety of languages to dismiss this as not challenging. I remember when I decided to take up VB.Net after C#....one of the things which you might find helpful while writing code, is to write it out in the language you know, such as VB.Net and then translate it into C# if you can't workout how it should be wrote in C#. Doing this will help you build up familiarity but don't depend on it as a way forward. Online translators are good for stuff like that. There are also yards of info from people on search engines who are trying or considering to make the transition. Reading some of the comments on some of those articles might help you get some extra pointers. You can also find converters from VB.Net to C# vice versa on Visual Studios MarketPlace.
 
Online translators are good for stuff like that.
In my experience, many online translators start to choke on anything at all advanced. I would suggest downloading and installing Instant C# from Tangible Software Solutions if converting VB to C# is something you expect to do often. Full disclosure, I was provided with a free license to the full version when I was an MVP but that expired some time ago. I still use the free versions of Instant VB and Instant C# from time to time.
 
And yet you didn't.
This is valid in VB
VB.NET:
        Dim Changed_Skip As Boolean = False
        Changed_Skip = True
while this is invalid in C#
C#:
        bool Changed_Skip = false;
        Changed_Skip = true;
Presumably VB.Net is implicitly distinguishing between code defining the type, and imperative code like the assignment, whereas with C# this needs to be explicit with { }. I was led astray because I made the obvious assumption from my previous knowledge. Hence my wish for a suitable conversion tutorial for people like me. Converters like Code Converter C# to VB and VB to C# – Telerik render the valid VB code as invalid C#.
C#:
{
    bool Changed_Skip = false;
    Changed_Skip = true;
}
 
In my experience, many online translators start to choke on anything at all advanced. I would suggest downloading and installing Instant C# from Tangible Software Solutions if converting VB to C# is something you expect to do often. Full disclosure, I was provided with a free license to the full version when I was an MVP but that expired some time ago. I still use the free versions of Instant VB and Instant C# from time to time.
This one? - VB.NET to C# Converter - I haven't heard of it before. It will be interesting to try it out, and see how it fares against some of the online translated variations. The one I also posted from the VS Market Place, isn't to bad. You're right about most of the online translators choking on a large wad of code. But I was really implying that the OP would use it for simple transliterations and not complex methods.
 
Back
Top Bottom