splitting code in 2 files

LoneWolf78

New member
Joined
Aug 4, 2017
Messages
4
Programming Experience
Beginner
I'm having some problems trying to move some code for better structure and reading so everything doesnt get clutterd.
But for some reasong I can not find the correct way to split this one.

The first part (piece of much more) need to be cut away from the second part (also just a piece of much more)
but as soon as I put it in a seprated class everything breaks. I can no longer call GetIdentiy()

I also tried to put it in a namespace but that gave me the error "A namespace cannot directly contain members such as fields or methods"
or I get "GetIdentity does not exist in the curent context"

this can not be so hard. I'm probably overlooking the obvious here.

(I just posted one problem line and what i'm trying to call. the code is working if its in the same file)


C#:
    void Update() {
        GW2_Mumble.Identity.FoV = GetIdentity().Fov; // <---- this line breaks
    }



// This piece needs to go in a sperate file. the code is getting to big to be good to read/manipulate

    public GW2Identity GetIdentity() 
    {
        var link = new MumbleLink();
        MumbleLinkedMemory MumbleData = link.Read();
        var identity = MumbleData.Identity;
        var stop = Array.IndexOf(MumbleData.Identity, '\0');
        unsafe
        {
            fixed (char* addr = identity)
            {
                return JsonConvert.DeserializeObject<GW2Identity>(new String(addr));
            }
        }
    }
 
Move GetIdentity() to a new file and make the class name the same as the class name in the current file. Before class, in both files, put partial behind it.

Here is an example. Let's say your class is Foo and inside of the class you currently have public GW2Identity GetIdentity() - It would look something like:

public class Foo
{
     public GW2Identity GetIdentity()
     {
          // Code ...
     }
}


You need to create a new file that has public class partial Foo and your GetIdentity function inside of the class like such:

public partial class Foo
{
     public GW2Identity GetIdentity()
     {
          // Code ...
     }
}


Then you need to change
public class Foo to public partial class Foo in the old file.
 
Last edited:
OP, what nebraskacoder said is accurate, however, I'd point out that if you want things properly encapsulated you'd only use a partial class if the functionality would otherwise be in your current class. In other words, keep you classes specific. If what you are moving truly belongs in another class then put it there rather than a partial class.

Sent from my SM-G935T using Tapatalk
 
Back
Top Bottom